[RedditBridge] Add keyword search function (#2229)

This commit is contained in:
Bockiii 2021-08-25 15:09:36 +02:00 committed by GitHub
parent 8f634eb4a1
commit a5d33615f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,7 @@ class RedditBridge extends BridgeAbstract {
const MAINTAINER = 'dawidsowa'; const MAINTAINER = 'dawidsowa';
const NAME = 'Reddit Bridge'; const NAME = 'Reddit Bridge';
const URI = 'https://www.reddit.com'; const URI = 'https://www.reddit.com';
const DESCRIPTION = 'Return hot, new and top submissions from Reddit'; const DESCRIPTION = 'Return hot submissions from Reddit';
const PARAMETERS = array( const PARAMETERS = array(
'global' => array( 'global' => array(
@ -17,14 +17,22 @@ class RedditBridge extends BridgeAbstract {
'title' => 'Filter out posts with lower score' 'title' => 'Filter out posts with lower score'
), ),
'd' => array( 'd' => array(
'name' => 'Section', 'name' => 'Sort By',
'type' => 'list', 'type' => 'list',
'title' => 'Choose whether to have new, hot and top submissions', 'title' => 'Sort by new, hot, top or relevancy',
'values' => array( 'values' => array(
'Hot' => '', // By default, Reddit displays hot submissions. 'Hot' => 'hot',
'Relevance' => 'relevance',
'New' => 'new', 'New' => 'new',
'Top' => 'top' 'Top' => 'top'
) ),
'defaultValue' => 'Hot'
),
'search' => array(
'name' => 'Keyword search',
'required' => false,
'exampleValue' => 'cats, dogs',
'title' => 'Keyword search, separated by commas'
) )
), ),
'single' => array( 'single' => array(
@ -112,11 +120,23 @@ class RedditBridge extends BridgeAbstract {
break; break;
} }
if(!($this->getInput('search') === '')) {
$keywords = $this->getInput('search');
$keywords = str_replace(array(',', ' '), '%20', $keywords);
$keywords = $keywords . '%20';
} else {
$keywords = '';
}
foreach ($subreddits as $subreddit) { foreach ($subreddits as $subreddit) {
$name = trim($subreddit); $name = trim($subreddit);
$values = getContents(self::URI . ($user ? '/user/' : '/r/') $values = getContents(self::URI
. $name . ((!$user && $section != '') ? "/$section" : '') . '.json' . '/search.json?q='
. (($user && $section != '') ? "?sort=$section" : '')) . $keywords
. ($user ? 'author%3A' : 'subreddit%3A')
. $name
. '&sort='
. $this->getInput('d'))
or returnServerError('Unable to fetch posts!'); or returnServerError('Unable to fetch posts!');
$decodedValues = json_decode($values); $decodedValues = json_decode($values);
@ -243,6 +263,10 @@ class RedditBridge extends BridgeAbstract {
$this->items[] = $item; $this->items[] = $item;
} }
} }
// Sort the order to put the latest posts first, even for mixed subreddits
usort($this->items, function($a, $b) {
return $a['timestamp'] < $b['timestamp'];
});
} }
private function encodePermalink($link) { private function encodePermalink($link) {