From b78ed4ba02ac1a1fc766a687cf3f36e6f4005f6b Mon Sep 17 00:00:00 2001 From: Ronald Schaten Date: Tue, 19 Mar 2013 23:03:33 +0100 Subject: [PATCH] implemented mail sending --- atomstrom.py | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/atomstrom.py b/atomstrom.py index fbc3893..dde0fa1 100755 --- a/atomstrom.py +++ b/atomstrom.py @@ -161,19 +161,32 @@ def get_entry_text(entry): def mail_daily_digest(session, sender, receiver, prefix): print 'mailing daily digest...' + entries = session.query(Feed, Feedinfo, Entry).\ + filter(Feed.id == Feedinfo.feed_id).\ + filter(Feed.id == Entry.feed_id).\ + filter(Feed.enabled == 1).\ + filter(Feed.daily == 1).\ + filter(Entry.sent == None).\ + order_by(desc(Entry.firstfetched), Feedinfo.title, Entry.title).\ + all() body = '' count = 0 - for feed, feedinfo, entry in session.query(Feed, Feedinfo, Entry).filter(Feed.id==Feedinfo.feed_id).filter(Feed.id==Entry.feed_id).filter(Feed.enabled==1).filter(Feed.daily==1).order_by(desc(Entry.updated)).all(): + for feed, feedinfo, entry in entries: count = count + 1 - body = body + '=> %s - %s\n' % (entry.updated.strftime('%y%m%d-%H%M'), feedinfo.title) + body = body + '=> %s - %s\n' % (entry.firstfetched.strftime('%y%m%d-%H%M'), feedinfo.title) body = body + ' %s\n' % entry.title body = body + '%s\n' % get_entry_text(entry)[0:100] body = body + '%s\n\n' % entry.link - today = datetime.now() - subject = '%s (%s) - %d entries' % (today.strftime('%y%m%d'), today.strftime('%A'), count) - if prefix != '': - subject = '%s %s' % (prefix, subject) - send_mail(sender, receiver, subject, body) + if count > 0: + today = datetime.now() + subject = '%s (%s) - %d entries' % (today.strftime('%y%m%d'), today.strftime('%A'), count) + if prefix != '': + subject = '%s %s' % (prefix, subject) + send_mail(sender, receiver, subject, body) + for feed, feedinfo, entry in entries: + entry.sent = datetime.now() + else: + print 'no unmailed digest-entries found... not sending mail.' def mail_single_entry(feed, feedinfo, entry, sender, receiver, prefix): subject = '%s' % (entry.title) @@ -183,11 +196,25 @@ def mail_single_entry(feed, feedinfo, entry, sender, receiver, prefix): body = body + '%s\n' % feedinfo.link body = body + '%s\n' % entry.link send_mail(sender, receiver, subject, body) + entry.sent = datetime.now() def mail_single_entries(session, sender, receiver, prefix): print 'mailing single entries...' - for feed, feedinfo, entry in session.query(Feed, Feedinfo, Entry).filter(Feed.id==Feedinfo.feed_id).filter(Feed.id==Entry.feed_id).filter(Feed.enabled==1).filter(Feed.daily==0).all(): + count = 0 + entries = session.query(Feed, Feedinfo, Entry).\ + filter(Feed.id == Feedinfo.feed_id).\ + filter(Feed.id == Entry.feed_id).\ + filter(Feed.enabled == 1).\ + filter(Feed.daily == 0).\ + filter(Entry.sent == None).\ + all() + for feed, feedinfo, entry in entries: mail_single_entry(feed, feedinfo, entry, sender, receiver, prefix) + count = count + 1 + if count > 0: + print 'sent %d mails' % count + else: + print 'no unmailed single entries found... not sending mail.' def fetch_readability(link): text = hn.upgradeLink(link)