mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-02-16 15:19:55 +03:00
fix(reddit): url encoding (#4010)
This commit is contained in:
parent
5b80af978f
commit
4bad1c140a
3 changed files with 62 additions and 49 deletions
|
@ -139,36 +139,13 @@ class RedditBridge extends BridgeAbstract
|
|||
break;
|
||||
}
|
||||
|
||||
if (!($this->getInput('search') === '')) {
|
||||
$keywords = $this->getInput('search');
|
||||
$keywords = str_replace([',', ' '], '%20', $keywords);
|
||||
$keywords = $keywords . '%20';
|
||||
} else {
|
||||
$keywords = '';
|
||||
}
|
||||
|
||||
if (!empty($this->getInput('f')) && $this->queriedContext == 'single') {
|
||||
$flair = $this->getInput('f');
|
||||
$flair = str_replace(' ', '%20', $flair);
|
||||
$flair = 'flair%3A%22' . $flair . '%22%20';
|
||||
} else {
|
||||
$flair = '';
|
||||
}
|
||||
$search = $this->getInput('search');
|
||||
$flareInput = $this->getInput('f');
|
||||
|
||||
foreach ($subreddits as $subreddit) {
|
||||
$name = trim($subreddit);
|
||||
$url = self::URI
|
||||
. '/search.json?q='
|
||||
. $keywords
|
||||
. $flair
|
||||
. ($user ? 'author%3A' : 'subreddit%3A')
|
||||
. $name
|
||||
. '&sort='
|
||||
. $this->getInput('d')
|
||||
. '&include_over_18=on';
|
||||
|
||||
$version = 'v0.0.1';
|
||||
$useragent = "rss-bridge $version (https://github.com/RSS-Bridge/rss-bridge)";
|
||||
$url = self::createUrl($search, $flareInput, $subreddit, $user, $section, $this->queriedContext);
|
||||
$json = getContents($url, ['User-Agent: ' . $useragent]);
|
||||
$parsedJson = Json::decode($json, false);
|
||||
|
||||
|
@ -278,6 +255,32 @@ class RedditBridge extends BridgeAbstract
|
|||
});
|
||||
}
|
||||
|
||||
public static function createUrl($search, $flareInput, $subreddit, bool $user, $section, $queriedContext): string
|
||||
{
|
||||
if ($search === '') {
|
||||
$keywords = '';
|
||||
} else {
|
||||
$keywords = $search;
|
||||
$keywords = str_replace([',', ' '], ' ', $keywords);
|
||||
$keywords = $keywords . ' ';
|
||||
}
|
||||
|
||||
if ($flareInput && $queriedContext == 'single') {
|
||||
$flair = $flareInput;
|
||||
$flair = str_replace([',', ' '], ' ', $flair);
|
||||
$flair = 'flair:"' . $flair . '" ';
|
||||
} else {
|
||||
$flair = '';
|
||||
}
|
||||
$name = trim($subreddit);
|
||||
$query = [
|
||||
'q' => $keywords . $flair . ($user ? 'author:' : 'subreddit:') . $name,
|
||||
'sort' => $section,
|
||||
'include_over_18' => 'on',
|
||||
];
|
||||
return 'https://old.reddit.com/search.json?' . http_build_query($query);
|
||||
}
|
||||
|
||||
public function getIcon()
|
||||
{
|
||||
return 'https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png';
|
||||
|
|
|
@ -157,29 +157,6 @@ class BridgeImplementationTest extends TestCase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataBridgesProvider
|
||||
*/
|
||||
public function testVisibleMethods($path)
|
||||
{
|
||||
$bridgeAbstractMethods = get_class_methods(BridgeAbstract::class);
|
||||
sort($bridgeAbstractMethods);
|
||||
$feedExpanderMethods = get_class_methods(FeedExpander::class);
|
||||
sort($feedExpanderMethods);
|
||||
|
||||
$this->setBridge($path);
|
||||
|
||||
$publicMethods = get_class_methods($this->bridge);
|
||||
sort($publicMethods);
|
||||
foreach ($publicMethods as $publicMethod) {
|
||||
if ($this->bridge instanceof FeedExpander) {
|
||||
$this->assertContains($publicMethod, $feedExpanderMethods);
|
||||
} else {
|
||||
$this->assertContains($publicMethod, $bridgeAbstractMethods);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataBridgesProvider
|
||||
*/
|
||||
|
|
33
tests/RedditBridgeTest.php
Normal file
33
tests/RedditBridgeTest.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RedditBridgeTest extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$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
|
||||
$expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+subreddit%3Aphp&sort=hot&include_over_18=on';
|
||||
$actual = RedditBridge::createUrl('cats,dogs hen', '', 'php', false, 'hot', 'single');
|
||||
$this->assertSame($expected, $actual);
|
||||
|
||||
// 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';
|
||||
$actual = RedditBridge::createUrl('', '', 'RavenousRandy', true, 'hot', 'user');
|
||||
$this->assertSame($expected, $actual);
|
||||
|
||||
// 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';
|
||||
$actual = RedditBridge::createUrl('cats,dogs hen', 'Proxy', 'php', false, 'hot', 'single');
|
||||
$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
|
||||
$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';
|
||||
$actual = RedditBridge::createUrl('cats,dogs hen', 'Proxy,Linux Server', 'php', false, 'hot', 'single');
|
||||
$this->assertSame($expected, $actual);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue