This commit is contained in:
2021-06-28 23:15:13 -04:00
parent 0061a3d920
commit f4a8db56ff
93 changed files with 2422 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View 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))

View File

@ -0,0 +1,10 @@
name: cryptos
channels:
- defaults
dependencies:
- httpx=0.17.1
- pip=21.1.2
- python=3.9.5
- setuptools=52.0.0
# - pip:
# - pyinstaller==4.3

View File

@ -0,0 +1,72 @@
import argparse
import httpx
import json
import datetime
class MiIndicador:
def __init__(self, base_url: str = None):
if base_url is None:
base_url = 'https://mindicador.cl/api'
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('')
return self.__get(url)
def get(self, indicador: str, fecha: str = None):
url = indicador
if fecha is not None:
url = '/'.join([url, fecha])
url = self.__build_url(url)
res = self.__get(url)
for i, item in enumerate(res['serie']):
res['serie'][i]['fecha'] = datetime.datetime.fromisoformat(item['fecha'].replace('T', ' ').replace('Z', ''))\
.strftime('%Y-%m-%d')
return res
def historical(self, indicador: str, since: str = None):
sub = indicador
if since is not None:
sub = '/'.join([sub, since])
url = self.__build_url(sub)
res = self.__get(url)
for i, item in enumerate(res['serie']):
res['serie'][i]['fecha'] = datetime.datetime.fromisoformat(item['fecha'].replace('T', ' ').replace('Z', ''))\
.strftime('%Y-%m-%d')
return res
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url')
parser.add_argument('-i', '--indicador')
hist = parser.add_subparsers()
hparser = hist.add_parser('hist')
hparser.add_argument('-hi', '--historical', action='store_true')
hparser.add_argument('-s', '--since')
args = parser.parse_args()
mi = MiIndicador(args.url)
if 'historical' in args and args.historical:
print(mi.historical(args.indicador, args.since))
exit()
print(mi.get(args.indicador))

View File

@ -0,0 +1,30 @@
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
altgraph=0.17=pypi_0
ca-certificates=2021.5.25=haa95532_1
certifi=2021.5.30=py39haa95532_0
future=0.18.2=pypi_0
h11=0.12.0=pyhd3eb1b0_0
h2=4.0.0=py39haa95532_3
hpack=4.0.0=py_0
httpcore=0.12.3=pyhd3eb1b0_0
httpx=0.17.1=pyhd3eb1b0_0
hyperframe=6.0.1=pyhd3eb1b0_0
idna=2.10=pyhd3eb1b0_0
openssl=1.1.1k=h2bbff1b_0
pefile=2021.5.24=pypi_0
pip=21.1.2=py39haa95532_0
pyinstaller=4.3=pypi_0
pyinstaller-hooks-contrib=2021.1=pypi_0
python=3.9.5=h6244533_3
pywin32-ctypes=0.2.0=pypi_0
rfc3986=1.4.0=py_0
setuptools=52.0.0=py39haa95532_0
sniffio=1.2.0=py39haa95532_1
sqlite=3.35.4=h2bbff1b_0
tzdata=2020f=h52ac0ba_0
vc=14.2=h21ff451_1
vs2015_runtime=14.27.29016=h5e58377_2
wheel=0.36.2=pyhd3eb1b0_0
wincertstore=0.2=py39h2bbff1b_0