[RedditBridge] Added time interval and filter for min comment count (#4471)

* Reddit Bridge - added filter for min comment count and time interval.

* [RedditBridge] Add sort by comment count

* lint

* consistent commas

---------

Co-authored-by: Dag <me@dvikan.no>
This commit is contained in:
chibicitiberiu 2025-03-23 21:45:35 +02:00 committed by GitHub
parent 87ab1e4513
commit 7260f28e10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 14 deletions

View File

@ -20,7 +20,15 @@ class RedditBridge extends BridgeAbstract
'required' => false, 'required' => false,
'type' => 'number', 'type' => 'number',
'exampleValue' => 100, 'exampleValue' => 100,
'title' => 'Filter out posts with lower score' 'title' => 'Filter out posts with lower score. Set to -1 to disable. If both score and comments are set, an OR is applied.',
],
'min_comments' => [
'name' => 'Minimal number of comments',
'required' => false,
'type' => 'number',
'exampleValue' => 100,
'title' => 'Filter out posts with lower number of comments. Set to -1 to disable. If both score and comments are set, an OR is applied.',
'defaultValue' => -1
], ],
'd' => [ 'd' => [
'name' => 'Sort By', 'name' => 'Sort By',
@ -30,10 +38,25 @@ class RedditBridge extends BridgeAbstract
'Hot' => 'hot', 'Hot' => 'hot',
'Relevance' => 'relevance', 'Relevance' => 'relevance',
'New' => 'new', 'New' => 'new',
'Top' => 'top' 'Top' => 'top',
'Comments' => 'comments',
], ],
'defaultValue' => 'Hot' 'defaultValue' => 'Hot'
], ],
't' => [
'name' => 'Time',
'type' => 'list',
'title' => 'Sort by new, hot, top or relevancy',
'values' => [
'All' => 'all',
'Year' => 'year',
'Month' => 'month',
'Week' => 'week',
'Day' => 'day',
'Hour' => 'hour',
],
'defaultValue' => 'week'
],
'search' => [ 'search' => [
'name' => 'Keyword search', 'name' => 'Keyword search',
'required' => false, 'required' => false,
@ -126,6 +149,7 @@ class RedditBridge extends BridgeAbstract
$frontend = 'https://old.reddit.com'; $frontend = 'https://old.reddit.com';
} }
$section = $this->getInput('d'); $section = $this->getInput('d');
$time = $this->getInput('t');
switch ($this->queriedContext) { switch ($this->queriedContext) {
case 'single': case 'single':
@ -147,7 +171,7 @@ class RedditBridge extends BridgeAbstract
foreach ($subreddits as $subreddit) { foreach ($subreddits as $subreddit) {
$version = 'v0.0.2'; $version = 'v0.0.2';
$useragent = "rss-bridge $version (https://github.com/RSS-Bridge/rss-bridge)"; $useragent = "rss-bridge $version (https://github.com/RSS-Bridge/rss-bridge)";
$url = self::createUrl($search, $flareInput, $subreddit, $user, $section, $this->queriedContext); $url = self::createUrl($search, $flareInput, $subreddit, $user, $section, $time, $this->queriedContext);
$response = getContents($url, ['User-Agent: ' . $useragent], [], true); $response = getContents($url, ['User-Agent: ' . $useragent], [], true);
@ -162,8 +186,20 @@ class RedditBridge extends BridgeAbstract
$data = $post->data; $data = $post->data;
if ($data->score < $this->getInput('score')) { $min_score = $this->getInput('score');
continue; $min_comments = $this->getInput('min_comments');
if ($min_score >= 0 && $min_comments >= 0) {
if ($data->num_comments < $min_comments || $data->score < $min_score) {
continue;
}
} elseif ($min_score >= 0) {
if ($data->score < $min_score) {
continue;
}
} elseif ($min_comments >= 0) {
if ($data->num_comments < $min_comments) {
continue;
}
} }
$item = []; $item = [];
@ -264,7 +300,7 @@ class RedditBridge extends BridgeAbstract
}); });
} }
public static function createUrl($search, $flareInput, $subreddit, bool $user, $section, $queriedContext): string public static function createUrl($search, $flareInput, $subreddit, bool $user, $section, $time, $queriedContext): string
{ {
if ($search === '') { if ($search === '') {
$keywords = ''; $keywords = '';
@ -286,6 +322,7 @@ class RedditBridge extends BridgeAbstract
'q' => $keywords . $flair . ($user ? 'author:' : 'subreddit:') . $name, 'q' => $keywords . $flair . ($user ? 'author:' : 'subreddit:') . $name,
'sort' => $section, 'sort' => $section,
'include_over_18' => 'on', 'include_over_18' => 'on',
't' => $time
]; ];
return 'https://old.reddit.com/search.json?' . http_build_query($query); return 'https://old.reddit.com/search.json?' . http_build_query($query);
} }

