Logging for python files

This commit is contained in:
2021-07-07 08:56:05 -04:00
parent 8c870cb43f
commit c930ffc55e
3 changed files with 39 additions and 17 deletions

View File

@ -1,10 +1,10 @@
FROM continuumio/miniconda3 as build FROM continuumio/miniconda3 as build
WORKDIR /app WORKDIR /app/
COPY ./python /app/src COPY ./python/ /app/src/
RUN conda env create -f src/environment.yml RUN conda env create -f ./src/environment.yml
#RUN echo "conda activate cryptos" >> ~/.bashrc #RUN echo "conda activate cryptos" >> ~/.bashrc
#SHELL ["/bin/bash", "--login", "-c"] #SHELL ["/bin/bash", "--login", "-c"]
@ -12,7 +12,7 @@ RUN conda env create -f src/environment.yml
RUN conda install -c conda-forge conda-pack RUN conda install -c conda-forge conda-pack
RUN conda pack -n cryptos -o /tmp/env.tar && \ RUN conda pack -n cryptos -o /tmp/env.tar && \
mkdir /venv && cd /venv && tar xf /tmp/env.tar && \ mkdir /venv/ && cd /venv/ && tar xf /tmp/env.tar && \
rm /tmp/env.tar rm /tmp/env.tar
RUN /venv/bin/conda-unpack RUN /venv/bin/conda-unpack
@ -20,18 +20,18 @@ RUN /venv/bin/conda-unpack
FROM python:buster as runtime FROM python:buster as runtime
WORKDIR /app WORKDIR /app/
COPY ./python /app/src COPY ./python/ /app/src/
COPY ./api/bin /app/bin COPY ./automation/bin/ /app/bin/
COPY --from=build /venv /venv COPY --from=build /venv/ /venv/
SHELL ["/bin/bash", "-c"] SHELL ["/bin/bash", "-c"]
RUN pip install pyinstaller RUN pip install pyinstaller
RUN pyinstaller -F -n coingecko --clean --log-level DEBUG --distpath /app/bin /app/src/coingecko.py && \ RUN pyinstaller -F -n coingecko --clean --log-level DEBUG --distpath /app/bin/ /app/src/coingecko.py && \
pyinstaller -F -n mindicador --clean --log-level DEBUG --distpath /app/bin /app/src/miindicador.py pyinstaller -F -n mindicador --clean --log-level DEBUG --distpath /app/bin/ /app/src/miindicador.py
ENTRYPOINT [ "/bin/bash" ] ENTRYPOINT [ "/bin/bash" ]

View File

@ -6,6 +6,8 @@ import datetime
class CoinGecko: class CoinGecko:
def __init__(self, base_url: str = None): def __init__(self, base_url: str = None):
print('Creating object CoinGecko')
if base_url is None: if base_url is None:
base_url = 'https://api.coingecko.com/api/v3' base_url = 'https://api.coingecko.com/api/v3'
self.base_url = base_url self.base_url = base_url
@ -30,10 +32,14 @@ class CoinGecko:
return json.loads(resp.text) return json.loads(resp.text)
def list(self): def list(self):
print('Getting list of coins from {}'.format(self.base_url))
url = self.__build_url('coins/list') url = self.__build_url('coins/list')
return self.__get(url) return self.__get(url)
def get(self, ids: tuple, currencies: tuple, last_updated: bool = True): def get(self, ids: tuple, currencies: tuple, last_updated: bool = True):
print('Getting {} in {} from {}'.format(ids, currencies, self.base_url))
sub = 'simple/price' sub = 'simple/price'
query = '&'.join([ query = '&'.join([
'='.join(['ids', ','.join(ids)]), '='.join(['ids', ','.join(ids)]),
@ -48,6 +54,8 @@ class CoinGecko:
return res return res
def historical(self, id_: str, currency: str, from_: str, to: str): def historical(self, id_: str, currency: str, from_: str, to: str):
print('Getting historical data for {} in {} from {} to {} from {}'.format(id_, currency, from_, to, self.base_url))
sub = '/'.join([ sub = '/'.join([
'coins', 'coins',
id_, id_,
@ -75,16 +83,19 @@ if __name__ == '__main__':
hparser.add_argument('-f', '--from_') hparser.add_argument('-f', '--from_')
hparser.add_argument('-t', '--to') hparser.add_argument('-t', '--to')
args = parser.parse_args() args = parser.parse_args()
print('Called with args: {}'.format(vars(args)))
cg = CoinGecko(args.url) cg = CoinGecko(args.url)
_ids = tuple(args.ids.split(',')) _ids = tuple(args.ids.split(','))
_currencies = tuple(args.currencies.split(',')) _currencies = tuple(args.currencies.split(','))
if 'historical' in args and args.historical: if 'historical' in args and args.historical:
from_ = args.from_ from__ = args.from_
if '-' in from_: if '-' in from__:
from_ = str(datetime.datetime.fromisoformat(from_).timestamp()) from__ = str(datetime.datetime.fromisoformat(from__).timestamp())
to = args.to to_ = args.to
if '-' in to: if '-' in to_:
to = str(datetime.datetime.fromisoformat(to).timestamp()) to_ = str(datetime.datetime.fromisoformat(to_).timestamp())
print(cg.historical(id_=_ids[0], currency=_currencies[0], from_=from_, to=to)) print(cg.historical(id_=_ids[0], currency=_currencies[0], from_=from__, to=to_))
exit() exit()
print(cg.get(ids=_ids, currencies=_currencies)) print(cg.get(ids=_ids, currencies=_currencies))

View File

@ -6,6 +6,8 @@ import datetime
class MiIndicador: class MiIndicador:
def __init__(self, base_url: str = None): def __init__(self, base_url: str = None):
print('Creating object Mindicador')
if base_url is None: if base_url is None:
base_url = 'https://mindicador.cl/api' base_url = 'https://mindicador.cl/api'
self.base_url = base_url self.base_url = base_url
@ -30,10 +32,14 @@ class MiIndicador:
return json.loads(resp.text) return json.loads(resp.text)
def list(self): def list(self):
print('List possible indicators')
url = self.__build_url('') url = self.__build_url('')
return self.__get(url) return self.__get(url)
def get(self, indicador: str, fecha: str = None): def get(self, indicador: str, fecha: str = None):
print('Getting {} in date {} from {}'.format(indicador, fecha, self.base_url))
url = indicador url = indicador
if fecha is not None: if fecha is not None:
url = '/'.join([url, fecha]) url = '/'.join([url, fecha])
@ -45,6 +51,8 @@ class MiIndicador:
return res return res
def historical(self, indicador: str, since: str = None): def historical(self, indicador: str, since: str = None):
print('Getting historical data for {} since {} from {}'.format(indicador, since, self.base_url))
sub = indicador sub = indicador
if since is not None: if since is not None:
sub = '/'.join([sub, since]) sub = '/'.join([sub, since])
@ -65,6 +73,9 @@ if __name__ == '__main__':
hparser.add_argument('-hi', '--historical', action='store_true') hparser.add_argument('-hi', '--historical', action='store_true')
hparser.add_argument('-s', '--since') hparser.add_argument('-s', '--since')
args = parser.parse_args() args = parser.parse_args()
print('Called with args: {}'.format(vars(args)))
mi = MiIndicador(args.url) mi = MiIndicador(args.url)
if 'historical' in args and args.historical: if 'historical' in args and args.historical:
print(mi.historical(args.indicador, args.since)) print(mi.historical(args.indicador, args.since))