From 0f25684e6521921d1ffaf2a3006234c3e713dd57 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 11 Feb 2017 11:26:52 +0100 Subject: [PATCH 1/4] [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 --- bridges/FlickrExploreBridge.php | 88 +++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 31 deletions(-) diff --git a/bridges/FlickrExploreBridge.php b/bridges/FlickrExploreBridge.php index 170bec03..53226592 100644 --- a/bridges/FlickrExploreBridge.php +++ b/bridges/FlickrExploreBridge.php @@ -1,42 +1,68 @@ 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); + // Find SCRIPT containing JSON data + $model = $html->find('.modelExport', 0); + $model_text = $model->innertext; - // Get the image JSON via Flickr API - $imageJSON = json_decode(getContents( - 'https://api.flickr.com/services/rest/?' - .'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... + // Find start and end of JSON data + $start = strpos($model_text, 'modelExport:') + strlen('modelExport:'); + $end = strpos($model_text, 'auth:') - strlen('auth:'); - $item = array(); - $item['uri'] = self::URI.'photo.gne?id='.$imageID; - $item['content'] = ''; // FIXME: Filter javascript ? - $item['title'] = $imageJSON->photo->title->_content; - $this->items[] = $item; + // 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]['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'] = '' + . '
' + . '

' + . $description + . '

'; + + $this->items[] = $item; + + break; + } + } } } } From 7ad8693b5f855c3482fcb82018972081d759994a Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 11 Feb 2017 12:20:05 +0100 Subject: [PATCH 2/4] [FlickrTagBridge] Fix and improve bridge by using the FlickrExploreBridge approach --- bridges/FlickrTagBridge.php | 82 +++++++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 12 deletions(-) diff --git a/bridges/FlickrTagBridge.php b/bridges/FlickrTagBridge.php index 2662c575..91d6b809 100644 --- a/bridges/FlickrTagBridge.php +++ b/bridges/FlickrTagBridge.php @@ -1,5 +1,5 @@ queriedContext){ case 'By keyword': - $html = getSimpleHTMLDOM(self::URI.'search/?q='.urlencode($this->getInput('q')).'&s=rec') + $key = 'photos'; + $html = getSimpleHTMLDOM(self::URI . 'search/?q=' . urlencode($this->getInput('q')) . '&s=rec') or returnServerError('No results for this query.'); break; - case 'by username': - $html = getSimpleHTMLDOM(self::URI.'photos/'.urlencode($this->getInput('u')).'/') + case 'By username': + $key = 'photoPageList'; + $html = getSimpleHTMLDOM(self::URI . 'photos/' . urlencode($this->getInput('u'))) or returnServerError('Requested username can\'t be found.'); break; + default: + returnClientError('Invalid context: ' . $this->queriedContext); } - foreach($html->find('span.photo_container') as $element) { - $item = array(); - $item['uri'] = self::URI.$element->find('a',0)->href; - $thumbnailUri = $element->find('img',0)->getAttribute('data-defer-src'); - $item['content'] = ''; // FIXME: Filter javascript ? - $item['title'] = $element->find('a',0)->title; - $this->items[] = $item; + // 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'] = '' + . '
' + . '

' + . $description + . '

'; + + $this->items[] = $item; + + break; + } + } } } } - From f16835c223a421ed708bb23f0c0fbc74184556f1 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 11 Feb 2017 12:36:08 +0100 Subject: [PATCH 3/4] [FlickrBridge] Add new bridge This bridge is a mashup of the existing FlickrExploreBridge by sebsauvage and FlickrTagBridge by erwang. It provides the same functionality as one single bridge. --- bridges/FlickrBridge.php | 121 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 bridges/FlickrBridge.php diff --git a/bridges/FlickrBridge.php b/bridges/FlickrBridge.php new file mode 100644 index 00000000..988c55fe --- /dev/null +++ b/bridges/FlickrBridge.php @@ -0,0 +1,121 @@ + array(), + 'By keyword' => array( + 'q' => array( + 'name' => 'Keyword', + 'type' => 'text', + 'required' => true, + 'title' => 'Insert keyword', + 'exampleValue' => 'bird' + ) + ), + 'By username' => array( + 'u' => array( + 'name' => 'Username', + 'type' => 'text', + 'required' => true, + 'title' => 'Insert username (as shown in the address bar)', + 'exampleValue' => 'flickr' + ) + ), + ); + + public function collectData(){ + switch($this->queriedContext){ + case 'Explore': + $key = 'photos'; + $html = getSimpleHTMLDOM(self::URI . 'explore') + or returnServerError('Could not request Flickr.'); + break; + case 'By keyword': + $key = 'photos'; + $html = getSimpleHTMLDOM(self::URI . 'search/?q=' . urlencode($this->getInput('q')) . '&s=rec') + or returnServerError('No results for this query.'); + break; + case 'By username': + $key = 'photoPageList'; + $html = getSimpleHTMLDOM(self::URI . 'photos/' . urlencode($this->getInput('u'))) + or returnServerError('Requested username can\'t be found.'); + break; + default: + returnClientError('Invalid context: ' . $this->queriedContext); + } + + // 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'] = '' + . '
' + . '

' + . $description + . '

'; + + $this->items[] = $item; + + break; + } + } + } + } +} From 37b5df89855e9d10d0655d12e54e631aae80026b Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 11 Feb 2017 12:37:07 +0100 Subject: [PATCH 4/4] Remove FlickrExploreBridge and FlickrTagBridge These bridges are replaced by the FlickrBridge --- bridges/FlickrExploreBridge.php | 68 -------------------- bridges/FlickrTagBridge.php | 106 -------------------------------- 2 files changed, 174 deletions(-) delete mode 100644 bridges/FlickrExploreBridge.php delete mode 100644 bridges/FlickrTagBridge.php diff --git a/bridges/FlickrExploreBridge.php b/bridges/FlickrExploreBridge.php deleted file mode 100644 index 53226592..00000000 --- a/bridges/FlickrExploreBridge.php +++ /dev/null @@ -1,68 +0,0 @@ -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]['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'] = '' - . '
' - . '

' - . $description - . '

'; - - $this->items[] = $item; - - break; - } - } - } - } -} diff --git a/bridges/FlickrTagBridge.php b/bridges/FlickrTagBridge.php deleted file mode 100644 index 91d6b809..00000000 --- a/bridges/FlickrTagBridge.php +++ /dev/null @@ -1,106 +0,0 @@ - array( - 'q'=>array( - 'name'=>'keyword', - 'required'=>true - ) - ), - - 'By username' => array( - 'u'=>array( - 'name'=>'Username', - 'required'=>true - ) - ), - ); - - public function collectData(){ - switch($this->queriedContext){ - case 'By keyword': - $key = 'photos'; - $html = getSimpleHTMLDOM(self::URI . 'search/?q=' . urlencode($this->getInput('q')) . '&s=rec') - or returnServerError('No results for this query.'); - break; - case 'By username': - $key = 'photoPageList'; - $html = getSimpleHTMLDOM(self::URI . 'photos/' . urlencode($this->getInput('u'))) - or returnServerError('Requested username can\'t be found.'); - break; - default: - returnClientError('Invalid context: ' . $this->queriedContext); - } - - // 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'] = '' - . '
' - . '

' - . $description - . '

'; - - $this->items[] = $item; - - break; - } - } - } - } -}