View File

@ -11,23 +11,23 @@ class RedditBridgeTest extends TestCase
$sut = new RedditBridge(new NullCache(), new NullLogger()); $sut = new RedditBridge(new NullCache(), new NullLogger());
// https://old.reddit.com/search.json?q=cats dogs hen subreddit:php&sort=hot&include_over_18=on // https://old.reddit.com/search.json?q=cats dogs hen subreddit:php&sort=hot&include_over_18=on
$expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+subreddit%3Aphp&sort=hot&include_over_18=on'; $expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+subreddit%3Aphp&sort=hot&include_over_18=on&t=all';
$actual = RedditBridge::createUrl('cats,dogs hen', '', 'php', false, 'hot', 'single'); $actual = RedditBridge::createUrl('cats,dogs hen', '', 'php', false, 'hot', 'all', 'single');
$this->assertSame($expected, $actual); $this->assertSame($expected, $actual);
// https://old.reddit.com/search.json?q=author:RavenousRandy&sort=hot&include_over_18=on // https://old.reddit.com/search.json?q=author:RavenousRandy&sort=hot&include_over_18=on
$expected = 'https://old.reddit.com/search.json?q=author%3ARavenousRandy&sort=hot&include_over_18=on'; $expected = 'https://old.reddit.com/search.json?q=author%3ARavenousRandy&sort=hot&include_over_18=on&t=week';
$actual = RedditBridge::createUrl('', '', 'RavenousRandy', true, 'hot', 'user'); $actual = RedditBridge::createUrl('', '', 'RavenousRandy', true, 'hot', 'week', 'user');
$this->assertSame($expected, $actual); $this->assertSame($expected, $actual);
// https://old.reddit.com/search.json?q=cats dogs hen flair:"Proxy" subreddit:php&sort=hot&include_over_18=on // https://old.reddit.com/search.json?q=cats dogs hen flair:"Proxy" subreddit:php&sort=hot&include_over_18=on
$expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+flair%3A%22Proxy%22+subreddit%3Aphp&sort=hot&include_over_18=on'; $expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+flair%3A%22Proxy%22+subreddit%3Aphp&sort=hot&include_over_18=on&t=month';
$actual = RedditBridge::createUrl('cats,dogs hen', 'Proxy', 'php', false, 'hot', 'single'); $actual = RedditBridge::createUrl('cats,dogs hen', 'Proxy', 'php', false, 'hot', 'month', 'single');
$this->assertSame($expected, $actual); $this->assertSame($expected, $actual);
// https://old.reddit.com/search.json?q=cats dogs hen flair:"Proxy Linux Server" subreddit:php&sort=hot&include_over_18=on // https://old.reddit.com/search.json?q=cats dogs hen flair:"Proxy Linux Server" subreddit:php&sort=hot&include_over_18=on
$expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+flair%3A%22Proxy+Linux+Server%22+subreddit%3Aphp&sort=hot&include_over_18=on'; $expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+flair%3A%22Proxy+Linux+Server%22+subreddit%3Aphp&sort=hot&include_over_18=on&t=day';
$actual = RedditBridge::createUrl('cats,dogs hen', 'Proxy,Linux Server', 'php', false, 'hot', 'single'); $actual = RedditBridge::createUrl('cats,dogs hen', 'Proxy,Linux Server', 'php', false, 'hot', 'day', 'single');
$this->assertSame($expected, $actual); $this->assertSame($expected, $actual);
} }
} }