From 275dab5a58568641f560dbc2b44cb0c42e99d580 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 11 Nov 2017 17:13:50 +0100 Subject: [PATCH] Added /get command --- robotrss.py | 48 +++++++++++++++++++++++++++++++++++++-- tests/test_feedhandler.py | 7 ++++++ util/database.py | 4 ++-- util/feedhandler.py | 10 +++++--- util/processing.py | 10 ++++---- 5 files changed, 67 insertions(+), 12 deletions(-) diff --git a/robotrss.py b/robotrss.py index d492ef1..9dd6da5 100644 --- a/robotrss.py +++ b/robotrss.py @@ -28,6 +28,7 @@ def __init__(self, telegram_token, update_interval): self._addCommand(CommandHandler("list", self.list)) self._addCommand(CommandHandler("about", self.about)) self._addCommand(CommandHandler("add", self.add, pass_args=True)) + self._addCommand(CommandHandler("get", self.get, pass_args=True)) self._addCommand(CommandHandler("remove", self.remove, pass_args=True)) # Start the Bot @@ -95,14 +96,15 @@ def add(self, bot, update, args): # Check if entry does not exists entries = self.db.get_urls_for_user(telegram_id=telegram_user.id) + print(entries) - if any(arg_url.lower() in arg_url for entry in entries): + if any(arg_url.lower() in entry for entry in entries): message = "Sorry, " + telegram_user.first_name + \ "! I already have that url with stored in your subscriptions." update.message.reply_text(message) return - if any(arg_entry in arg_entry for entry in entries): + if any(arg_entry in entry for entry in entries): message = "Sorry! I already have an entry with name " + \ arg_entry + " stored in your subscriptions.. Please choose another entry name or delete the entry using '/remove " + arg_entry + "'" update.message.reply_text(message) @@ -113,6 +115,48 @@ def add(self, bot, update, args): message = "I successfully added " + arg_entry + " to your subscriptions!" update.message.reply_text(message) + def get(self, bot, update, args): + """ + Manually parses an rss feed + """ + + telegram_user = update.message.from_user + + if len(args) > 2: + message = "To get the last news of your subscription please use /get [optional: ]. Make sure you first add a feed using the /add command." + update.message.reply_text(message) + return + + if len(args) == 2: + args_entry = args[0] + args_count = int(args[1]) + else: + args_entry = args[0] + args_count = 4 + + url = self.db.get_user_bookmark( + telegram_id=telegram_user.id, alias=args_entry) + + if url is None: + message = "I can not find an entry with label " + \ + args_entry + " in your subscriptions! Please check your subscriptions using /list and use the delete command again!" + update.message.reply_text(message) + return + + entries = FeedHandler.parse_feed(url[0], args_count) + for entry in entries: + message = "[" + url[1] + "] " + entry.title + "" + print(message) + + try: + update.message.reply_text(message, parse_mode=ParseMode.HTML) + except Unauthorized: + self.db.update_user(telegram_id=telegram_user.id, is_active=0) + except TelegramError: + # handle all other telegram related errors + pass + def remove(self, bot, update, args): """ Removes an rss subscription from user diff --git a/tests/test_feedhandler.py b/tests/test_feedhandler.py index c7320c0..b8cd64f 100644 --- a/tests/test_feedhandler.py +++ b/tests/test_feedhandler.py @@ -8,6 +8,13 @@ def test_parse_feed(self): url = "https://lorem-rss.herokuapp.com/feed" feed = FeedHandler.parse_feed(url) self.assertIsNotNone(url) + url = "https://lorem-rss.herokuapp.com/feed" + + def test_parse_feed_amount(self): + url = "https://lorem-rss.herokuapp.com/feed" + feed = FeedHandler.parse_feed(url, 5) + self.assertIsNotNone(url) + self.assertEqual(len(feed), 5) def test_is_parsable(self): url = "https://lorem-rss.herokuapp.com/feed" diff --git a/util/database.py b/util/database.py index 3bc8eee..fec684e 100644 --- a/util/database.py +++ b/util/database.py @@ -155,7 +155,7 @@ def get_url(self, url): return result - def get_all_urls(self, ): + def get_all_urls(self): conn = sqlite3.connect(self.database_path) cursor = conn.cursor() @@ -237,7 +237,7 @@ def get_users_for_url(self, url): cursor = conn.cursor() cursor.execute( - "SELECT user.* FROM user, web_user WHERE web_user.telegram_id = user.telegram_id AND web_user.url ='" + str(url) + "';") + "SELECT user.*, web_user.alias FROM user, web_user WHERE web_user.telegram_id = user.telegram_id AND web_user.url ='" + str(url) + "';") result = cursor.fetchall() conn.commit() diff --git a/util/feedhandler.py b/util/feedhandler.py index fc10b79..492d1db 100644 --- a/util/feedhandler.py +++ b/util/feedhandler.py @@ -5,13 +5,17 @@ class FeedHandler(object): @staticmethod - def parse_feed(url): + def parse_feed(url, entries=0): """ Parses the given url, returns a list containing all available entries """ - feed = feedparser.parse(url) - return feed.entries + if 1 <= entries <= 10: + feed = feedparser.parse(url) + return feed.entries[:entries] + else: + feed = feedparser.parse(url) + return feed.entries[:4] @staticmethod def is_parsable(url): diff --git a/util/processing.py b/util/processing.py index 528f217..01e2ed7 100644 --- a/util/processing.py +++ b/util/processing.py @@ -55,7 +55,7 @@ def update_feed(self, url): try: for post in FeedHandler.parse_feed(url[0]): self.send_newest_messages( - url=url, post=post, telegram_id=user[0]) + url=url, post=post, user=user) except: message = "Something went wrong when I tried to parse the URL: \n\n " + \ url[0] + "\n\nCould you please check that for me? Remove the url from your subscriptions using the /remove command, it seems like it does not work anymore!" @@ -65,18 +65,18 @@ def update_feed(self, url): self.db.update_url(url=url[0], last_updated=str( DateHandler.get_datetime_now())) - def send_newest_messages(self, url, post, telegram_id): + def send_newest_messages(self, url, post, user): post_update_date = DateHandler.parse_datetime(datetime=post.updated) url_update_date = DateHandler.parse_datetime(datetime=url[1]) if post_update_date > url_update_date: - message = "" + post.title + "" try: self.bot.send_message( - chat_id=telegram_id, text=message, parse_mode=ParseMode.HTML) + chat_id=user[0], text=message, parse_mode=ParseMode.HTML) except Unauthorized: - self.db.update_user(telegram_id=telegram_id, is_active=0) + self.db.update_user(telegram_id=user[0], is_active=0) except TelegramError: # handle all other telegram related errors pass