Browse Source

Added proper csv lib usage and extra dep for easier command line args handling

master
rodley82 3 years ago
parent
commit
64317b33f0
  1. 130
      export_csv.py
  2. 3
      requirements.txt

130
export_csv.py

@ -2,53 +2,111 @@
from lbcapi import api from lbcapi import api
import sys import sys
import csv
import pycommand
import os import os
from urllib.parse import urlparse from urllib.parse import urlparse
from urllib.parse import parse_qs from urllib.parse import parse_qs
import pdb 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) exported_count = 0
max_records = -1
if arg_count == 2:
# print("A max count of", sys.argv[1] ,"has been provided")
max_records = int(sys.argv[1])
# 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 : trades = api_res["data"]["contact_list"]
hmac_key = os.environ["LB_HMAC_KEY"] next_page_available = True
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) 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") if interrupt == True:
params = {'order_by': ['-closed_at'], 'fields': 'released_at,contact_id,amount, closed_at, canceled_at'} break
api_res = conn.call('GET', '/api/dashboard/released/seller/', params = params).json() 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"] class LbExportCommand(pycommand.CommandBase):
next_page_available = True '''An example of a basic CLI program'''
usagestr = 'usage: basic-example [options]'
description = __doc__
while next_page_available == True : optionList = (
# import pdb; pdb.set_trace() ('help', ('h', False, 'show this help information')),
('file', ('f', '<filename>', 'output to specified file')),
('tradescount', ('n', '<count>', 'how many trades to output')),
)
for trade in trades: def run(self):
data = trade["data"] if self.flags.help:
print(data["released_at"] + "," + str(data["contact_id"]) + "," + data["amount"]) print(self.usage)
return 0
exported_count = exported_count + 1 export = LbCsvExport()
if max_records != -1 and exported_count == max_records: if self.flags.file:
exit(0) export.setFilename(self.flags.file)
if "pagination" in api_res and "next" in api_res["pagination"] : if self.flags.tradescount:
# print("We have a next page!") export.setTradesToExportCount(self.flags.tradescount)
pagination_data = urlparse(api_res["pagination"]["next"])
next_params = parse_qs(pagination_data.query) print("Starting exporting")
api_res = conn.call('GET', '/api/dashboard/released/seller/', params = next_params).json() export.run()
trades = api_res["data"]["contact_list"] print("Finished exporting")
else :
# print("No next page available!") if __name__ == '__main__':
next_page_available = False cmd = LbExportCommand(sys.argv[1:])
if cmd.error:
print('error: {0}'.format(cmd.error))
sys.exit(1)
else:
sys.exit(cmd.run())

3
requirements.txt

@ -1 +1,2 @@
lbcapi==1.0.3 lbcapi==1.0.3
pycommand==0.4.0

Loading…
Cancel
Save