1
0
Fork 0
forked from blallo/rss-bridge

[FlickrExplore] Fix and improve bridge

Instead of utilizing API requests for each element, the information
is now read directly from the source page, which provides information
as JSON data embedded in a script block.

The author name is returned for each element.

Improves the title and optionally adds the description if available
This commit is contained in:
logmanoriginal 2017-02-11 11:26:52 +01:00
parent 9bf74b2715
commit 0f25684e65

View file

@ -1,42 +1,68 @@
<?php <?php
class FlickrExploreBridge extends BridgeAbstract{ class FlickrExploreBridge extends BridgeAbstract {
const MAINTAINER = "sebsauvage"; const MAINTAINER = "sebsauvage";
const NAME = "Flickr Explore"; const NAME = "Flickr Explore";
const URI = "https://www.flickr.com/"; const URI = "https://www.flickr.com/";
const CACHE_TIMEOUT = 21600; // 6 const CACHE_TIMEOUT = 21600; // 6 hours
const DESCRIPTION = "Returns the latest interesting images from Flickr"; const DESCRIPTION = "Returns the latest interesting images from Flickr";
public function collectData(){ public function collectData(){
$html = getSimpleHTMLDOM(self::URI.'explore') $html = getSimpleHTMLDOM(self::URI . 'explore')
or returnServerError('Could not request Flickr.'); or returnServerError('Could not request Flickr.');
foreach($html->find('.photo-list-photo-view') as $element) { // Find SCRIPT containing JSON data
// Get the styles $model = $html->find('.modelExport', 0);
$style = explode(';', $element->style); $model_text = $model->innertext;
// 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);
// Get the image JSON via Flickr API // Find start and end of JSON data
$imageJSON = json_decode(getContents( $start = strpos($model_text, 'modelExport:') + strlen('modelExport:');
'https://api.flickr.com/services/rest/?' $end = strpos($model_text, 'auth:') - strlen('auth:');
.'method=flickr.photos.getInfo&'
.'api_key=103b574d49bd51f0e18bfe907da44a0f&'
.'photo_id='.$imageID.'&'
.'format=json&'
.'nojsoncallback=1'
)) or returnServerError('Could not request Flickr.'); // FIXME: Request time too long...
$item = array(); // Dissect JSON data and remove trailing comma
$item['uri'] = self::URI.'photo.gne?id='.$imageID; $model_text = trim(substr($model_text, $start, $end - $start));
$item['content'] = '<a href="' . $item['uri'] . '"><img src="' . $imageURI . '" /></a>'; // FIXME: Filter javascript ? $model_text = substr($model_text, 0, strlen($model_text) - 1);
$item['title'] = $imageJSON->photo->title->_content;
$this->items[] = $item; $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]['photos']['_data'] as $element){
if($element['id'] === $imageID){
$item = array();
$item['author'] = (array_key_exists('username', $element) ? $element['username'] : 'Anonymous');
$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;
}
}
} }
} }
} }