From 64317b33f0fd231bce17a19332332f3749d9deba Mon Sep 17 00:00:00 2001 From: rodley82 Date: Sun, 1 Jan 2023 12:13:56 -0300 Subject: [PATCH] Added proper csv lib usage and extra dep for easier command line args handling --- export_csv.py | 130 ++++++++++++++++++++++++++++++++++------------- requirements.txt | 3 +- 2 files changed, 96 insertions(+), 37 deletions(-) diff --git a/export_csv.py b/export_csv.py index d231727..97f8fb1 100755 --- a/export_csv.py +++ b/export_csv.py @@ -2,53 +2,111 @@ from lbcapi import api import sys +import csv +import pycommand import os from urllib.parse import urlparse from urllib.parse import parse_qs - import pdb +class LbCsvExport(): + def __init__(self): + self.trades_count = None + self.filename = None + def setFilename(self, filename): + self.filename = filename + def setTradesToExportCount(self, count): + self.trades_count = count + def run(self): + # arg_count = len(sys.argv) + max_records = -1 + if self.trades_count != None: + max_records = int(self.trades_count) + print("A max count of", max_records ,"has been provided") + 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) -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]) + 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() -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) + trades = api_res["data"]["contact_list"] + next_page_available = True -conn = api.hmac(hmac_key, hmac_secret) + data_to_export = [] + interrupt = False + while next_page_available == True and interrupt == False : + # import pdb; pdb.set_trace() -exported_count = 0 + for trade in trades: + data = trade["data"] + data_to_export.append( + [ + data["released_at"], + str(data["contact_id"]), + data["amount"] + ] + ) + exported_count = exported_count + 1 + if max_records != -1 and exported_count == max_records: + print("We've reached the max records to export at:" + str(exported_count)) + interrupt = True + break -# 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() + if interrupt == True: + break + 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 + print("Writing to " + self.filename) + with open(self.filename, 'w', newline='') as csvfile: + tradeswriter = csv.writer(csvfile, delimiter=',', + quotechar='|', quoting=csv.QUOTE_MINIMAL) + for data in data_to_export: + tradeswriter.writerow(data) -trades = api_res["data"]["contact_list"] -next_page_available = True +class LbExportCommand(pycommand.CommandBase): + '''An example of a basic CLI program''' + usagestr = 'usage: basic-example [options]' + description = __doc__ -while next_page_available == True : - # import pdb; pdb.set_trace() + optionList = ( + ('help', ('h', False, 'show this help information')), + ('file', ('f', '', 'output to specified file')), + ('tradescount', ('n', '', 'how many trades to output')), + ) - for trade in trades: - data = trade["data"] - print(data["released_at"] + "," + str(data["contact_id"]) + "," + data["amount"]) + def run(self): + if self.flags.help: + print(self.usage) + return 0 - 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 + export = LbCsvExport() + if self.flags.file: + export.setFilename(self.flags.file) + if self.flags.tradescount: + export.setTradesToExportCount(self.flags.tradescount) + + print("Starting exporting") + export.run() + print("Finished exporting") + +if __name__ == '__main__': + cmd = LbExportCommand(sys.argv[1:]) + if cmd.error: + print('error: {0}'.format(cmd.error)) + sys.exit(1) + else: + sys.exit(cmd.run()) diff --git a/requirements.txt b/requirements.txt index 60b7c94..6647e63 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -lbcapi==1.0.3 \ No newline at end of file +lbcapi==1.0.3 +pycommand==0.4.0