0

I'm doing a bit of an experiment in Python. I'm making a script which checks a rss-feed for new items, and then sends the title and link of the items via email. I've got the script to work to a certain level: when it runs it will take the link+title of the newest item and email it, regardless of wether it emailed that file already or not. I'd need to add 2 things: a way to get multiple items at once (and email those, one by one), and a way to check wether they have been sent already. How would I do this? I'm using feedparser, this is what I've got so far:

d = feedparser.parse('http://feedparser.org/docs/examples/rss20.xml')
link = d.entries[0].link
title = d.entries[0].title

And then a couple of lines which send an email with "link" and "title" in there. I know I'd need to use the Etag, but haven't been able to work out how, and how would I send the emails 1 by 1?

HankSmackHood
  • 4,673
  • 7
  • 29
  • 30

2 Answers2

0

For the simplest method see the python smtplib documentation example. (I won't repeat the code here.) It's all you need for basic email sending.

For nicer/more complicated email content also look into python's email module, of course.

Tim
  • 356
  • 1
  • 2
0

for the feed parsing part, you could consider following the advise given in this question regarding How to detect changed and new items in an RSS feed?. Basically, you could hash the contents of each entry and use that as an id.

For instance, on the first run of your program it will calculate the hash of each entry, store that hash, and send these new entries by mail. On it's next run, it will rehash each entry's content and compare those hashes with the ones found before (you should use some sort of database for this, or at least an on memory dictionary/list when developing with the entries already parsed and sent). If your program finds hashes that where not generated on the previous runs, it will assemble a new email and send it with the "new" entries.

As for your email assembling part, the question Sending HTML email in Python could help. Just make sure to send a text only and a html version.

Community
  • 1
  • 1
Federico Cáceres
  • 1,216
  • 12
  • 19
  • Isn't the ETag in essence a hash itself? So if I'd save that, I'd have the same result as hashing any other thing? And sending an email isn't the problem, I've got that sorted. Only thing I don't know how to do, is to send the emails 1 by 1. Right now I can only get it to send 1 email at a time, using the newest item in the rss-feed. – HankSmackHood Feb 08 '11 at 15:50
  • You said that you can only send 1 email at a time, with the newest item, and tahat you want to send the mails one by one. You mean that if there are more that 1 new item you cannot know which are they? – Federico Cáceres Feb 08 '11 at 16:56
  • Unless I'm mistaken, an etag represents the whole feed, that is, comparing two etags you can know that the feed has changed, but you don't know which feeds are new. You could use etags to find out if the feed has changed, and once you find out that it has, you check the feed's entries for new ones (using the hashing technique on the content of each entry). And for each new entry to send your mail. – Federico Cáceres Feb 08 '11 at 17:29