From a059e00533bb4e6ee8955e1c906ccd71115c014c Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Mon, 21 Jan 2013 19:33:28 +0100 Subject: Test if the article is present in the database before sending it via mail. --- source/feedgetter.py | 4 ++-- source/mongodb.py | 5 ++++- source/utils.py | 8 ++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) (limited to 'source') diff --git a/source/feedgetter.py b/source/feedgetter.py index 8b1c8401..84dd2f47 100755 --- a/source/feedgetter.py +++ b/source/feedgetter.py @@ -155,8 +155,8 @@ class FeedGetter(object): articles.append(article) - if feed["mail"]: - # send new articles by e-mail if desired. + if feed["mail"] and self.articles.get_articles(feed_id, article_id) == False: + # if subscribed to the feed AND if article not already in the database threading.Thread(None, utils.send_mail, None, (conf.mail_from, conf.mail_to, \ a_feed.feed.title, \ article_title, description)).start() diff --git a/source/mongodb.py b/source/mongodb.py index 7879adee..93894154 100644 --- a/source/mongodb.py +++ b/source/mongodb.py @@ -131,7 +131,10 @@ class Articles(object): elif feed_id != None and article_id != None: # Return a precise article. collection = self.db[str(feed_id)] - return next(collection.find({"article_id":article_id})) + try: + return next(collection.find({"article_id":article_id})) + except: + return False def get_favorites(self, feed_id=None): """ diff --git a/source/utils.py b/source/utils.py index 4ceb3026..f2756286 100755 --- a/source/utils.py +++ b/source/utils.py @@ -168,8 +168,12 @@ def send_mail(mfrom, mto, feed_title, article_title, description): msg.attach(part2) # Send the message via local SMTP server. - s = smtplib.SMTP(conf.smtp_server) - s.login(conf.username, conf.password) + try: + s = smtplib.SMTP(conf.smtp_server) + s.login(conf.username, conf.password) + except: + print("smtp error") + return # sendmail function takes 3 arguments: sender's address, recipient's address # and message to send - here it is sent as one string. s.sendmail(mfrom, mto, msg.as_string()) -- cgit