-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmsc_prices.py
74 lines (61 loc) · 2.45 KB
/
msc_prices.py
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
#!/usr/bin/python
#######################################################
# #
# Copyright Masterchain Grazcoin Grimentz 2013-2014 #
# https://github.com/grazcoin/mastercoin-tools #
# https://masterchain.info #
# masterchain@@bitmessage.ch #
# License AGPLv3 #
# #
#######################################################
import os
import urllib2
from optparse import OptionParser
from msc_utils_validating import *
##################################
# main function - updates prices #
##################################
def update_prices():
# parse command line arguments
parser = OptionParser("usage: %prog [options]")
parser.add_option("-d", "--debug", action="store_true",dest='debug_mode', default=False,
help="turn debug mode on")
(options, args) = parser.parse_args()
msc_globals.init()
msc_globals.d=options.debug_mode
updated_prices={}
info('starting update prices')
filename='general/bitcoinaverage_ticker_usd.json'
response = urllib2.urlopen('https://api.bitcoinaverage.com/ticker/USD/')
f = open(filename, "w")
f.write(response.read())
f.close()
l=load_dict_from_file(filename, all_list=True)
bitcoin_avg=l['24h_avg']
updated_prices['Bitcoin']=bitcoin_avg
values=load_dict_from_file('www/values.json', all_list=True)
for entry in values:
try:
last_price=float(entry['last_price'])
symbol=entry['currency']
coin_name=currencies_per_symbol_dict[symbol]['name']
updated_prices[coin_name]=last_price*float(bitcoin_avg)
except KeyError:
pass
# prepare updated currency list
updated_currencies_list=[]
for coin in coins_list:
coin_details=currencies_per_name_dict[coin]
id=coins_dict[coin] # take from pre generated dict
price=0.0
try:
price=updated_prices[coin]
except KeyError:
pass
info('updating '+coin+' with price '+str(price))
d={"ID":id,"name":coin_details["name"],"symbol":coin_details["symbol"],"dollar":price}
updated_currencies_list.append(d)
atomic_json_dump(updated_currencies_list,'www/currencies.json', add_brackets=False)
info('update prices done')
if __name__ == "__main__":
update_prices()