2017-02-11 14:36:08 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/* This is a mashup of FlickrExploreBridge by sebsauvage and FlickrTagBridge
|
|
|
|
* by erwang, providing the functionality of both in one.
|
|
|
|
*/
|
|
|
|
class FlickrBridge extends BridgeAbstract {
|
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
const MAINTAINER = 'logmanoriginal';
|
|
|
|
const NAME = 'Flickr Bridge';
|
|
|
|
const URI = 'https://www.flickr.com/';
|
|
|
|
const CACHE_TIMEOUT = 21600; // 6 hours
|
|
|
|
const DESCRIPTION = 'Returns images from Flickr';
|
|
|
|
|
|
|
|
const PARAMETERS = array(
|
|
|
|
'Explore' => array(),
|
|
|
|
'By keyword' => array(
|
|
|
|
'q' => array(
|
|
|
|
'name' => 'Keyword',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'title' => 'Insert keyword',
|
|
|
|
'exampleValue' => 'bird'
|
2020-11-16 20:33:48 +03:00
|
|
|
),
|
|
|
|
'media' => array(
|
|
|
|
'name' => 'Media',
|
|
|
|
'type' => 'list',
|
|
|
|
'values' => array(
|
|
|
|
'All (Photos & videos)' => 'all',
|
|
|
|
'Photos' => 'photos',
|
|
|
|
'Videos' => 'videos',
|
|
|
|
),
|
|
|
|
'defaultValue' => 'all',
|
|
|
|
),
|
|
|
|
'sort' => array(
|
|
|
|
'name' => 'Sort By',
|
|
|
|
'type' => 'list',
|
|
|
|
'values' => array(
|
|
|
|
'Relevance' => 'relevance',
|
|
|
|
'Date uploaded' => 'date-posted-desc',
|
|
|
|
'Date taken' => 'date-taken-desc',
|
|
|
|
'Interesting' => 'interestingness-desc',
|
|
|
|
),
|
|
|
|
'defaultValue' => 'relevance',
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'By username' => array(
|
|
|
|
'u' => array(
|
|
|
|
'name' => 'Username',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'title' => 'Insert username (as shown in the address bar)',
|
|
|
|
'exampleValue' => 'flickr'
|
2020-11-16 20:33:48 +03:00
|
|
|
),
|
|
|
|
'media' => array(
|
|
|
|
'name' => 'Media',
|
|
|
|
'type' => 'list',
|
|
|
|
'values' => array(
|
|
|
|
'All (Photos & videos)' => 'all',
|
|
|
|
'Photos' => 'photos',
|
|
|
|
'Videos' => 'videos',
|
|
|
|
),
|
|
|
|
'defaultValue' => 'all',
|
|
|
|
),
|
|
|
|
'sort' => array(
|
|
|
|
'name' => 'Sort By',
|
|
|
|
'type' => 'list',
|
|
|
|
'values' => array(
|
|
|
|
'Relevance' => 'relevance',
|
|
|
|
'Date uploaded' => 'date-posted-desc',
|
|
|
|
'Date taken' => 'date-taken-desc',
|
|
|
|
'Interesting' => 'interestingness-desc',
|
|
|
|
),
|
|
|
|
'defaultValue' => 'date-posted-desc',
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
2018-07-22 15:02:40 +03:00
|
|
|
)
|
2017-02-11 18:16:56 +03:00
|
|
|
);
|
|
|
|
|
2020-11-16 20:33:48 +03:00
|
|
|
private $username = '';
|
|
|
|
|
|
|
|
public function collectData() {
|
2018-07-22 15:02:40 +03:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
switch($this->queriedContext) {
|
2018-07-22 15:02:40 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
case 'Explore':
|
2018-07-22 15:02:40 +03:00
|
|
|
$filter = 'photo-lite-models';
|
2020-11-16 20:33:48 +03:00
|
|
|
$html = getSimpleHTMLDOM($this->getURI())
|
2017-02-11 18:16:56 +03:00
|
|
|
or returnServerError('Could not request Flickr.');
|
|
|
|
break;
|
2018-07-22 15:02:40 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
case 'By keyword':
|
2018-07-22 15:02:40 +03:00
|
|
|
$filter = 'photo-lite-models';
|
2020-11-16 20:33:48 +03:00
|
|
|
$html = getSimpleHTMLDOM($this->getURI())
|
2017-02-11 18:16:56 +03:00
|
|
|
or returnServerError('No results for this query.');
|
|
|
|
break;
|
2018-07-22 15:02:40 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
case 'By username':
|
2020-11-16 20:33:48 +03:00
|
|
|
//$filter = 'photo-models';
|
|
|
|
$filter = 'photo-lite-models';
|
|
|
|
$html = getSimpleHTMLDOM($this->getURI())
|
2017-02-11 18:16:56 +03:00
|
|
|
or returnServerError('Requested username can\'t be found.');
|
2020-11-16 20:33:48 +03:00
|
|
|
|
|
|
|
$this->username = $this->getInput('u');
|
|
|
|
|
|
|
|
if ($html->find('span.search-pill-name', 0)) {
|
|
|
|
$this->username = $html->find('span.search-pill-name', 0)->plaintext;
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
break;
|
2018-07-22 15:02:40 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
default:
|
|
|
|
returnClientError('Invalid context: ' . $this->queriedContext);
|
2018-07-22 15:02:40 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
$model_json = $this->extractJsonModel($html);
|
|
|
|
$photo_models = $this->getPhotoModels($model_json, $filter);
|
|
|
|
|
|
|
|
foreach($photo_models as $model) {
|
|
|
|
$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', $model)) {
|
2020-11-16 20:33:48 +03:00
|
|
|
$item['author'] = urldecode($model['username']);
|
2018-07-22 15:02:40 +03:00
|
|
|
} elseif (array_key_exists('owner', reset($model_json)[0])) {
|
2020-11-16 20:33:48 +03:00
|
|
|
$item['author'] = urldecode(reset($model_json)[0]['owner']['username']);
|
2018-07-22 15:02:40 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 20:33:48 +03:00
|
|
|
$item['title'] = urldecode((array_key_exists('title', $model) ? $model['title'] : 'Untitled'));
|
2018-07-22 15:02:40 +03:00
|
|
|
$item['uri'] = self::URI . 'photo.gne?id=' . $model['id'];
|
|
|
|
|
|
|
|
$description = (array_key_exists('description', $model) ? $model['description'] : '');
|
|
|
|
|
|
|
|
$item['content'] = '<a href="'
|
|
|
|
. $item['uri']
|
|
|
|
. '"><img src="'
|
|
|
|
. $this->extractContentImage($model)
|
|
|
|
. '" style="max-width: 640px; max-height: 480px;"/></a><br><p>'
|
2020-11-16 20:33:48 +03:00
|
|
|
. urldecode($description)
|
2018-07-22 15:02:40 +03:00
|
|
|
. '</p>';
|
|
|
|
|
|
|
|
$item['enclosures'] = $this->extractEnclosures($model);
|
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-16 20:33:48 +03:00
|
|
|
public function getURI() {
|
|
|
|
|
|
|
|
switch($this->queriedContext) {
|
|
|
|
case 'Explore':
|
|
|
|
return self::URI . 'explore';
|
|
|
|
break;
|
|
|
|
case 'By keyword':
|
|
|
|
return self::URI . 'search/?q=' . urlencode($this->getInput('q'))
|
|
|
|
. '&sort=' . $this->getInput('sort') . '&media=' . $this->getInput('media');
|
|
|
|
break;
|
|
|
|
case 'By username':
|
|
|
|
return self::URI . 'search/?user_id=' . urlencode($this->getInput('u'))
|
|
|
|
. '&sort=' . $this->getInput('sort') . '&media=' . $this->getInput('media');
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return parent::getURI();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
|
|
|
|
switch($this->queriedContext) {
|
|
|
|
case 'Explore':
|
|
|
|
return 'Explore - ' . self::NAME;
|
|
|
|
break;
|
|
|
|
case 'By keyword':
|
|
|
|
return $this->getInput('q') . ' - keyword - ' . self::NAME;
|
|
|
|
break;
|
|
|
|
case 'By username':
|
|
|
|
return $this->username . ' - ' . self::NAME;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return parent::getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getName();
|
|
|
|
}
|
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
private function extractJsonModel($html) {
|
|
|
|
|
2017-02-11 18:16:56 +03: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:');
|
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
// Extract JSON data, remove trailing comma
|
2017-02-11 18:16:56 +03:00
|
|
|
$model_text = trim(substr($model_text, $start, $end - $start));
|
|
|
|
$model_text = substr($model_text, 0, strlen($model_text) - 1);
|
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
return json_decode($model_text, true);
|
|
|
|
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
private function getPhotoModels($json, $filter) {
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
// The JSON model contains a "legend" array, where each element contains
|
|
|
|
// the path to an element in the "main" object
|
|
|
|
$photo_models = array();
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
foreach($json['legend'] as $legend) {
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
$photo_model = $json['main'];
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
foreach($legend as $element) { // Traverse tree
|
|
|
|
$photo_model = $photo_model[$element];
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
// We are only interested in content
|
|
|
|
if($photo_model['_flickrModelRegistry'] === $filter) {
|
|
|
|
$photo_models[] = $photo_model;
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
return $photo_models;
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function extractEnclosures($model) {
|
|
|
|
|
|
|
|
$areas = array();
|
|
|
|
|
|
|
|
foreach($model['sizes'] as $size) {
|
|
|
|
$areas[$size['width'] * $size['height']] = $size['url'];
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-23 21:14:38 +03:00
|
|
|
return array($this->fixURL(max($areas)));
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-07-22 15:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function extractContentImage($model) {
|
|
|
|
|
|
|
|
$areas = array();
|
|
|
|
$limit = 320 * 240;
|
|
|
|
|
|
|
|
foreach($model['sizes'] as $size) {
|
|
|
|
|
|
|
|
$image_area = $size['width'] * $size['height'];
|
|
|
|
|
|
|
|
if($image_area >= $limit) {
|
|
|
|
$areas[$image_area] = $size['url'];
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2018-07-22 15:02:40 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2018-07-22 15:02:40 +03:00
|
|
|
|
2018-07-23 21:14:38 +03:00
|
|
|
return $this->fixURL(min($areas));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function fixURL($url) {
|
|
|
|
|
|
|
|
// For some reason the image URLs don't include the protocol (https)
|
|
|
|
if(strpos($url, '//') === 0) {
|
|
|
|
$url = 'https:' . $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
2018-07-22 15:02:40 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2017-02-11 14:36:08 +03:00
|
|
|
}
|