Hi,

Making good progress with a script that collects rss feeds from a news website and emails headlines but wondering whether I can alter what I have to email only the new headlines.
My idea is that the new headlines would be compared with the previous ones using something like this:

= open('testlista.txt', 'r').read().split('\n')
>>> b = open('testlistb.txt', 'r').read().split('\n')
>>> c = open('difference.txt', 'w')
>>> c.write('\n'.join([comm for comm in b if not (comm in a)]))
>>> c.close()

However, would I be best to use tempfile instead of lists? I would anticipate 100kb a day in total.

Not sure whether it matters, but existing stuff is as follows:

#! /usr/bin/env python

import urllib
import sys
import xml.dom.minidom

#The url of the feed
address = 'http://www.stuff.co.nz/rss/'

#Our actual xml document
f = open('alerttest.txt',  'w')

document = xml.dom.minidom.parse(urllib.urlopen(address))
for item in document.getElementsByTagName('item'):
    title = item.getElementsByTagName('title')[0].firstChild.data
    link = item.getElementsByTagName('link')[0].firstChild.data
    description = item.getElementsByTagName('description')[0].firstChild.data
    
    print>>f,  '''<a href="%s">%s</a> (%s)''' % (link.encode('UTF8', 'replace'),
                                            title.encode('UTF8','replace'),
                                            description.encode('UTF8','replace'))

f.close()
import smtplib

# from http://stackoverflow.com/questions/399129/failing-to-send-email-with-the-python-example
FROMADDR = "orpheus.alert@gmail.com"
LOGIN    = FROMADDR
PASSWORD = "elmrow97"
TOADDRS  = ["blair.mayston@xtra.co.nz"]
SUBJECT  = "Test alert"

msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
       % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += open('alerttest.txt', 'r').read()

server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()

Cheers,

Blair

This article has been dead for over six months. Start a new discussion instead.