Recovered old changelog from 2011
[curcy.git] / curcy-update
1 #!/usr/bin/env python3
2
3 import logging
4
5 REFERENCE='EUR'
6
7 def get_yahoo_file():
8     from urllib import request
9     req = request.Request(
10         'http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote;currency=true?view=basic&format=json',
11         headers={'User-Agent': 'curcy'})
12     uo = request.urlopen(req)
13     httpdata = uo.read()
14     uo.close()
15     return httpdata.decode('UTF-8')
16
17 def get_yahoo_raw():
18     import json
19     yahoo_file = get_yahoo_file()
20     #file('curency.json', 'w').write(yahoo_file)
21     #yahoo_file = open('curency.json').read()
22     #print yahoo_file
23     yahoo_file = yahoo_file.replace(',\n]', ']')
24     return json.loads(yahoo_file)
25
26 def parse_yahoo(json_obj):
27     list = json_obj['list']
28     for res in list['resources']:
29         quote = res['resource']['fields']
30         if not quote['name'].startswith('USD/') or len(quote['name']) != 7:
31             logging.debug('Invalid name quote name %s', quote['name'])
32             continue
33         #yield quote['name']
34         yield quote['name'][4:], float(quote['price'])
35
36 def get_yahoo_quotes():
37     quotes = {}
38     for name, value in parse_yahoo(get_yahoo_raw()):
39         quotes[name] = value
40     assert len(quotes) > 10
41     quotes['USD'] = 1
42
43     ref_usd = quotes[REFERENCE]
44     
45     local_quotes = {}
46     for cur in quotes.keys():
47         if quotes[cur]:
48             local_quotes[cur] = ref_usd / quotes[cur]
49         else:
50             logging.error('no value for currency %s', cur)
51     quotes = local_quotes
52
53     # add historical european values
54     if REFERENCE == 'EUR':
55         quotes['ATS'] = 0.07267283416785971236
56         quotes['BEF'] = 0.02478935247732393982
57         quotes['DEM'] = 0.51129188119621848524
58         quotes['ESP'] = 0.00601012104383782289
59         quotes['FIM'] = 0.16818792646151103396
60         quotes['FRF'] = 0.15244901723741037903
61         quotes['GRD'] = 0.00293470286133528980
62         quotes['IEP'] = 1.26973807842918162841
63         quotes['ITL'] = 0.00051645689908948648
64         quotes['LUF'] = 0.02478935247732393982
65         quotes['NLG'] = 0.45378021609013890212
66         quotes['PTE'] = 0.00498797897068065961
67         quotes['SIT'] = 0.00417292605575029210
68         quotes['VAL'] = 0.00051645689908948648
69     else:
70         logging.warning('reference must be EUR to get legacy currencies exchange rates.')
71         logging.warning('WARNING: ATS BEF DEM ESP FIM FRF GRD IEP ITL LUF NLG PTE SIT VAL will be unavailable!')
72
73     return quotes
74
75 def main():
76     from optparse import OptionParser
77     parser = OptionParser('%prog [options]')
78     parser.add_option('-v', '--verbose',
79         help='Display informational messages',
80         action='store_true', dest='verbose', default=False)
81     parser.add_option('-l', '--listonly',
82         help='Just list the supported currencies',
83         action='store_true', dest='listonly', default=False)
84     parser.add_option('-a', '--aggressive',
85         help='Deprecaded.',
86         action='store_true', dest='aggressive', default=False)
87     parser.add_option('--target',
88         help='Set target filename. Defaults to %default',
89         dest='target_file', default='/var/currencies')
90     options, args = parser.parse_args()
91     
92     if options.verbose:
93         loglevel = logging.DEBUG
94     else:
95         loglevel = logging.INFO
96     logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s')
97
98     target_file = open(options.target_file, 'w')
99     for cur, value in get_yahoo_quotes().items():
100         print(cur, value, file=target_file)
101
102 if __name__ == '__main__':
103     main()
104