Hi,
Playing with a script I did a while ago that sends emails alerts when rss feeds are updated to make it more readable, and have struck a problem.
At present the email it sends is formatted as follows:
"http://www.stuff.co.nz/national/2344448/Group-charges-in-to-reduce-bag-use" Group charges in to reduce bag use (Pressure from environmentalists helped sway Foodstuffs' decision to charge for plastic bags.)
Would like to have the html address removed, with the headline (in the example above it would be Group charges in to reduce bag use) a clickable link.
Any hints on what I should look at would be much appreciated.
Blair
PS: The script so far, if required, is:
#! /usr/bin/env python
import urllib
import sys
import xml.dom.minidom
import hashlib
import os
import re
#import time
#The url of the feed
address = 'http://www.stuff.co.nz/rss/'
#Our actual xml document
f = open('appendtest.txt.tmp', '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, '"%s" %s (%s)''' % (link.encode('UTF8', 'replace'),
title.encode('UTF8','replace'),
description.encode('UTF8','replace'))
#The url of the feed
addresstwo = 'http://www.stuff.co.nz/rss/national'
#Our actual xml document
f = open('appendtest.txt.tmp', 'a')
print>>f, "<b>NATIONAL STORIES<b>"
document = xml.dom.minidom.parse(urllib.urlopen(addresstwo))
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, '"%s" %s (%s)''' % (link.encode('UTF8', 'replace'),
title.encode('UTF8','replace'),
description.encode('UTF8','replace'))
f.close()
fh = open('appendtest.txt.tmp','r')
stuff = fh.read()
fh.close()
new_stuff = re.sub(r'\)', r')\n\n',stuff)
fh = open('appendtest.txt.tmp','w')
fh.write(new_stuff)
fh.close()
import md5
orig_md5= hashlib.md5(file('newstuff.txt').read())
new_md5= hashlib.md5(file('appendtest.txt.tmp').read())
if orig_md5.digest() == new_md5.digest():
os.remove('appendtest.txt.tmp')
sys.exit()
else:
os.rename('appendtest.txt.tmp', 'newstuff.txt')
import smtplib
#from http://stackoverflow.com/questions/399129/failing-to-send-email-with-the-python-example
FROMADDR = "myaddresst@gmail.com"
LOGIN = FROMADDR
PASSWORD = "mypassword"
TOADDRS = ["address1", "address2"]
SUBJECT = "Top/National combined test alert"
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
% (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += open('newstuff.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()