- The following code snippet can be used as daily crontab or windows scheduled task to watch the foreign exchange rate from BankOfCanada web site.
The file format is not fancy XML but a simple CSV for noon rate for last 5 days.
urllib2.urlopen is used as it can auto detect the proxy if it is used.
- The default url connnect timeout is 60 seconds here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import string
import smtplib
import urllib2
# configure ur threshold rate, smtp server here
# threshold rate for CAD --> USD noon rate
thresholdRate=1.10
smtpServer='test-smtp-server'
fromaddr='foo@bar.com'
toaddrs='you@corp.com'
# end of configuration
url='http://www.bankofcanada.ca/stats/assets/csv/fx-seven-day.csv'
f=urllib2.urlopen(url,timeout=60)
# read all in for efficency as it is a small file anyways.
data=f.read()
start=data.find('U.S. dollar (close)')
rate=string.strip(data[start:].split('\n')[0].split(',')[-1])
if float(rate) > thresholdRate:
#send email
msg = ('Subject: Bank of Canada Noon Foreign Exchange Rates %s for 1 USD ' % rate)
server = smtplib.SMTP(smtpServer)
#server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
f.close()
|
It is written for fun.