From adff8d86cf26081cbc0ca6f989d9fb7710618b6e Mon Sep 17 00:00:00 2001 From: Henning Bocklage Date: Fri, 30 Jul 2021 21:00:56 +0200 Subject: [PATCH 1/4] [Reddit] Add keyword search function --- bridges/RedditBridge.php | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/bridges/RedditBridge.php b/bridges/RedditBridge.php index 9c19e2a190e..3af4fb4389c 100644 --- a/bridges/RedditBridge.php +++ b/bridges/RedditBridge.php @@ -5,7 +5,7 @@ class RedditBridge extends BridgeAbstract { const MAINTAINER = 'dawidsowa'; const NAME = 'Reddit Bridge'; 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( 'global' => array( @@ -17,14 +17,22 @@ class RedditBridge extends BridgeAbstract { 'title' => 'Filter out posts with lower score' ), 'd' => array( - 'name' => 'Section', + 'name' => 'Sort By', 'type' => 'list', - 'title' => 'Choose whether to have new, hot and top submissions', + 'title' => 'Sort by new, hot, top or relevancy', 'values' => array( - 'Hot' => '', // By default, Reddit displays hot submissions. + 'Hot' => 'hot', + 'Relevance' => 'relevance', 'New' => 'new', 'Top' => 'top' - ) + ), + 'defaultValue' => 'Hot' + ), + 'search' => array( + 'name' => 'Keyword search', + 'required' => false, + 'exampleValue' => 'cats, dogs', + 'title' => 'Keyword search, separated by commas' ) ), 'single' => array( @@ -112,11 +120,23 @@ public function collectData() { break; } + if(!($this->getInput('search') === '')) { + $keywords = $this->getInput('search'); + $keywords = str_replace(array(',', ' '), '%20', $keywords); + $keywords = $keywords . '%20'; + } else { + $keywords = ''; + } + foreach ($subreddits as $subreddit) { $name = trim($subreddit); - $values = getContents(self::URI . ($user ? '/user/' : '/r/') - . $name . ((!$user && $section != '') ? "/$section" : '') . '.json' - . (($user && $section != '') ? "?sort=$section" : '')) + $values = getContents(self::URI + . '/search.json?q=' + . $keywords + . ($user ? 'author%3A' : 'subreddit%3A') + . $name + . '&sort=' + . $this->getInput('d')) or returnServerError('Unable to fetch posts!'); $decodedValues = json_decode($values); @@ -243,6 +263,10 @@ public function collectData() { $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) { From 8b5c752d7e49e79d68c64a26da136286910bc18c Mon Sep 17 00:00:00 2001 From: Henning Bocklage Date: Fri, 30 Jul 2021 21:04:49 +0200 Subject: [PATCH 2/4] linting --- bridges/RedditBridge.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bridges/RedditBridge.php b/bridges/RedditBridge.php index 3af4fb4389c..8d98d6e296e 100644 --- a/bridges/RedditBridge.php +++ b/bridges/RedditBridge.php @@ -130,12 +130,12 @@ public function collectData() { foreach ($subreddits as $subreddit) { $name = trim($subreddit); - $values = getContents(self::URI - . '/search.json?q=' - . $keywords - . ($user ? 'author%3A' : 'subreddit%3A') - . $name - . '&sort=' + $values = getContents(self::URI + . '/search.json?q=' + . $keywords + . ($user ? 'author%3A' : 'subreddit%3A') + . $name + . '&sort= . $this->getInput('d')) or returnServerError('Unable to fetch posts!'); $decodedValues = json_decode($values); From efb737aa16d2bed8df20c12a9914e9a1dba52e62 Mon Sep 17 00:00:00 2001 From: Henning Bocklage Date: Fri, 30 Jul 2021 21:06:53 +0200 Subject: [PATCH 3/4] wops... one delete too much --- bridges/RedditBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/RedditBridge.php b/bridges/RedditBridge.php index 8d98d6e296e..5c364e60fc5 100644 --- a/bridges/RedditBridge.php +++ b/bridges/RedditBridge.php @@ -135,7 +135,7 @@ public function collectData() { . $keywords . ($user ? 'author%3A' : 'subreddit%3A') . $name - . '&sort= + . '&sort=' . $this->getInput('d')) or returnServerError('Unable to fetch posts!'); $decodedValues = json_decode($values); From 0b58d1cf37f04c8a3574e71434199e792cf836ea Mon Sep 17 00:00:00 2001 From: Henning Bocklage Date: Fri, 30 Jul 2021 21:08:38 +0200 Subject: [PATCH 4/4] one day I wont have linting errors. --- bridges/RedditBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/RedditBridge.php b/bridges/RedditBridge.php index 5c364e60fc5..fa55997a8fc 100644 --- a/bridges/RedditBridge.php +++ b/bridges/RedditBridge.php @@ -127,7 +127,7 @@ public function collectData() { } else { $keywords = ''; } - + foreach ($subreddits as $subreddit) { $name = trim($subreddit); $values = getContents(self::URI