Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FlickrBridge] Add filter by media and sort by options #1758

Merged
merged 10 commits into from
Nov 16, 2020
110 changes: 100 additions & 10 deletions bridges/FlickrBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ class FlickrBridge extends BridgeAbstract {
'required' => true,
'title' => 'Insert keyword',
'exampleValue' => 'bird'
),
'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',
)
),
'By username' => array(
Expand All @@ -29,30 +50,60 @@ class FlickrBridge extends BridgeAbstract {
'required' => true,
'title' => 'Insert username (as shown in the address bar)',
'exampleValue' => 'flickr'
),
'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',
)
)
);

public function collectData(){
private $username = '';

public function collectData() {

switch($this->queriedContext) {

case 'Explore':
$filter = 'photo-lite-models';
$html = getSimpleHTMLDOM(self::URI . 'explore')
$html = getSimpleHTMLDOM($this->getURI())
or returnServerError('Could not request Flickr.');
break;

case 'By keyword':
$filter = 'photo-lite-models';
$html = getSimpleHTMLDOM(self::URI . 'search/?q=' . urlencode($this->getInput('q')) . '&s=rec')
$html = getSimpleHTMLDOM($this->getURI())
or returnServerError('No results for this query.');
break;

case 'By username':
$filter = 'photo-models';
$html = getSimpleHTMLDOM(self::URI . 'photos/' . urlencode($this->getInput('u')))
//$filter = 'photo-models';
$filter = 'photo-lite-models';
$html = getSimpleHTMLDOM($this->getURI())
or returnServerError('Requested username can\'t be found.');

$this->username = $this->getInput('u');

if ($html->find('span.search-pill-name', 0)) {
$this->username = $html->find('span.search-pill-name', 0)->plaintext;
}
break;

default:
Expand All @@ -64,20 +115,19 @@ public function collectData(){
$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)) {
$item['author'] = $model['username'];
$item['author'] = urldecode($model['username']);
} elseif (array_key_exists('owner', reset($model_json)[0])) {
$item['author'] = reset($model_json)[0]['owner']['username'];
$item['author'] = urldecode(reset($model_json)[0]['owner']['username']);
}

$item['title'] = (array_key_exists('title', $model) ? $model['title'] : 'Untitled');
$item['title'] = urldecode((array_key_exists('title', $model) ? $model['title'] : 'Untitled'));
$item['uri'] = self::URI . 'photo.gne?id=' . $model['id'];

$description = (array_key_exists('description', $model) ? $model['description'] : '');
Expand All @@ -87,7 +137,7 @@ public function collectData(){
. '"><img src="'
. $this->extractContentImage($model)
. '" style="max-width: 640px; max-height: 480px;"/></a><br><p>'
. $description
. urldecode($description)
. '</p>';

$item['enclosures'] = $this->extractEnclosures($model);
Expand All @@ -98,6 +148,46 @@ public function collectData(){

}

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();
}

private function extractJsonModel($html) {

// Find SCRIPT containing JSON data
Expand Down