-
Notifications
You must be signed in to change notification settings - Fork 0
/
market_prices.py
executable file
·104 lines (88 loc) · 3.35 KB
/
market_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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Script by https://github.com/barrucadu/hledger-scripts
from html.parser import HTMLParser
import json
import sys
import time
import urllib.request
from datetime import date, timedelta
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
def get_coinbase(base, currency):
req = urllib.request.Request(
"https://api.coinbase.com/v2/prices/{}-{}/spot/".format(base, currency),
headers={"CB-VERSION": "2018-05-25"})
with urllib.request.urlopen(req) as response:
resp = json.load(response)
return resp['data']['amount']
def ft_find_price(url, currency):
class FTPriceFinder(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.found = None
self.isnext = False
def handle_data(self, data):
if data == 'Price ({})'.format(currency):
self.isnext = True
elif self.isnext:
self.found = data
self.isnext = False
req = urllib.request.Request(url)
with urllib.request.urlopen(req) as response:
html = response.read().decode('utf-8')
finder = FTPriceFinder()
finder.feed(html)
if finder.found is None:
raise Exception("could not find price")
else:
return finder.found
def get_ft_currency(base, currency):
return ft_find_price(
"https://markets.ft.com/data/currencies/tearsheet/summary?s={}{}".format(base, currency),
currency)
def get_ft_fund(isin, currency):
return ft_find_price(
"https://markets.ft.com/data/funds/tearsheet/summary?s={}:{}".format(isin, currency),
currency)
config = json.load(sys.stdin)
symbols = config.get('symbols', {})
for commodity, cconfig in config.get('commodities', {}).items():
try:
try:
provider = cconfig['provider']
except KeyError:
raise Exception("missing provider")
currency = cconfig.get('currency', 'PLN')
if provider == 'coinbase':
rate = get_coinbase(
cconfig.get('base', commodity),
currency)
elif provider == 'ft_currency':
rate = get_ft_currency(
cconfig.get('base', commodity),
currency)
elif provider == 'ft_fund':
rate = get_ft_fund(
cconfig.get('isin', commodity),
currency)
else:
raise Exception("unknown provider '{}'".format(provider))
#if sys.argv[1]:
# try:
# date = time.strftime(sys.argv[1])
# except:
#date = time.strftime('%Y-%m-%d')
start_date = date(2013, 1, 1)
end_date = date(2022, 2, 1)
for single_date in daterange(start_date, end_date):
if currency in symbols:
#print('P {} {} {}{}'.format(date, commodity, symbols[currency], rate))
print('P {} {} {}{}'.format(single_date, commodity, symbols[currency], rate))
else:
#print('P {} {} {} {}'.format(date, commodity, rate, currency))
print('P {} {} {} {}'.format(single_date, commodity, rate, currency))
except Exception as e:
print("; error processing commodity '{}': {}".format(commodity, e))