From e2afd9f27af9b62256bcd33265e5b8aef2d87a5a Mon Sep 17 00:00:00 2001 From: Fabio Date: Tue, 21 May 2019 20:40:51 +0200 Subject: [PATCH] Add redd.it urls integration (#4) Add ability to link redd.it urls Refactor reddit_linker Add redd.it test case --- helpers.py | 13 ++++++++++++- reddit_linker.py | 31 +++++++++++++++---------------- telereddit.py | 9 ++++----- test_helpers.py | 10 ++++++++++ 4 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 test_helpers.py diff --git a/helpers.py b/helpers.py index b4676aa..c00f35b 100644 --- a/helpers.py +++ b/helpers.py @@ -14,4 +14,15 @@ def truncate_text(text, length=100): def polish_text(text): - return text.replace('\n', ' ') \ No newline at end of file + return text.replace('\n', ' ') + + +def get_urls_from_text(text): + polished = polish_text(text) + urls = list() + for w in polished.lower().split(' '): + if 'reddit.com' in w: + urls.append(w.partition('/?')[0] + '.json') + if 'redd.it' in w: + urls.append(f'https://www.reddit.com/comments/{w.partition("redd.it/")[2]}.json') + return urls \ No newline at end of file diff --git a/reddit_linker.py b/reddit_linker.py index 15aa585..aab7171 100644 --- a/reddit_linker.py +++ b/reddit_linker.py @@ -22,54 +22,53 @@ def send_random_posts(bot, chat_id, text): if status == 'failed': tries += 1 if status != 'success': - _send_exception_message(bot, chat_id, subreddit, fail_msg) + _send_exception_message(bot, chat_id, fail_msg) def send_post_from_url(bot, chat_id, post_url): - subreddit = helpers.get_subreddit_names(post_url) - if not len(subreddit): - return - subreddit = subreddit[0] - status, fail_msg = send_post(bot, chat_id, subreddit, post_url) + status, fail_msg = send_post(bot, chat_id, post_url=post_url) if status == 'failed': - _send_exception_message(bot, chat_id, subreddit, fail_msg) + _send_exception_message(bot, chat_id, fail_msg) -def _get_post(subreddit, post_url=None): +def _get_post(subreddit=None, post_url=None): if not post_url: post_url = f'https://www.reddit.com/{subreddit}/random.json' try: json = requests.get(post_url, headers={'User-agent': 'telereddit_bot'}).json() except ValueError: - return None, f"I'm sorry, I can't find {subreddit}." + return None, f"I'm sorry, I can't find that subreddit." if not json: - return None, f"I'm sorry, I can't find {subreddit}." + return None, f"I'm sorry, I can't find that subreddit." # some subreddits have the json data wrapped in brackets, some do not json = json if isinstance(json, dict) else json[0] if json.get('reason') == 'private': - return None, f"I'm sorry, the subreddit {subreddit} is private." + return None, f"I'm sorry, this subreddit is private." not_found = json.get('error', 200) == 404 or len(json['data']['children']) == 0 if not_found: - return None, f"I'm sorry, the subreddit {subreddit} doesn't exist!" + return None, f"I'm sorry, this subreddit doesn't exist!" return json, None -def send_post(bot, chat_id, subreddit, post_url=None): +def send_post(bot, chat_id, subreddit=None, post_url=None): + if not subreddit and not post_url: + return + json, err_msg = _get_post(subreddit, post_url) if err_msg: bot.sendMessage(chat_id, err_msg) return 'success', None - subreddit_url = f'https://www.reddit.com/{subreddit}' try: idx = random.randint(0, len(json['data']['children']) - 1) data = json['data']['children'][idx]['data'] - + subreddit = data['subreddit_name_prefixed'] + subreddit_url = f'https://www.reddit.com/{subreddit}' post_text = data['selftext'] content_url = data['url'] permalink = data['permalink'] @@ -164,7 +163,7 @@ def more_button_callback(bot, msg): send_random_posts(bot, chat_id, subreddit) -def _send_exception_message(bot, chat_id, subreddit, msg): +def _send_exception_message(bot, chat_id, msg): keyboard = InlineKeyboardMarkup(inline_keyboard=[ [InlineKeyboardButton(text='Try with another random post', callback_data='reddit')] ]) diff --git a/telereddit.py b/telereddit.py index 1be5ff2..c49f7d5 100644 --- a/telereddit.py +++ b/telereddit.py @@ -17,11 +17,10 @@ def on_chat_message(msg): return text = msg['text'] - if 'reddit.com' in text.lower(): - polished = helpers.polish_text(text) - post_url = [w for w in polished.lower().split(' ') if 'reddit.com' in w][0] - post_url = post_url.partition('/?')[0] + '.json' - reddit_linker.send_post_from_url(bot, chat_id, post_url) + if any(r in text.lower() for r in ['reddit.com', 'redd.it']): + posts_url = helpers.get_urls_from_text(text) + for url in posts_url: + reddit_linker.send_post_from_url(bot, chat_id, url) elif 'r/' in text.lower(): reddit_linker.send_random_posts(bot, chat_id, text) diff --git a/test_helpers.py b/test_helpers.py new file mode 100644 index 0000000..108da26 --- /dev/null +++ b/test_helpers.py @@ -0,0 +1,10 @@ +import unittest +import helpers + + +class TestHelpers(unittest.TestCase): + def test_get_urls_from_text(self): + text = 'https://redd.it/bqdlxe https://redd.it/bqekoq' + urls = ['https://www.reddit.com/comments/bqdlxe.json', + 'https://www.reddit.com/comments/bqekoq.json'] + self.assertListEqual(helpers.get_urls_from_text(text), urls) \ No newline at end of file