Compare commits
No commits in common. "872df6777675c4edba5171948b6b72f783c4acc4" and "5702811203e21f9d075a43d308b87c68de9b82fd" have entirely different histories.
872df67776
...
5702811203
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1 @@
|
|||||||
__pycache__
|
__pycache__
|
||||||
cache.json
|
|
||||||
|
34
meuro.py
34
meuro.py
@ -1,26 +1,11 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import requests, json, os.path
|
import requests
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from dateutil import relativedelta
|
from dateutil import relativedelta
|
||||||
from dateutil import parser as dparser
|
from dateutil import parser as dparser
|
||||||
from dateutil.parser._parser import ParserError as DateParserError
|
from dateutil.parser._parser import ParserError as DateParserError
|
||||||
|
|
||||||
_years = None
|
# Ugly code thats outside of any function
|
||||||
|
|
||||||
def _loadYearsTable(maxCacheSeconds=3600):
|
|
||||||
global _years
|
|
||||||
if os.path.isfile('cache.json'):
|
|
||||||
with open('cache.json', 'r') as f:
|
|
||||||
cacheUpdate, cacheYears = json.loads(f.read())
|
|
||||||
if (datetime.now() - dparser.isoparse(cacheUpdate)).total_seconds() < maxCacheSeconds:
|
|
||||||
# JSON does not allow integers as keys; so we convert them back here...
|
|
||||||
cacheYears = {int(y):{int(m):float(n) for m,n in ms.items()} for y,ms in cacheYears.items()}
|
|
||||||
_years = cacheYears
|
|
||||||
return
|
|
||||||
_years = _loadYearsTableWeb()
|
|
||||||
|
|
||||||
def _loadYearsTableWeb():
|
|
||||||
print('[i] Fetching new data from ECB-Servers...')
|
|
||||||
url = 'https://sdw.ecb.europa.eu/quickviewexport.do?SERIES_KEY=122.ICP.M.U2.N.000000.4.ANR&type=csv'
|
url = 'https://sdw.ecb.europa.eu/quickviewexport.do?SERIES_KEY=122.ICP.M.U2.N.000000.4.ANR&type=csv'
|
||||||
resp = requests.get(url)
|
resp = requests.get(url)
|
||||||
lines = resp.text.split('\n')[6:]
|
lines = resp.text.split('\n')[6:]
|
||||||
@ -39,17 +24,11 @@ def _loadYearsTableWeb():
|
|||||||
for month in range(1,13):
|
for month in range(1,13):
|
||||||
if month not in months:
|
if month not in months:
|
||||||
years[year][month] = 1 + 0.02/12 # Lets say the ECB archives their target
|
years[year][month] = 1 + 0.02/12 # Lets say the ECB archives their target
|
||||||
with open('cache.json', 'w') as f:
|
|
||||||
f.write(json.dumps([datetime.now().isoformat(),years]))
|
|
||||||
return years
|
|
||||||
|
|
||||||
# Gives you the exchange rate between euros and meuros for the given date.
|
# Gives you the exchange rate between euros and meuros for the given date.
|
||||||
# (Should always be a float < 1)
|
# (Should always be a float < 1)
|
||||||
# date should be either a datetime-object or None (= current date)
|
# date should be either a datetime-object or None (= current date)
|
||||||
def exchangeRate(date=None):
|
def exchangeRate(date=None):
|
||||||
global _years
|
|
||||||
if _years==None:
|
|
||||||
_loadYearsTable()
|
|
||||||
if date==None:
|
if date==None:
|
||||||
date = datetime.now()
|
date = datetime.now()
|
||||||
month, year = date.month, date.year
|
month, year = date.month, date.year
|
||||||
@ -58,24 +37,23 @@ def exchangeRate(date=None):
|
|||||||
akk = 1
|
akk = 1
|
||||||
for fullYear in range(2001, year):
|
for fullYear in range(2001, year):
|
||||||
yearlyInf = 1
|
yearlyInf = 1
|
||||||
for m in _years[fullYear]:
|
for m in years[fullYear]:
|
||||||
monthlyInf = _years[fullYear][m]
|
monthlyInf = years[fullYear][m]
|
||||||
yearlyInf *= monthlyInf
|
yearlyInf *= monthlyInf
|
||||||
akk *= yearlyInf
|
akk *= yearlyInf
|
||||||
|
|
||||||
for fullMonth in range(1, month):
|
for fullMonth in range(1, month):
|
||||||
monthlyInf = _years[year][fullMonth]
|
monthlyInf = years[year][fullMonth]
|
||||||
akk *= monthlyInf
|
akk *= monthlyInf
|
||||||
|
|
||||||
beginningOfMonth = date.replace(hour=0, minute=0, second=0, microsecond=0, day=1)
|
beginningOfMonth = date.replace(hour=0, minute=0, second=0, microsecond=0, day=1)
|
||||||
endOfMonth = beginningOfMonth + relativedelta.relativedelta(months=1)
|
endOfMonth = beginningOfMonth + relativedelta.relativedelta(months=1)
|
||||||
fracOfMonth = (date - beginningOfMonth).total_seconds() / (endOfMonth - beginningOfMonth).total_seconds()
|
fracOfMonth = (date - beginningOfMonth).total_seconds() / (endOfMonth - beginningOfMonth).total_seconds()
|
||||||
inflationThisMonth = 1 + fracOfMonth * (_years[year][month]-1)
|
inflationThisMonth = 1 + fracOfMonth * (years[year][month]-1)
|
||||||
|
|
||||||
akk *= inflationThisMonth
|
akk *= inflationThisMonth
|
||||||
|
|
||||||
return 1/akk
|
return 1/akk
|
||||||
|
|
||||||
# Converts the given amount of euros to meuros for the given date. (wholeCents means it rounds to two decimal places)
|
# Converts the given amount of euros to meuros for the given date. (wholeCents means it rounds to two decimal places)
|
||||||
# date should be either a datetime-object or None (= current date)
|
# date should be either a datetime-object or None (= current date)
|
||||||
def euroToMeuro(eur,date=None,wholeCents=True):
|
def euroToMeuro(eur,date=None,wholeCents=True):
|
||||||
|
Loading…
Reference in New Issue
Block a user