Skip to content

Commit

Permalink
ver 0.09: API requests are now cached. Added more details on debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
asokratis committed Apr 3, 2018
1 parent bfff731 commit 4c5807c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 44 deletions.
44 changes: 29 additions & 15 deletions README.md

Large diffs are not rendered by default.

Empty file added configurations/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
Empty file added data/.gitignore
Empty file.
82 changes: 54 additions & 28 deletions get_currency.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from urllib.request import urlopen
from urllib.error import URLError
import currency_configurations
import retry_configurations
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

# 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):
Expand Down Expand Up @@ -49,8 +50,28 @@ def get_currency(reference_date):
usersymbol=args.symbollist
access_key=args.apiaccesskey
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:
if args.legacy_user:
url = "https://data.fixer.io/api/" + reference_date + "?access_key=" + access_key + "&base=" + currency_base
else:
url = "http://data.fixer.io/api/" + reference_date + "?access_key=" + access_key + "&base=EUR"
if args.debug:
print("Debug: Calling API to cache data from " + url)
html = urlopen(url)
handler = html.read()
result = json.loads(handler)
if result["success"] == False:
print("ERROR " + str(result['error']['code']))
print(result['error']['info'])
else:
to_db = [(key,reference_date,value) for key,value in result['rates'].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 + ",")
Expand All @@ -60,34 +81,29 @@ def get_currency(reference_date):
print("Currency base was not found. Please try again and check currency configuration for the list of currency.")
else:
if args.legacy_user:
url = "https://data.fixer.io/api/" + reference_date + "?access_key=" + access_key + "&base=" + currency_base + "&date=" + reference_date + "&symbols=" + symbols[:-1]
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:
url = "http://data.fixer.io/api/" + reference_date + "?access_key=" + access_key + "&base=EUR" + "&date=" + reference_date + "&symbols=" + symbols[:-1] + "," + currency_base
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 url - " + url)
html = urlopen(url)
handler = html.read()
result = json.loads(handler)
if result["success"] == False:
print("ERROR " + str(result['error']['code']))
print(result['error']['info'])
else:
outputlist=[]
denominator = 1
if not args.legacy_user:
denominator=decimal.Decimal(result['rates'][currency_base])
for symbol,rate in sorted(result['rates'].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
print("Debug: Running query: " + query)
cur.execute(query)
finalresult = dict(cur.fetchall())
outputlist=[]
denominator = 1
if not args.legacy_user:
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")
Expand Down Expand Up @@ -185,6 +201,15 @@ def get_currency(reference_date):
rendereddatelist = sorted(args.datelist)
resultlist=[]
fullresultlist=[]
sqlite_file='data/currency_'
if args.legacy_user:
sqlite_file += args.basecurrency.lower() + '.sqlite'
else:
sqlite_file += "eur.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()
Expand Down Expand Up @@ -225,3 +250,4 @@ def get_currency(reference_date):
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()
Binary file added images/why_use.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion update_currency_configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
print("ERROR " + str(result['error']['code']))
print(result['error']['info'])
else:
file = open('currency_configurations.py',mode='w',encoding='utf-8')
file = open('configurations/currency_configurations.py',mode='w',encoding='utf-8')
file.write("# -*- coding: utf-8 -*-\n")
file.write("currencymap = {\n")
is_first = True
Expand Down

0 comments on commit 4c5807c

Please sign in to comment.