v0.1.0
This commit is contained in:
90
backend/python/coingecko.py
Normal file
90
backend/python/coingecko.py
Normal file
@ -0,0 +1,90 @@
|
||||
import argparse
|
||||
import httpx
|
||||
import json
|
||||
import datetime
|
||||
|
||||
|
||||
class CoinGecko:
|
||||
def __init__(self, base_url: str = None):
|
||||
if base_url is None:
|
||||
base_url = 'https://api.coingecko.com/api/v3'
|
||||
self.base_url = base_url
|
||||
|
||||
def __build_url(self, sub_url: str, query: str = ''):
|
||||
sub = sub_url
|
||||
if query != '':
|
||||
sub = '?'.join([
|
||||
sub,
|
||||
query
|
||||
])
|
||||
url = '/'.join([
|
||||
self.base_url,
|
||||
sub
|
||||
])
|
||||
return url
|
||||
|
||||
def __get(self, url: str):
|
||||
resp = httpx.get(url)
|
||||
if resp.status_code != httpx.codes.OK:
|
||||
raise Exception(resp.reason_phrase)
|
||||
return json.loads(resp.text)
|
||||
|
||||
def list(self):
|
||||
url = self.__build_url('coins/list')
|
||||
return self.__get(url)
|
||||
|
||||
def get(self, ids: tuple, currencies: tuple, last_updated: bool = True):
|
||||
sub = 'simple/price'
|
||||
query = '&'.join([
|
||||
'='.join(['ids', ','.join(ids)]),
|
||||
'='.join(['vs_currencies', ','.join(currencies)]),
|
||||
'='.join(['include_last_updated_at', 'true' if last_updated else 'false'])
|
||||
])
|
||||
url = self.__build_url(sub, query)
|
||||
res = self.__get(url)
|
||||
for k, d in res.items():
|
||||
res[k]['last_updated_at'] = datetime.datetime.fromtimestamp(d['last_updated_at'])\
|
||||
.strftime('%Y-%m-%d %H:%M:%S.%f%z')
|
||||
return res
|
||||
|
||||
def historical(self, id_: str, currency: str, from_: str, to: str):
|
||||
sub = '/'.join([
|
||||
'coins',
|
||||
id_,
|
||||
'market_chart',
|
||||
'range'
|
||||
])
|
||||
query = '&'.join([
|
||||
'='.join(['vs_currency', currency]),
|
||||
'='.join(['from', from_]),
|
||||
'='.join(['to', to])
|
||||
])
|
||||
url = self.__build_url(sub, query)
|
||||
res = self.__get(url)
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-u', '--url')
|
||||
parser.add_argument('-i', '--ids', type=str)
|
||||
parser.add_argument('-c', '--currencies', type=str)
|
||||
hist = parser.add_subparsers()
|
||||
hparser = hist.add_parser('hist')
|
||||
hparser.add_argument('-hi', '--historical', action='store_true')
|
||||
hparser.add_argument('-f', '--from_')
|
||||
hparser.add_argument('-t', '--to')
|
||||
args = parser.parse_args()
|
||||
cg = CoinGecko(args.url)
|
||||
_ids = tuple(args.ids.split(','))
|
||||
_currencies = tuple(args.currencies.split(','))
|
||||
if 'historical' in args and args.historical:
|
||||
from_ = args.from_
|
||||
if '-' in from_:
|
||||
from_ = str(datetime.datetime.fromisoformat(from_).timestamp())
|
||||
to = args.to
|
||||
if '-' in to:
|
||||
to = str(datetime.datetime.fromisoformat(to).timestamp())
|
||||
print(cg.historical(id_=_ids[0], currency=_currencies[0], from_=from_, to=to))
|
||||
exit()
|
||||
print(cg.get(ids=_ids, currencies=_currencies))
|
Reference in New Issue
Block a user