implemented first version of mail_daily_digest()

This commit is contained in:
Ronald Schaten 2010-10-30 11:39:19 +02:00
parent 0b366a9d55
commit b3d5acc6df

View File

@ -93,11 +93,13 @@ class Entry(Base):
fullpage = Column(Text) fullpage = Column(Text)
readability = Column(Text) readability = Column(Text)
firstfetched = Column(DateTime)
lastfetched = Column(DateTime) lastfetched = Column(DateTime)
sent = Column(DateTime) sent = Column(DateTime)
def __init__(self, entry): def __init__(self, entry):
self.update(entry) self.update(entry)
self.firstfetched = datetime.datetime.now()
def __repr__(self): def __repr__(self):
return "<Entry('%s','%s','%s')>" % (self.title, "", "") return "<Entry('%s','%s','%s')>" % (self.title, "", "")
@ -132,27 +134,40 @@ def send_mail(sender, subject, body):
print 'Subject: %s' % subject.decode('latin-1') print 'Subject: %s' % subject.decode('latin-1')
print 'Body: %s' % body.decode('latin-1') print 'Body: %s' % body.decode('latin-1')
def get_entry_text(entry):
if entry.readability:
text = entry.readability
elif entry.fullpage:
text = entry.fullpage
elif entry.summary:
text = entry.summary
else:
text = 'no text, sorry'
return text
def mail_daily_digest(): def mail_daily_digest():
pass print 'mailing daily digest...'
sender = 'atomstrom'
subject = '[atomstrom]'
body = ''
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(Entry.firstfetched).all():
body = body + '=> %s - %s\n' % (feedinfo.title, entry.title)
body = body + get_entry_text(entry)[0:100]
body = body + '\n'
body = body + 'link: [%s]\n\n' % entry.link
send_mail(sender, subject, body)
def mail_single_entry(feed, feedinfo, entry): def mail_single_entry(feed, feedinfo, entry):
sender = feedinfo.title sender = feedinfo.title
subject = entry.title subject = entry.title
if entry.readability: body = get_entry_text(entry)
body = entry.readability
elif entry.fullpage:
body = entry.fullpage
elif entry.summary:
body = entry.summary
else:
body = 'no text, sorry'
body = body + '\n\n' body = body + '\n\n'
body = body + 'site: [%s]\n' % feedinfo.link body = body + 'site: [%s]\n' % feedinfo.link
body = body + 'link: [%s]\n' % entry.link body = body + 'link: [%s]\n' % entry.link
send_mail(sender, subject, body) send_mail(sender, subject, body)
def mail_single_entries(): def mail_single_entries():
print "mailing single entries..." 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(): 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():
mail_single_entry(feed, feedinfo, entry) mail_single_entry(feed, feedinfo, entry)
@ -171,7 +186,7 @@ def process_feed_entry(feed, entry):
try: try:
thisentry = query.one() thisentry = query.one()
thisentry.update(entry) thisentry.update(entry)
return "-" return '-'
except Exception, e: except Exception, e:
thisentry = Entry(entry) thisentry = Entry(entry)
if feed.fullpage: if feed.fullpage:
@ -179,12 +194,12 @@ def process_feed_entry(feed, entry):
if feed.readability: if feed.readability:
thisentry.readability = fetch_readability(entry.link) thisentry.readability = fetch_readability(entry.link)
feed.entry.append(thisentry) feed.entry.append(thisentry)
return "+" return '+'
def fetch_single_feed(feed): def fetch_single_feed(feed):
print "fetching %s" % feed.url print 'fetching %s' % feed.url
parser = feedparser.parse(feed.url) parser = feedparser.parse(feed.url)
print "processing feed info..." print 'processing feed info...'
query = session.query(Feedinfo).filter(Feedinfo.feed_id==feed.id) query = session.query(Feedinfo).filter(Feedinfo.feed_id==feed.id)
try: try:
feed.feedinfo = query.one() feed.feedinfo = query.one()
@ -192,25 +207,26 @@ def fetch_single_feed(feed):
except Exception, e: except Exception, e:
feed.feedinfo = Feedinfo(parser) feed.feedinfo = Feedinfo(parser)
print "processing feed entries: ", print 'processing feed entries: ',
entries_new = 0 entries_new = 0
entries_total = 0 entries_total = 0
for entry in parser.entries: for entry in parser.entries:
entries_total = entries_total + 1 entries_total = entries_total + 1
ret = process_feed_entry(feed, entry) ret = process_feed_entry(feed, entry)
if ret == "+": if ret == '+':
entries_new = entries_new + 1 entries_new = entries_new + 1
sys.stdout.write(ret) sys.stdout.write(ret)
print " (%d/%d new)" % (entries_new, entries_total) print ' (%d/%d new)' % (entries_new, entries_total)
def fetch_all_feeds(): def fetch_all_feeds():
print "fetching all feeds..." print 'fetching all feeds...'
for feed in session.query(Feed).filter_by(enabled=1).order_by(Feed.id): for feed in session.query(Feed).filter_by(enabled=1).order_by(Feed.id):
fetch_single_feed(feed) fetch_single_feed(feed)
print print
if __name__ == "__main__": if __name__ == '__main__':
fetch_all_feeds() #fetch_all_feeds()
mail_single_entries() #mail_single_entries()
mail_daily_digest()
session.commit() session.commit()