commit f0965547e996bf1e0346f164f92515c90a562e30 Author: rodley82 Date: Sun Dec 25 22:34:58 2022 -0300 Version inicial funcionando diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea6909c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode +test +venv +*.csv diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f30904e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM python:3.9 + +RUN /usr/local/bin/python -m pip install --upgrade pip + +ENV PROJECT_USER=lbapi +ENV PROJECT_USER_UID=1000 +ENV PROJECT_USER_GID=1000 +ENV PROJECT_USER_HOME=/home/${PROJECT_USER} +ENV PROJECT_USER_APP_HOME=${PROJECT_USER_HOME}/lbapi + +RUN groupadd -r --gid ${PROJECT_USER_GID} ${PROJECT_USER} +RUN useradd --system --no-log-init --uid ${PROJECT_USER_UID} --gid ${PROJECT_USER_GID} --home-dir ${PROJECT_USER_HOME} --create-home --shell /bin/bash ${PROJECT_USER} + +RUN mkdir -p ${PROJECT_USER_APP_HOME} && chown -R ${PROJECT_USER}:${PROJECT_USER} ${PROJECT_USER_APP_HOME} + +USER ${PROJECT_USER} + +WORKDIR ${PROJECT_USER_APP_HOME} +COPY --chown=${PROJECT_USER_UID}:${PROJECT_USER_GID} requirements.txt export_csv.py ./ +RUN pip3 install -r requirements.txt + +CMD ["python3", "export_csv.py"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..5a32afa --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# CSV Export - Localbitcoins + +Exports your trades as a seller out onto CSV to stdout + +``` +virtualenv venv +source venv/bin/activate +pip3 install -r requirements.txt +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2fc7540 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3' +services: + lb_csv_export: + build: . + image: "lb_csv_export" + environment: + - LB_HMAC_KEY=LB_HMAC_KEY + - LB_HMAC_SECRET=LB_HMAC_SECRET + diff --git a/export_csv.py b/export_csv.py new file mode 100755 index 0000000..d231727 --- /dev/null +++ b/export_csv.py @@ -0,0 +1,54 @@ +#! /bin/python3 + +from lbcapi import api +import sys +import os +from urllib.parse import urlparse +from urllib.parse import parse_qs + +import pdb + +arg_count = len(sys.argv) +max_records = -1 +if arg_count == 2: + # print("A max count of", sys.argv[1] ,"has been provided") + max_records = int(sys.argv[1]) + + +if "LB_HMAC_KEY" in os.environ and "LB_HMAC_SECRET" in os.environ : + hmac_key = os.environ["LB_HMAC_KEY"] + hmac_secret = os.environ["LB_HMAC_SECRET"] +else : + print("LB_HMAC_KEY and LB_HMAC_SECRET env variables must be set") + exit(1) + +conn = api.hmac(hmac_key, hmac_secret) + +exported_count = 0 + +# print("About to call the API for the first time") +params = {'order_by': ['-closed_at'], 'fields': 'released_at,contact_id,amount, closed_at, canceled_at'} +api_res = conn.call('GET', '/api/dashboard/released/seller/', params = params).json() + +trades = api_res["data"]["contact_list"] +next_page_available = True + +while next_page_available == True : + # import pdb; pdb.set_trace() + + for trade in trades: + data = trade["data"] + print(data["released_at"] + "," + str(data["contact_id"]) + "," + data["amount"]) + + exported_count = exported_count + 1 + if max_records != -1 and exported_count == max_records: + exit(0) + if "pagination" in api_res and "next" in api_res["pagination"] : + # print("We have a next page!") + pagination_data = urlparse(api_res["pagination"]["next"]) + next_params = parse_qs(pagination_data.query) + api_res = conn.call('GET', '/api/dashboard/released/seller/', params = next_params).json() + trades = api_res["data"]["contact_list"] + else : + # print("No next page available!") + next_page_available = False diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..60b7c94 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +lbcapi==1.0.3 \ No newline at end of file