-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.py
60 lines (47 loc) · 1.66 KB
/
main.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
import json
import random
import time
from datetime import datetime, timezone, timedelta
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
from config.builder import Builder
from config.config import config
from logs import logger
from presentation.observer import Observable
DATA_SLICE_DAYS = 1
DATETIME_FORMAT = "%Y-%m-%dT%H:%M"
def get_dummy_data():
logger.info('Generating dummy data')
def fetch_prices():
logger.info('Fetching prices')
timeslot_end = datetime.now(timezone.utc)
end_date = timeslot_end.strftime(DATETIME_FORMAT)
start_data = (timeslot_end - timedelta(days=DATA_SLICE_DAYS)).strftime(DATETIME_FORMAT)
url = f'https://production.api.coindesk.com/v2/price/values/{config.currency}?ohlc=true&start_date={start_data}&end_date={end_date}'
req = Request(url)
data = urlopen(req).read()
external_data = json.loads(data)
prices = [entry[1:] for entry in external_data['data']['entries']]
return prices
def main():
logger.info('Initialize')
data_sink = Observable()
builder = Builder(config)
builder.bind(data_sink)
try:
while True:
try:
prices = [entry[1:] for entry in get_dummy_data()] if config.dummy_data else fetch_prices()
data_sink.update_observers(prices)
time.sleep(config.refresh_interval)
except (HTTPError, URLError) as e:
logger.error(str(e))
time.sleep(5)
except IOError as e:
logger.error(str(e))
except KeyboardInterrupt:
logger.info('Exit')
data_sink.close()
exit()
if __name__ == "__main__":
main()