2019-11-01 15:54:03 +03:00
|
|
|
<?php
|
|
|
|
|
2023-12-19 09:53:25 +03:00
|
|
|
/**
|
|
|
|
* This bridge does NOT use reddit's official rss feeds.
|
|
|
|
*
|
|
|
|
* This bridge uses reddit's json api: https://old.reddit.com/search.json?q=
|
|
|
|
*/
|
2020-12-23 20:42:15 +03:00
|
|
|
class RedditBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const MAINTAINER = 'dawidsowa';
|
2019-11-01 15:54:03 +03:00
|
|
|
const NAME = 'Reddit Bridge';
|
2023-12-19 09:53:25 +03:00
|
|
|
const URI = 'https://old.reddit.com';
|
2021-08-25 16:09:36 +03:00
|
|
|
const DESCRIPTION = 'Return hot submissions from Reddit';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 15:54:03 +03:00
|
|
|
const PARAMETERS = [
|
2021-04-19 20:14:35 +03:00
|
|
|
'global' => [
|
|
|
|
'score' => [
|
|
|
|
'name' => 'Minimal score',
|
|
|
|
'required' => false,
|
|
|
|
'type' => 'number',
|
|
|
|
'exampleValue' => 100,
|
|
|
|
'title' => 'Filter out posts with lower score'
|
2021-07-01 22:41:56 +03:00
|
|
|
],
|
|
|
|
'd' => [
|
2021-08-25 16:09:36 +03:00
|
|
|
'name' => 'Sort By',
|
2021-07-01 22:41:56 +03:00
|
|
|
'type' => 'list',
|
2021-08-25 16:09:36 +03:00
|
|
|
'title' => 'Sort by new, hot, top or relevancy',
|
2021-07-01 22:41:56 +03:00
|
|
|
'values' => [
|
2021-08-25 16:09:36 +03:00
|
|
|
'Hot' => 'hot',
|
|
|
|
'Relevance' => 'relevance',
|
2021-07-01 22:41:56 +03:00
|
|
|
'New' => 'new',
|
|
|
|
'Top' => 'top'
|
2021-08-25 16:09:36 +03:00
|
|
|
],
|
|
|
|
'defaultValue' => 'Hot'
|
|
|
|
],
|
|
|
|
'search' => [
|
|
|
|
'name' => 'Keyword search',
|
|
|
|
'required' => false,
|
|
|
|
'exampleValue' => 'cats, dogs',
|
|
|
|
'title' => 'Keyword search, separated by commas'
|
2024-02-08 22:05:24 +03:00
|
|
|
],
|
|
|
|
'frontend' => [
|
|
|
|
'type' => 'list',
|
|
|
|
'name' => 'frontend',
|
|
|
|
'title' => 'choose frontend for reddit',
|
|
|
|
'values' => [
|
|
|
|
'old.reddit.com' => 'https://old.reddit.com',
|
|
|
|
'reddit.com' => 'https://reddit.com',
|
|
|
|
'libreddit.kavin.rocks' => 'https://libreddit.kavin.rocks',
|
|
|
|
]
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2021-04-19 20:14:35 +03:00
|
|
|
],
|
2019-11-01 15:54:03 +03:00
|
|
|
'single' => [
|
|
|
|
'r' => [
|
|
|
|
'name' => 'SubReddit',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'selfhosted',
|
|
|
|
'title' => 'SubReddit name'
|
2022-10-02 08:34:20 +03:00
|
|
|
],
|
|
|
|
'f' => [
|
|
|
|
'name' => 'Flair',
|
|
|
|
'required' => false,
|
|
|
|
'exampleValue' => 'Proxy',
|
|
|
|
'title' => 'Flair filter'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2019-11-01 15:54:03 +03:00
|
|
|
],
|
|
|
|
'multi' => [
|
|
|
|
'rs' => [
|
|
|
|
'name' => 'SubReddits',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'selfhosted, php',
|
|
|
|
'title' => 'SubReddit names, separated by commas'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2021-02-23 10:08:43 +03:00
|
|
|
],
|
|
|
|
'user' => [
|
|
|
|
'u' => [
|
|
|
|
'name' => 'User',
|
|
|
|
'required' => true,
|
2022-03-24 13:59:34 +03:00
|
|
|
'exampleValue' => 'shwikibot',
|
2021-02-23 10:08:43 +03:00
|
|
|
'title' => 'User name'
|
|
|
|
],
|
|
|
|
'comments' => [
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'name' => 'Comments',
|
|
|
|
'title' => 'Whether to return comments',
|
|
|
|
'defaultValue' => false
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
|
|
|
]
|
2019-11-01 15:54:03 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
public function collectData()
|
2023-09-10 22:50:15 +03:00
|
|
|
{
|
2023-12-14 00:06:47 +03:00
|
|
|
$forbiddenKey = 'reddit_forbidden';
|
|
|
|
if ($this->cache->get($forbiddenKey)) {
|
|
|
|
throw new HttpException('403 Forbidden', 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
$rateLimitKey = 'reddit_rate_limit';
|
|
|
|
if ($this->cache->get($rateLimitKey)) {
|
2023-09-10 22:50:15 +03:00
|
|
|
throw new HttpException('429 Too Many Requests', 429);
|
|
|
|
}
|
2023-12-14 00:06:47 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
try {
|
|
|
|
$this->collectDataInternal();
|
|
|
|
} catch (HttpException $e) {
|
2023-12-13 23:56:14 +03:00
|
|
|
if ($e->getCode() === 403) {
|
|
|
|
// 403 Forbidden
|
|
|
|
// This can possibly mean that reddit has permanently blocked this server's ip address
|
2023-12-14 00:06:47 +03:00
|
|
|
$this->cache->set($forbiddenKey, true, 60 * 61);
|
|
|
|
}
|
|
|
|
if ($e->getCode() === 429) {
|
|
|
|
$this->cache->set($rateLimitKey, true, 60 * 16);
|
2023-12-13 23:56:14 +03:00
|
|
|
}
|
2023-09-14 04:26:01 +03:00
|
|
|
throw $e;
|
2023-09-10 22:50:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function collectDataInternal(): void
|
2020-12-23 20:42:15 +03:00
|
|
|
{
|
2021-02-23 10:08:43 +03:00
|
|
|
$user = false;
|
|
|
|
$comments = false;
|
2024-02-08 22:05:24 +03:00
|
|
|
$frontend = $this->getInput('frontend');
|
|
|
|
if ($frontend == '') {
|
2024-03-31 22:32:27 +03:00
|
|
|
$frontend = 'https://old.reddit.com';
|
2024-02-08 22:05:24 +03:00
|
|
|
}
|
2021-07-01 22:41:56 +03:00
|
|
|
$section = $this->getInput('d');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case 'single':
|
|
|
|
$subreddits[] = $this->getInput('r');
|
|
|
|
break;
|
|
|
|
case 'multi':
|
|
|
|
$subreddits = explode(',', $this->getInput('rs'));
|
|
|
|
break;
|
2021-02-23 10:08:43 +03:00
|
|
|
case 'user':
|
|
|
|
$subreddits[] = $this->getInput('u');
|
|
|
|
$user = true;
|
|
|
|
$comments = $this->getInput('comments');
|
|
|
|
break;
|
2020-12-23 20:42:15 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-03-13 01:59:10 +03:00
|
|
|
$search = $this->getInput('search');
|
|
|
|
$flareInput = $this->getInput('f');
|
2022-10-02 08:34:20 +03:00
|
|
|
|
2019-11-01 15:54:03 +03:00
|
|
|
foreach ($subreddits as $subreddit) {
|
2023-07-19 07:39:17 +03:00
|
|
|
$version = 'v0.0.1';
|
|
|
|
$useragent = "rss-bridge $version (https://github.com/RSS-Bridge/rss-bridge)";
|
2024-03-13 01:59:10 +03:00
|
|
|
$url = self::createUrl($search, $flareInput, $subreddit, $user, $section, $this->queriedContext);
|
2023-07-19 07:39:17 +03:00
|
|
|
$json = getContents($url, ['User-Agent: ' . $useragent]);
|
|
|
|
$parsedJson = Json::decode($json, false);
|
|
|
|
|
|
|
|
foreach ($parsedJson->data->children as $post) {
|
2021-02-23 10:08:43 +03:00
|
|
|
if ($post->kind == 't1' && !$comments) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
$data = $post->data;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-04-19 20:14:35 +03:00
|
|
|
if ($data->score < $this->getInput('score')) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
$item = [];
|
|
|
|
$item['author'] = $data->author;
|
|
|
|
$item['uid'] = $data->id;
|
|
|
|
$item['timestamp'] = $data->created_utc;
|
2023-12-30 03:33:31 +03:00
|
|
|
$item['uri'] = $this->urlEncodePathParts($data->permalink);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-02-08 22:05:24 +03:00
|
|
|
if ($frontend != 'https://old.reddit.com') {
|
|
|
|
$item['uri'] = preg_replace('#^https://old\.reddit\.com#', $frontend, $item['uri']);
|
|
|
|
}
|
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
$item['categories'] = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-23 10:08:43 +03:00
|
|
|
if ($post->kind == 't1') {
|
|
|
|
$item['title'] = 'Comment: ' . $data->link_title;
|
|
|
|
} else {
|
|
|
|
$item['title'] = $data->title;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-23 10:08:43 +03:00
|
|
|
$item['categories'][] = $data->link_flair_text;
|
|
|
|
$item['categories'][] = $data->pinned ? 'Pinned' : null;
|
|
|
|
$item['categories'][] = $data->spoiler ? 'Spoiler' : null;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
$item['categories'][] = $data->over_18 ? 'NSFW' : null;
|
|
|
|
$item['categories'] = array_filter($item['categories']);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-23 10:08:43 +03:00
|
|
|
if ($post->kind == 't1') {
|
|
|
|
// Comment
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-30 03:33:31 +03:00
|
|
|
$item['content'] = htmlspecialchars_decode($data->body_html);
|
2024-03-17 21:02:51 +03:00
|
|
|
} elseif ($data->is_self && isset($data->selftext_html)) {
|
2020-12-23 20:42:15 +03:00
|
|
|
// Text post
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-30 03:33:31 +03:00
|
|
|
$item['content'] = htmlspecialchars_decode($data->selftext_html);
|
2022-10-26 01:47:45 +03:00
|
|
|
} elseif (isset($data->post_hint) && $data->post_hint == 'link') {
|
2020-12-23 20:42:15 +03:00
|
|
|
// Link with preview
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
if (isset($data->media)) {
|
2022-10-26 01:47:45 +03:00
|
|
|
// todo: maybe switch on the type
|
|
|
|
if (isset($data->media->oembed->html)) {
|
|
|
|
// Reddit embeds content for some sites (e.g. Twitter)
|
|
|
|
$embed = htmlspecialchars_decode($data->media->oembed->html);
|
|
|
|
} else {
|
|
|
|
$embed = '';
|
|
|
|
}
|
2020-12-23 20:42:15 +03:00
|
|
|
} else {
|
|
|
|
$embed = '';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-30 03:33:31 +03:00
|
|
|
$item['content'] = $this->createFigureLink($data->url, $data->thumbnail, $data->domain) . $embed;
|
|
|
|
} elseif (isset($data->post_hint) && $data->post_hint == 'image') {
|
2020-12-23 20:42:15 +03:00
|
|
|
// Single image
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-30 03:33:31 +03:00
|
|
|
$item['content'] = $this->createLink($this->urlEncodePathParts($data->permalink), '<img src="' . $data->url . '" />');
|
2020-12-23 20:42:15 +03:00
|
|
|
} elseif ($data->is_gallery ?? false) {
|
|
|
|
// Multiple images
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
$images = [];
|
|
|
|
foreach ($data->gallery_data->items as $media) {
|
|
|
|
$id = $media->media_id;
|
|
|
|
$type = $data->media_metadata->$id->m == 'image/gif' ? 'gif' : 'u';
|
|
|
|
$src = $data->media_metadata->$id->s->$type;
|
2021-07-01 22:41:56 +03:00
|
|
|
$images[] = '<figure><img src="' . $src . '"/></figure><br>';
|
2020-12-23 20:42:15 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
$item['content'] = implode('', $images);
|
|
|
|
} elseif ($data->is_video) {
|
|
|
|
// Video
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
// Higher index -> Higher resolution
|
|
|
|
end($data->preview->images[0]->resolutions);
|
|
|
|
$index = key($data->preview->images[0]->resolutions);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-30 03:33:31 +03:00
|
|
|
$item['content'] = $this->createFigureLink($data->url, $data->preview->images[0]->resolutions[$index]->url, 'Video');
|
|
|
|
} elseif (isset($data->media) && $data->media->type == 'youtube.com') {
|
2020-12-23 20:42:15 +03:00
|
|
|
// Youtube link
|
2023-12-30 03:33:31 +03:00
|
|
|
$item['content'] = $this->createFigureLink($data->url, $data->media->oembed->thumbnail_url, 'YouTube');
|
|
|
|
//$item['content'] = htmlspecialchars_decode($data->media->oembed->html);
|
2020-12-23 20:42:15 +03:00
|
|
|
} elseif (explode('.', $data->domain)[0] == 'self') {
|
|
|
|
// Crossposted text post
|
|
|
|
// TODO (optionally?) Fetch content of the original post.
|
2023-12-30 03:33:31 +03:00
|
|
|
$item['content'] = $this->createLink($this->urlEncodePathParts($data->permalink), 'Crossposted from r/' . explode('.', $data->domain)[1]);
|
2020-12-23 20:42:15 +03:00
|
|
|
} else {
|
|
|
|
// Link WITHOUT preview
|
2023-12-30 03:33:31 +03:00
|
|
|
$item['content'] = $this->createLink($data->url, $data->domain);
|
2020-12-23 20:42:15 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-12-23 20:42:15 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2020-12-23 20:42:15 +03:00
|
|
|
}
|
2021-08-25 16:09:36 +03:00
|
|
|
// Sort the order to put the latest posts first, even for mixed subreddits
|
|
|
|
usort($this->items, function ($a, $b) {
|
2023-12-30 03:33:31 +03:00
|
|
|
return $b['timestamp'] <=> $a['timestamp'];
|
2021-08-25 16:09:36 +03:00
|
|
|
});
|
2019-11-01 15:54:03 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-03-13 01:59:10 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-07-19 07:39:17 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 03:33:31 +03:00
|
|
|
private function urlEncodePathParts($link)
|
2020-12-23 20:42:15 +03:00
|
|
|
{
|
2023-12-30 03:33:31 +03:00
|
|
|
return self::URI . implode('/', array_map('urlencode', explode('/', $link)));
|
2020-12-23 20:42:15 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-30 03:33:31 +03:00
|
|
|
private function createFigureLink($href, $src, $caption)
|
2020-12-23 20:42:15 +03:00
|
|
|
{
|
2023-12-30 03:33:31 +03:00
|
|
|
return sprintf('<a href="%s"><figure><figcaption>%s</figcaption><img src="%s"/></figure></a>', $href, $caption, $src);
|
2020-12-23 20:42:15 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-30 03:33:31 +03:00
|
|
|
private function createLink($href, $text)
|
2020-12-23 20:42:15 +03:00
|
|
|
{
|
2023-12-30 03:33:31 +03:00
|
|
|
return sprintf('<a href="%s">%s</a>', $href, $text);
|
2020-12-23 20:42:15 +03:00
|
|
|
}
|
2023-07-19 07:39:17 +03:00
|
|
|
|
|
|
|
public function detectParameters($url)
|
|
|
|
{
|
2023-09-24 19:34:09 +03:00
|
|
|
try {
|
|
|
|
$urlObject = Url::fromString($url);
|
|
|
|
} catch (UrlException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-19 07:39:17 +03:00
|
|
|
|
2023-09-24 19:34:09 +03:00
|
|
|
$host = $urlObject->getHost();
|
|
|
|
$path = $urlObject->getPath();
|
2023-07-19 07:39:17 +03:00
|
|
|
|
2023-09-24 19:34:09 +03:00
|
|
|
$pathSegments = explode('/', $path);
|
|
|
|
|
|
|
|
if ($host !== 'www.reddit.com' && $host !== 'old.reddit.com') {
|
2023-07-19 07:39:17 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:34:09 +03:00
|
|
|
if ($pathSegments[1] == 'r') {
|
2023-07-19 07:39:17 +03:00
|
|
|
return [
|
2023-08-09 23:40:24 +03:00
|
|
|
'context' => 'single',
|
2023-09-24 19:34:09 +03:00
|
|
|
'r' => $pathSegments[2],
|
2023-07-19 07:39:17 +03:00
|
|
|
];
|
2023-09-24 19:34:09 +03:00
|
|
|
} elseif ($pathSegments[1] == 'user') {
|
2023-07-19 07:39:17 +03:00
|
|
|
return [
|
2023-08-09 23:40:24 +03:00
|
|
|
'context' => 'user',
|
2023-09-24 19:34:09 +03:00
|
|
|
'u' => $pathSegments[2],
|
2023-07-19 07:39:17 +03:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-11-01 15:54:03 +03:00
|
|
|
}
|