2014-05-26 19:45:10 +02:00
|
|
|
<?php
|
2017-02-11 12:20:05 +01:00
|
|
|
class FlickrTagBridge extends BridgeAbstract {
|
2014-05-26 19:45:10 +02:00
|
|
|
|
2016-08-30 11:23:55 +02:00
|
|
|
const MAINTAINER = "erwang";
|
|
|
|
const NAME = "Flickr TagUser";
|
|
|
|
const URI = "http://www.flickr.com/";
|
2016-09-25 17:04:28 +02:00
|
|
|
const CACHE_TIMEOUT = 21600; //6h
|
2016-08-30 11:23:55 +02:00
|
|
|
const DESCRIPTION = "Returns the tagged or user images from Flickr";
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2016-08-30 11:23:55 +02:00
|
|
|
const PARAMETERS = array(
|
2016-08-27 21:03:26 +02:00
|
|
|
'By keyword' => array(
|
2016-08-28 19:53:05 +02:00
|
|
|
'q'=>array(
|
|
|
|
'name'=>'keyword',
|
|
|
|
'required'=>true
|
|
|
|
)
|
2016-08-27 21:03:26 +02:00
|
|
|
),
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2016-08-27 21:03:26 +02:00
|
|
|
'By username' => array(
|
2016-08-28 19:53:05 +02:00
|
|
|
'u'=>array(
|
|
|
|
'name'=>'Username',
|
|
|
|
'required'=>true
|
|
|
|
)
|
2016-08-27 21:03:26 +02:00
|
|
|
),
|
|
|
|
);
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
public function collectData(){
|
2016-08-28 19:53:05 +02:00
|
|
|
switch($this->queriedContext){
|
|
|
|
case 'By keyword':
|
2017-02-11 12:20:05 +01:00
|
|
|
$key = 'photos';
|
|
|
|
$html = getSimpleHTMLDOM(self::URI . 'search/?q=' . urlencode($this->getInput('q')) . '&s=rec')
|
2016-09-25 23:22:33 +02:00
|
|
|
or returnServerError('No results for this query.');
|
2016-08-28 19:53:05 +02:00
|
|
|
break;
|
2017-02-11 12:20:05 +01:00
|
|
|
case 'By username':
|
|
|
|
$key = 'photoPageList';
|
|
|
|
$html = getSimpleHTMLDOM(self::URI . 'photos/' . urlencode($this->getInput('u')))
|
2016-09-25 23:22:33 +02:00
|
|
|
or returnServerError('Requested username can\'t be found.');
|
2016-08-28 19:53:05 +02:00
|
|
|
break;
|
2017-02-11 12:20:05 +01:00
|
|
|
default:
|
|
|
|
returnClientError('Invalid context: ' . $this->queriedContext);
|
2014-05-26 19:45:10 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 12:20:05 +01:00
|
|
|
// Find SCRIPT containing JSON data
|
|
|
|
$model = $html->find('.modelExport', 0);
|
|
|
|
$model_text = $model->innertext;
|
|
|
|
|
|
|
|
// Find start and end of JSON data
|
|
|
|
$start = strpos($model_text, 'modelExport:') + strlen('modelExport:');
|
|
|
|
$end = strpos($model_text, 'auth:') - strlen('auth:');
|
|
|
|
|
|
|
|
// Dissect JSON data and remove trailing comma
|
|
|
|
$model_text = trim(substr($model_text, $start, $end - $start));
|
|
|
|
$model_text = substr($model_text, 0, strlen($model_text) - 1);
|
|
|
|
|
|
|
|
$model_json = json_decode($model_text, true);
|
|
|
|
|
|
|
|
foreach($html->find('.photo-list-photo-view') as $element){
|
|
|
|
// Get the styles
|
|
|
|
$style = explode(';', $element->style);
|
|
|
|
|
|
|
|
// Get the background-image style
|
|
|
|
$backgroundImage = explode(':', end($style));
|
|
|
|
|
|
|
|
// URI type : url(//cX.staticflickr.com/X/XXXXX/XXXXXXXXX.jpg)
|
|
|
|
$imageURI = trim(str_replace(['url(', ')'], '', end($backgroundImage)));
|
|
|
|
|
|
|
|
// Get the image ID
|
|
|
|
$imageURIs = explode('_', basename($imageURI));
|
|
|
|
$imageID = reset($imageURIs);
|
|
|
|
|
|
|
|
// Use JSON data to build items
|
|
|
|
foreach(reset($model_json)[0][$key]['_data'] as $element){
|
|
|
|
if($element['id'] === $imageID){
|
|
|
|
$item = array();
|
|
|
|
|
|
|
|
/* Author name depends on scope. On a keyword search the
|
|
|
|
* author is part of the picture data. On a username search
|
|
|
|
* the author is part of the owner data.
|
|
|
|
*/
|
|
|
|
if(array_key_exists('username', $element)){
|
|
|
|
$item['author'] = $element['username'];
|
|
|
|
} elseif (array_key_exists('owner', reset($model_json)[0])){
|
|
|
|
$item['author'] = reset($model_json)[0]['owner']['username'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$item['title'] = (array_key_exists('title', $element) ? $element['title'] : 'Untitled');
|
|
|
|
$item['uri'] = self::URI . 'photo.gne?id=' . $imageID;
|
|
|
|
|
|
|
|
$description = (array_key_exists('description', $element) ? $element['description'] : '');
|
|
|
|
|
|
|
|
$item['content'] = '<a href="'
|
|
|
|
. $item['uri']
|
|
|
|
. '"><img src="'
|
|
|
|
. $imageURI . '" /></a>'
|
|
|
|
. '<br>'
|
|
|
|
. '<p>'
|
|
|
|
. $description
|
|
|
|
. '</p>';
|
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 19:45:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|