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)
readability = Column(Text)
firstfetched = Column(DateTime)
lastfetched = Column(DateTime)
sent = Column(DateTime)
def __init__(self, entry):
self.update(entry)
self.firstfetched = datetime.datetime.now()
def __repr__(self):
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 '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():
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):
sender = feedinfo.title
subject = entry.title
if entry.readability:
body = entry.readability
elif entry.fullpage:
body = entry.fullpage
elif entry.summary:
body = entry.summary
else:
body = 'no text, sorry'
body = get_entry_text(entry)
body = body + '\n\n'
body = body + 'site: [%s]\n' % feedinfo.link
body = body + 'link: [%s]\n' % entry.link
send_mail(sender, subject, body)
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():
mail_single_entry(feed, feedinfo, entry)
@ -171,7 +186,7 @@ def process_feed_entry(feed, entry):
try:
thisentry = query.one()
thisentry.update(entry)
return "-"
return '-'
except Exception, e:
thisentry = Entry(entry)
if feed.fullpage:
@ -179,12 +194,12 @@ def process_feed_entry(feed, entry):
if feed.readability:
thisentry.readability = fetch_readability(entry.link)
feed.entry.append(thisentry)
return "+"
return '+'
def fetch_single_feed(feed):
print "fetching %s" % feed.url
print 'fetching %s' % 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)
try:
feed.feedinfo = query.one()
@ -192,25 +207,26 @@ def fetch_single_feed(feed):
except Exception, e:
feed.feedinfo = Feedinfo(parser)
print "processing feed entries: ",
print 'processing feed entries: ',
entries_new = 0
entries_total = 0
for entry in parser.entries:
entries_total = entries_total + 1
ret = process_feed_entry(feed, entry)
if ret == "+":
if ret == '+':
entries_new = entries_new + 1
sys.stdout.write(ret)
print " (%d/%d new)" % (entries_new, entries_total)
print ' (%d/%d new)' % (entries_new, entries_total)
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):
fetch_single_feed(feed)
print
if __name__ == "__main__":
fetch_all_feeds()
mail_single_entries()
if __name__ == '__main__':
#fetch_all_feeds()
#mail_single_entries()
mail_daily_digest()
session.commit()