summaryrefslogtreecommitdiff
blob: dceb6ff5ac37cc37db235fd2394139a5df6be6f6 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python2

import datetime
import sys
import urllib
import xml.etree.ElementTree as ET

outfile = 'currency.units'
outstr = ''

if len(sys.argv)==2:
  outfile = sys.argv[1]
elif len(sys.argv)>2:
  sys.stderr.write('Usage: {0} [filename]\n\n'.format(sys.argv[0]))
  sys.stderr.write('Update currency information for \'units\' into the specified\n')
  sys.stderr.write('filename or the default location, \'{0}\'.\n'.format(outfile))
  sys.exit(1)

try:
  currencies = ET.parse(urllib.urlopen('http://rss.timegenie.com/forex.xml')).findall('data')
except IOError, exc:
  sys.stderr.write('Error connecting to currency server. {0}\n'.format(exc))
  sys.exit(1)

# print codes here

outstr += '# ISO Currency Codes\n\n'

maxlen = 0

for currency in currencies:
  code = currency.find('code').text
  description = currency.find('description').text.lower().replace(' ','')
  currency.find('description').text = description
  outstr += code + ' '*20 + description + '\n'
  if len(currency.find('description').text) > maxlen:
    maxlen = len(currency.find('description').text)
  if currency.find('code').text == 'USD':
    usdval = currency.find('rate').text[2:]
    currencies.remove(currency)

# print rates here

now = datetime.datetime.now()
outstr += '\n# Currency exchange rates from Time Genie (www.timegenie.com)\n'
outstr += '\n!message Currency exchange rates from ' + now.strftime('%Y-%m-%d') + '\n\n'

for currency in currencies:
  if currency.find('code').text == 'EUR':
    euro = currency.find('rate').text
    currency.find('rate').text = usdval + ' US$'

  else:
    currency.find('rate').text += ' euro'
  outstr += currency.find('description').text.ljust(maxlen+2) + '1|' + currency.find('rate').text + '\n'

# precious metals prices

# Another source for this data might be 
# http://www.xmlcharts.com/cache/precious-metals.xml

outstr += '\n# Precious metals prices from http://services.packetizer.com/spotprices/\n\n'

try:
  spotprices = ET.parse(urllib.urlopen('http://services.packetizer.com/spotprices/?f=xml'))
except IOError, exc:
  sys.stderr.write('Error connecting to spotprices server. {0}\n'.format(exc))
  sys.exit(1)

metals = ['gold','platinum','silver']

for metal in metals:
  outstr += '{0}    {1} US$/troyounce\n'.format((metal+'price').ljust(15), spotprices.find(metal).text)

try:
  if outfile == '-':
    outfile = sys.stdout
  else:
    outfile = open(outfile,'w')
except IOError, exc:
  sys.stderr.write('Unable to write to output file. {0}\n'.format(exc))
  sys.exit(1)

outfile.write(outstr)