-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_currency.py
273 lines (263 loc) · 11.7 KB
/
get_currency.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from urllib.request import urlopen
from urllib.error import URLError
from configurations import currency_configurations
from configurations import retry_configurations
import datetime
import argparse
import time
import simplejson as json
from functools import wraps
import decimal
import csv, sqlite3
import configparser
config = configparser.RawConfigParser()
config.read('settings.ini')
# Trying out a Retry decorator in Python = https://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
def deco_retry(f):
@wraps(f)
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay
while mtries > 1:
try:
return f(*args, **kwargs)
except ExceptionToCheck as e:
msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
if logger:
logger.warning(msg)
else:
print(msg)
time.sleep(mdelay)
mtries -= 1
mdelay *= backoff
return f(*args, **kwargs)
return f_retry # true decorator
return deco_retry
def validate(date_text):
try:
date_converted = datetime.datetime.strptime(date_text, '%Y-%m-%d')
return date_converted
except ValueError:
raise ValueError("Incorrect data format, should be YYYY-MM-DD")
@retry(URLError, tries=retry_configurations.config["tries"], delay=retry_configurations.config["delay"], backoff=retry_configurations.config["backoff"])
def get_currency(reference_date):
currency_base=args.basecurrency
basecurrencyusedonsite= args.basecurrency if args.paid_membership else default_currencybase
country_map = currency_configurations.currencymap
search=args.currencynamelist
usersymbol=args.symbollist
symbols=""
url=""
query=""
inputvalues = [item.lower() for item in search]
inputsymbols = [item.lower() for item in usersymbol]
cur.execute("SELECT COUNT(*) FROM currency WHERE date = '%s'" % reference_date + ";")
if (cur.fetchall()[0])[0] == 0:
url = domain_name + prefix_get_date.replace("YYYY-MM-DD",reference_date) + variable_apikey + "=" + value_apikey + "&" + variable_basekey + "=" + basecurrencyusedonsite
if args.debug:
print("Debug: Calling API to cache data from " + url.replace(value_apikey,"<YOUR_API_KEY>"))
html = urlopen(url)
handler = html.read()
result = json.loads(handler)
if (error_type == "1" and not result["success"] == True) or (error_type == "2" and 'error' in result):
if error_type == "1":
print("ERROR " + str(result['error']['code']))
print(result['error']['info'])
elif error_type == "2":
print("ERROR" + str(result['status']))
print(str(result['description']))
else:
to_db = [(key.replace(basecurrencyusedonsite,"",1) if remove_exchange_prefix == "1" else key,reference_date,value) for key,value in result[output_tree].items()]
cur.executemany("INSERT INTO currency (symbol, date, rate) VALUES (?, ?, ?);", to_db)
con.commit()
for key, value in currency_configurations.currencymap.items():
if any(inputvalue in value.lower() for inputvalue in inputvalues) and any(inputsymbol==key.lower() for inputsymbol in inputsymbols):
symbols+=(key + ",")
if len(symbols) == 0:
print("No currency name found. Please try again and check currency configuration for the list of available currency names.")
elif not (any(currency_base==key for key in currency_configurations.currencymap.keys())):
print("Currency base was not found. Please try again and check currency configuration for the list of currency.")
else:
if args.paid_membership:
query = "select symbol, rate from currency where date = '%s'" % reference_date + " and upper(symbol) IN (%s) " % ','.join("'" + symbol + "'" for symbol in symbols[:-1].split(","))
else:
query = "select symbol, rate from currency where date = '%s'" % reference_date + " and upper(symbol) IN (%s " % ','.join("'" + symbol + "'" for symbol in symbols[:-1].split(",")) + ",'%s')" % currency_base
if args.debug:
print("Debug: Running query: " + query)
cur.execute(query)
finalresult = dict(cur.fetchall())
outputlist=[]
denominator = 1
if not args.paid_membership:
denominator=decimal.Decimal(finalresult[currency_base])
for symbol,rate in sorted(finalresult.items()):
if symbol in symbols:
amount=args.amount.quantize(decimal.Decimal("0.01"),decimal.ROUND_HALF_UP)
rate=(decimal.Decimal(rate)/denominator)
reciprocal_rate=(decimal.Decimal(1)/rate)*amount
rate=rate*amount
rate=rate.quantize(decimal.Decimal("0.0000000000000001"),decimal.ROUND_HALF_UP)
reciprocal_rate=reciprocal_rate.quantize(decimal.Decimal("0.0000000000000001"),decimal.ROUND_HALF_UP)
output=""
output += str(currency_configurations.currencymap[symbol]) + "," + str(symbol) + "," + str(reference_date) + "," + "{0:.2f}".format(amount) + "," + "{0:.14f}".format(rate) + "," + "{0:.14f}".format(reciprocal_rate)
outputlist.append(str(output))
return outputlist
now=datetime.datetime.now()
currentdate=now.strftime("%Y-%m-%d")
CLI=argparse.ArgumentParser()
CLI.add_argument(
"serviceprovider",
type=str
)
CLI.add_argument(
"--currencynamelist",
nargs="*",
type=str,
default=list(currency_configurations.currencymap.values())
)
CLI.add_argument(
"--symbollist",
nargs="*",
type=str,
default=list(currency_configurations.currencymap.keys())
)
CLI.add_argument(
"--datelist",
nargs="*",
type=str,
default=[currentdate]
)
CLI.add_argument(
"--amount",
type=decimal.Decimal,
default=decimal.Decimal(1.00)
)
CLI.add_argument(
"--basecurrency",
type=str,
default="EUR"
)
CLI.add_argument(
"--daysinterval",
type=int,
default=0
)
CLI.add_argument(
"--debug",
action='store_true'
)
CLI.add_argument(
"--visual",
action='store_true'
)
CLI.add_argument(
"--paid_membership",
action='store_true'
)
CLI.add_argument(
"--no_header",
action='store_true'
)
CLI.add_argument(
"--sort_by_symbol",
action='store_true'
)
CLI.add_argument(
"--output_fluctuation",
action='store_true'
)
args = CLI.parse_args()
exchange_tree=config.get(args.serviceprovider,'exchange_tree')
variable_apikey=config.get(args.serviceprovider,'variable_apikey')
domain_name=config.get(args.serviceprovider,'domain_name')
value_apikey=config.get(args.serviceprovider,'value_apikey')
error_type=config.get(args.serviceprovider,'error_type')
get_currencylist=config.get(args.serviceprovider,'get_currencylist')
exchange_tree=config.get(args.serviceprovider,'exchange_tree')
variable_basekey=config.get(args.serviceprovider,'variable_basekey')
prefix_get_date=config.get(args.serviceprovider,'prefix_get_date')
remove_exchange_prefix=config.get(args.serviceprovider,'remove_exchange_prefix')
default_currencybase=config.get(args.serviceprovider,'default_currencybase')
output_tree=config.get(args.serviceprovider,'output_tree')
header=""
header1=""
header2=""
decimal.getcontext().prec = 50
if args.visual:
header1= "Currency Name".ljust(40," ") + "Symbol".ljust(12," ") + "Date".rjust(10," ") + "Amount".rjust(12," ") + "Rate".rjust(32," ") + "Reciprocal Rate".rjust(32," ")
header2= "=" * 140
if args.output_fluctuation:
header1+= "Perc. Diff.".rjust(16," ") + "Difference".rjust(32," ")
header2+= "=" * 48
header=header1+"\n"+header2
else:
header="currency_name,symbol,date,amount,rate,reciprocal_rate"
if args.output_fluctuation:
header+=",perc_diff,difference"
rendereddatelist=[]
if args.daysinterval != 0 and len(args.datelist) != 1:
print("Parameter --daysinterval cannot be used when parameter --datelist has more than one date.\nWhen parameter --daysinterval is in use, place only one date in parameter --datelist.")
else:
if args.daysinterval != 0 and len(args.datelist) == 1:
if args.daysinterval > 0:
startdate=validate(args.datelist[0])
enddate=validate(args.datelist[0]) + datetime.timedelta(days=args.daysinterval)
elif args.daysinterval < 0:
enddate=validate(args.datelist[0])
startdate=validate(args.datelist[0]) + datetime.timedelta(days=args.daysinterval)
rendereddatelist = [str((startdate + datetime.timedelta(days=x)).date()) for x in range((enddate-startdate).days + 1)]
else:
rendereddatelist = sorted(args.datelist)
resultlist=[]
fullresultlist=[]
sqlite_file='data/currency_'
sqlite_file += args.serviceprovider.lower() + "_"
if args.paid_membership:
sqlite_file += args.basecurrency.lower() + '.sqlite'
else:
sqlite_file += default_currencybase.lower() + ".sqlite"
con = sqlite3.connect(sqlite_file)
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS currency (symbol, date, rate);")
con.commit()
for i in rendereddatelist:
date_converted = validate(i)
maximum = datetime.date.today()
minimum = datetime.date(1999, 1, 1)
if not (minimum <= date_converted.date() <= maximum):
print("Date " + i + " is out of bounds. Date range must be between " + minimum.strftime("%Y-%m-%d") + " and " + maximum.strftime("%Y-%m-%d") + ".")
elif not (args.amount >= 0.01 and args.amount <= 1000000):
print("Amount " + str(args.amount) + " is out of bounds. Amount range must be between 0.01 and 1000000.00")
else:
resultlist.append(get_currency(i))
if not args.no_header:
print(header)
for datelist in resultlist:
for symbollist in datelist:
fullresultlist.append(symbollist.split(","))
if args.sort_by_symbol or args.output_fluctuation:
fullresultlist = sorted(fullresultlist, key=lambda row:(row[1],row[2]), reverse=False)
lastrate=decimal.Decimal(0)
lastcurrencysymbol=""
for columnlist in fullresultlist:
fluctuationoutput=""
if args.output_fluctuation:
if lastcurrencysymbol == str(columnlist[1]) and lastrate != 0:
perc_diff = (((decimal.Decimal(columnlist[4]) - lastrate) / lastrate)*100)
difference = (decimal.Decimal(columnlist[4]) - lastrate)
else:
perc_diff = decimal.Decimal(0.0)
difference = decimal.Decimal(0.0)
lastcurrencysymbol=str(columnlist[1])
lastrate=decimal.Decimal(columnlist[4])
difference=difference.quantize(decimal.Decimal("0.0000000000000001"),decimal.ROUND_HALF_UP)
perc_diff=perc_diff.quantize(decimal.Decimal("0.0001"),decimal.ROUND_HALF_UP)
if args.visual:
fluctuationoutput="{0:.4f}".format(decimal.Decimal(perc_diff)).rjust(16," ")+"{0:.14f}".format(decimal.Decimal(difference)).rjust(32," ")
else:
fluctuationoutput=","+"{0:.4f}".format(decimal.Decimal(perc_diff))+","+"{0:.14f}".format(decimal.Decimal(difference))
if args.visual:
print(columnlist[0].ljust(40," ") + columnlist[1].ljust(12," ") + columnlist[2].ljust(10," ") + "{0:.2f}".format(decimal.Decimal(columnlist[3])).rjust(12," ") + "{0:.14f}".format(decimal.Decimal(columnlist[4])).rjust(32," ") + "{0:.14f}".format(decimal.Decimal(columnlist[5])).rjust(32," ") + fluctuationoutput)
else:
print(columnlist[0]+","+columnlist[1]+","+columnlist[2]+","+columnlist[3]+","+columnlist[4]+","+columnlist[5]+fluctuationoutput)
con.close()