Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
Added /get command
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian committed Nov 11, 2017
1 parent 106db30 commit 275dab5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
48 changes: 46 additions & 2 deletions robotrss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 <entryname> [optional: <count 1-10>]. 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] + "] <a href='" + \
entry.link + "'>" + entry.title + "</a>"
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
Expand Down
7 changes: 7 additions & 0 deletions tests/test_feedhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions util/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
10 changes: 7 additions & 3 deletions util/feedhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions util/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand All @@ -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 = "<a href='" + post.link + \
message = "[" + user[7] + "] <a href='" + post.link + \
"'>" + post.title + "</a>"
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
Expand Down

0 comments on commit 275dab5

Please sign in to comment.