-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
68 lines (54 loc) · 1.88 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
61
62
63
64
65
66
67
68
import uvicorn
import json
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi_utils.tasks import repeat_every
from stats_utils import get_availiable_pairs, summary_for_pair, ticker_for_pair, orderbook_for_pair, trades_for_pair, atomicdex_info, get_data_from_gecko
path_to_db = '/DB/43ec929fe30ee72be42c9162c56dde910a05e50d/MM2.db'
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
available_pairs = get_availiable_pairs(path_to_db)
@app.on_event("startup")
@repeat_every(seconds=60) # caching data every minute
def cache_gecko_data():
gecko_data = get_data_from_gecko()
try:
with open('gecko_cache.json', 'w') as json_file:
json_file.write(json.dumps(gecko_data))
except Exception as e:
print(e)
print("saved gecko data to file")
@app.get('/api/v1/summary')
def summary():
available_pairs = get_availiable_pairs(path_to_db)
summary_data = []
for pair in available_pairs:
summary_data.append(summary_for_pair(pair, path_to_db))
return summary_data
@app.get('/api/v1/ticker')
def ticker():
available_pairs = get_availiable_pairs(path_to_db)
ticker_data = []
for pair in available_pairs:
ticker_data.append(ticker_for_pair(pair, path_to_db))
return ticker_data
@app.get('/api/v1/orderbook/{market_pair}')
def orderbook(market_pair="KMD_BTC"):
orderbook_data = orderbook_for_pair(market_pair)
return orderbook_data
@app.get('/api/v1/trades/{market_pair}')
def trades(market_pair="KMD_BTC"):
trades_data = trades_for_pair(market_pair, path_to_db)
return trades_data
@app.get('/api/v1/atomicdexio')
def atomicdex_info_api():
data = atomicdex_info(path_to_db)
return data
if __name__ == '__main__':
uvicorn.run("main:app", host="0.0.0.0", port=8080)