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()

print>>f,  '<a href="%s">%s</a> (%s)''' % (link.encode('UTF8', 'replace'),
                                            title.encode('UTF8','replace'),
                                            description.encode('UTF8','replace'))

print>>f,  '<a href="%s">%s</a> (%s)''' % (link.encode('UTF8', 'replace'),
                                            title.encode('UTF8','replace'),
                                            description.encode('UTF8','replace'))

Hi,

Thanks for the suggestion; unfortunately, it didn't act as I hoped. Inserting that in the script gave output as follows:

<a href="http://www.stuff.co.nz/national/crime/2345292/Homicide-inquiry-after-man-dies-in-Hastings">Homicide inquiry after man dies in Hastings </a> (Police have begun a homicide investigation after a man died at a party in Hastings overnight. )

What I'm after is:

Homicide query after man dies in Hastings #acting as link to story
Police have become a homicide investigation after a man died at a party in Hastings overnight.

Cheers,

Blair

Hi,

Thanks for the suggestion; unfortunately, it didn't act as I hoped. Inserting that in the script gave output as follows:

<a href="http://www.stuff.co.nz/national/crime/2345292/Homicide-inquiry-after-man-dies-in-Hastings">Homicide inquiry after man dies in Hastings </a> (Police have begun a homicide investigation after a man died at a party in Hastings overnight. )

What I'm after is:

Homicide query after man dies in Hastings #acting as link to story
Police have become a homicide investigation after a man died at a party in Hastings overnight.

Cheers,

Blair

Looks like this is what you need: http://code.activestate.com/recipes/67083/

It suggests using both a plain text version and a html version, so in your script you'd create 2 files, and in the loop do something like:

print>>textfile,  '"%s" %s (%s)''' % (link.encode('UTF8', 'replace'),
                                            title.encode('UTF8','replace'),
                                            description.encode('UTF8','replace'))
    print>>htmlfile,  '<a href="%s">%s</a> (%s)''' % (link.encode('UTF8', 'replace'),
                                            title.encode('UTF8','replace'),
                                            description.encode('UTF8','replace'))

and then use createhtmlmail to create the message to send.

Thank you, will give that a go tomorrow (Sunday night here in NZ and had a few too many wines to trust myself working on this now:icon_redface:

Looks like this is what you need: http://code.activestate.com/recipes/67083/

It suggests using both a plain text version and a html version, so in your script you'd create 2 files, and in the loop do something like:

print>>textfile,  '"%s" %s (%s)''' % (link.encode('UTF8', 'replace'),
                                            title.encode('UTF8','replace'),
                                            description.encode('UTF8','replace'))
    print>>htmlfile,  '<a href="%s">%s</a> (%s)''' % (link.encode('UTF8', 'replace'),
                                            title.encode('UTF8','replace'),
                                            description.encode('UTF8','replace'))

and then use createhtmlmail to create the message to send.