From 569276f4efa27b883a78f34604e4395abe4c687a Mon Sep 17 00:00:00 2001 From: dawidsowa Date: Tue, 23 Feb 2021 08:08:43 +0100 Subject: [PATCH] [RedditBridge]: Add user option (#1943) --- bridges/RedditBridge.php | 55 +++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/bridges/RedditBridge.php b/bridges/RedditBridge.php index 130dc662..1dbd8d91 100644 --- a/bridges/RedditBridge.php +++ b/bridges/RedditBridge.php @@ -23,6 +23,19 @@ class RedditBridge extends BridgeAbstract { 'exampleValue' => 'selfhosted, php', 'title' => 'SubReddit names, separated by commas' ) + ), + 'user' => array( + 'u' => array( + 'name' => 'User', + 'required' => true, + 'title' => 'User name' + ), + 'comments' => array( + 'type' => 'checkbox', + 'name' => 'Comments', + 'title' => 'Whether to return comments', + 'defaultValue' => false + ) ) ); @@ -33,12 +46,18 @@ class RedditBridge extends BridgeAbstract { 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; + $comments = false; + switch ($this->queriedContext) { case 'single': $subreddits[] = $this->getInput('r'); @@ -46,33 +65,55 @@ class RedditBridge extends BridgeAbstract { case 'multi': $subreddits = explode(',', $this->getInput('rs')); break; + case 'user': + $subreddits[] = $this->getInput('u'); + $user = true; + $comments = $this->getInput('comments'); + break; } foreach ($subreddits as $subreddit) { $name = trim($subreddit); - $values = getContents(self::URI . '/r/' . $name . '.json') + $values = getContents(self::URI . ($user ? '/user/' : '/r/') . $name . '.json') or returnServerError('Unable to fetch posts!'); $decodedValues = json_decode($values); foreach ($decodedValues->data->children as $post) { + if ($post->kind == 't1' && !$comments) { + continue; + } + $data = $post->data; $item = array(); $item['author'] = $data->author; - $item['title'] = $data->title; $item['uid'] = $data->id; $item['timestamp'] = $data->created_utc; $item['uri'] = $this->encodePermalink($data->permalink); $item['categories'] = array(); - $item['categories'][] = $data->link_flair_text; - $item['categories'][] = $data->pinned ? 'Pinned' : null; + + if ($post->kind == 't1') { + $item['title'] = 'Comment: ' . $data->link_title; + } else { + $item['title'] = $data->title; + + $item['categories'][] = $data->link_flair_text; + $item['categories'][] = $data->pinned ? 'Pinned' : null; + $item['categories'][] = $data->spoiler ? 'Spoiler' : null; + } + $item['categories'][] = $data->over_18 ? 'NSFW' : null; - $item['categories'][] = $data->spoiler ? 'Spoiler' : null; $item['categories'] = array_filter($item['categories']); - if ($data->is_self) { + if ($post->kind == 't1') { + // Comment + + $item['content'] + = htmlspecialchars_decode($data->body_html); + + } elseif ($data->is_self) { // Text post $item['content'] @@ -112,7 +153,7 @@ class RedditBridge extends BridgeAbstract { $id = $media->media_id; $type = $data->media_metadata->$id->m == 'image/gif' ? 'gif' : 'u'; $src = $data->media_metadata->$id->s->$type; - $images[] = ''; + $images[] = '
'; } $item['content'] = implode('', $images);