/usr/bin/units_cur is in units 2.12-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #!/usr/bin/python
#
# Version 2
# 9 July 2013
#
# Modified to use xml format output from timegenie instead of text file
#
# Version 3
# 7 March 2014
#
# Added checks for unicode currency names
#
# Version 3.1
# 23 June 2014
#
# Added test for non-unicode strings to prevent a runtime warning
#
import sys
import urllib
import os
import xml.etree.ElementTree as ET
import codecs
from datetime import datetime
from unidecode import unidecode
outfile='/usr/share/units/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
codes = [x.find('code').text for x in currencies]
names = [x.find('description').text for x in currencies]
values = [x.find('rate').text for x in currencies]
# Sometimes currency rates are missing from the xml file
# Delete such currencies
while True:
try:
none = values.index(None)
sys.stderr.write('no rate for "%s" (%s)\n' % (names[none], codes[none]))
del codes[none]
del names[none]
del values[none]
except ValueError:
break
# Precious metals are showing up with weird prices (per gram?)
# Delete them
metalcodes = ['XAG','XAU','XPD','XPT']
for i in xrange(len(metalcodes)):
try:
badind = codes.index(metalcodes[i])
del codes[badind]
del names[badind]
del values[badind]
except ValueError:
break
names = [x.lower().replace(' ','') for x in names]
foundunicode=False
for i in xrange(len(names)):
if names[i] == 'anguilla(ecd)':
names[i] = 'eastcaribbeandollar'
if isinstance(names[i],unicode): # this test needed only for python2
ascii = unidecode(names[i])
else:
ascii = names[i]
if names[i] != ascii:
if not foundunicode:
foundunicode = True
outstr += '# Unicode Currency Names\n\n!utf8\n'
outstr += names[i] + ' ' *(23-len(names[i])) + ascii + '\n'
names[i] = ascii
if foundunicode:
outstr += '!endutf8\n\n'
outstr += '# ISO Currency Codes\n\n'
outstr += '\n'.join([x + ' '*20 + y for x,y in zip(codes,names)])
usd = codes.index('USD')
euro = codes.index('EUR')
usdval = values[usd]
values = ['1|' + x +' euro' for x in values]
values[euro] = usdval + ' US$'
del names[usd]
del values[usd]
# print rates here
now = datetime.now()
outstr += '\n\n# Currency exchange rates from Time Genie (www.timegenie.com)\n'
outstr += '\n!message Currency exchange rates from www.timegenie.com on '+now.strftime('%Y-%m-%d')+'\n\n'
maxlen = max(map(len,names)) + 2
outstr += '\n'.join([x.ljust(maxlen) + y for x,y in zip(names, values)])
# precious metals prices
outstr += '\n\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 == '-':
info = codecs.lookup('utf8')
outfile = codecs.StreamReaderWriter(sys.stdout, info.streamreader, info.streamwriter)
else:
outfile = codecs.open(outfile,'w','utf8')
except IOError, exc:
sys.stderr.write('Unable to write to output file. {0}\n'.format(exc))
sys.exit(1)
outfile.write(outstr.replace('\n',os.linesep))
# In python3, open(outfile,mode='w',encoding='utf8') should work
# and then the explicit newline handling won't be needed
|