-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.py
35 lines (29 loc) · 1.35 KB
/
jobs.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
import urllib.request, json
from models import PriceHistory, update_price
from datetime import datetime
import os
# narrowing down to specific markets
supported_exchanges = os.environ.get("SUPPORTED_EXCHANGES").split(',')
def update_price_history(db):
'''Updates currency pair prices'''
has_more = True
last_cursor = None
while(has_more):
url = f'{os.environ.get("API_BASE_URL")}/markets/prices?api_key={os.environ.get("API_KEY")}'
if(last_cursor):
url = f'{url}&cursor={last_cursor}'
response = json.loads(urllib.request.urlopen(url).read())
results = response['result']
for symbol in results:
# symbol looks something like "market:binance-us:1inchusd" where the actual symbol is the last portion
symbols_details = symbol.split(':')
exchange = symbols_details [1]
parsed_symbol = symbols_details [2]
if exchange in supported_exchanges:
'''Exclude exchanges we don't care about'''
history_item = PriceHistory(parsed_symbol, exchange, results[symbol], datetime.now())
update_price(db, history_item)
cursor = response['cursor']
has_more = cursor['hasMore']
last_cursor = cursor['last']
print(f'Completed updating prices at { str(datetime.now())}', flush="True")