fix(reddit): set custom http ua to fix 429 errors (#3552)

* refactor

* refactor

* fix(reddit): set custom http ua to fix 429 errors

* lint
This commit is contained in:
Dag 2023-07-19 06:39:17 +02:00 committed by GitHub
parent f91723d9e5
commit a4a328583a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -73,47 +73,6 @@ class RedditBridge extends BridgeAbstract
]
];
public function detectParameters($url)
{
$parsed_url = parse_url($url);
$host = $parsed_url['host'] ?? null;
if ($host != 'www.reddit.com' && $host != 'old.reddit.com') {
return null;
}
$path = explode('/', $parsed_url['path']);
if ($path[1] == 'r') {
return [
'r' => $path[2]
];
} elseif ($path[1] == 'user') {
return [
'u' => $path[2]
];
} else {
return null;
}
}
public function getIcon()
{
return 'https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png';
}
public function getName()
{
if ($this->queriedContext == 'single') {
return 'Reddit r/' . $this->getInput('r');
} elseif ($this->queriedContext == 'user') {
return 'Reddit u/' . $this->getInput('u');
} else {
return self::NAME;
}
}
public function collectData()
{
$user = false;
@ -152,7 +111,7 @@ class RedditBridge extends BridgeAbstract
foreach ($subreddits as $subreddit) {
$name = trim($subreddit);
$values = getContents(self::URI
$url = self::URI
. '/search.json?q='
. $keywords
. $flair
@ -160,10 +119,14 @@ class RedditBridge extends BridgeAbstract
. $name
. '&sort='
. $this->getInput('d')
. '&include_over_18=on');
$decodedValues = json_decode($values);
. '&include_over_18=on';
foreach ($decodedValues->data->children as $post) {
$version = 'v0.0.1';
$useragent = "rss-bridge $version (https://github.com/RSS-Bridge/rss-bridge)";
$json = getContents($url, ['User-Agent: ' . $useragent]);
$parsedJson = Json::decode($json, false);
foreach ($parsedJson->data->children as $post) {
if ($post->kind == 't1' && !$comments) {
continue;
}
@ -288,6 +251,22 @@ class RedditBridge extends BridgeAbstract
});
}
public function getIcon()
{
return 'https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png';
}
public function getName()
{
if ($this->queriedContext == 'single') {
return 'Reddit r/' . $this->getInput('r');
} elseif ($this->queriedContext == 'user') {
return 'Reddit u/' . $this->getInput('u');
} else {
return self::NAME;
}
}
private function encodePermalink($link)
{
return self::URI . implode(
@ -307,4 +286,29 @@ class RedditBridge extends BridgeAbstract
{
return '<a href="' . $href . '">' . $text . '</a>';
}
public function detectParameters($url)
{
$parsed_url = parse_url($url);
$host = $parsed_url['host'] ?? null;
if ($host != 'www.reddit.com' && $host != 'old.reddit.com') {
return null;
}
$path = explode('/', $parsed_url['path']);
if ($path[1] == 'r') {
return [
'r' => $path[2]
];
} elseif ($path[1] == 'user') {
return [
'u' => $path[2]
];
} else {
return null;
}
}
}