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. 108
      export_csv.py
  2. 1
      requirements.txt

108
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():
arg_count = len(sys.argv) def __init__(self):
max_records = -1 self.trades_count = None
if arg_count == 2: self.filename = None
# print("A max count of", sys.argv[1] ,"has been provided") def setFilename(self, filename):
max_records = int(sys.argv[1]) self.filename = filename
def setTradesToExportCount(self, count):
self.trades_count = count
if "LB_HMAC_KEY" in os.environ and "LB_HMAC_SECRET" in os.environ : 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_key = os.environ["LB_HMAC_KEY"]
hmac_secret = os.environ["LB_HMAC_SECRET"] hmac_secret = os.environ["LB_HMAC_SECRET"]
else : else :
print("LB_HMAC_KEY and LB_HMAC_SECRET env variables must be set") print("LB_HMAC_KEY and LB_HMAC_SECRET env variables must be set")
exit(1) exit(1)
conn = api.hmac(hmac_key, hmac_secret)
conn = api.hmac(hmac_key, hmac_secret) exported_count = 0
exported_count = 0
# print("About to call the API for the first time") # 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'} 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() api_res = conn.call('GET', '/api/dashboard/released/seller/', params = params).json()
trades = api_res["data"]["contact_list"] trades = api_res["data"]["contact_list"]
next_page_available = True next_page_available = True
while next_page_available == True : data_to_export = []
interrupt = False
while next_page_available == True and interrupt == False :
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
for trade in trades: for trade in trades:
data = trade["data"] data = trade["data"]
print(data["released_at"] + "," + str(data["contact_id"]) + "," + data["amount"]) data_to_export.append(
[
data["released_at"],
str(data["contact_id"]),
data["amount"]
]
)
exported_count = exported_count + 1 exported_count = exported_count + 1
if max_records != -1 and exported_count == max_records: if max_records != -1 and exported_count == max_records:
exit(0) print("We've reached the max records to export at:" + str(exported_count))
interrupt = True
break
if interrupt == True:
break
if "pagination" in api_res and "next" in api_res["pagination"] : if "pagination" in api_res and "next" in api_res["pagination"] :
# print("We have a next page!") print("We have a next page")
pagination_data = urlparse(api_res["pagination"]["next"]) pagination_data = urlparse(api_res["pagination"]["next"])
next_params = parse_qs(pagination_data.query) next_params = parse_qs(pagination_data.query)
api_res = conn.call('GET', '/api/dashboard/released/seller/', params = next_params).json() api_res = conn.call('GET', '/api/dashboard/released/seller/', params = next_params).json()
trades = api_res["data"]["contact_list"] trades = api_res["data"]["contact_list"]
else : else :
# print("No next page available!") print("No next page available")
next_page_available = False 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)
class LbExportCommand(pycommand.CommandBase):
'''An example of a basic CLI program'''
usagestr = 'usage: basic-example [options]'
description = __doc__
optionList = (
('help', ('h', False, 'show this help information')),
('file', ('f', '<filename>', 'output to specified file')),
('tradescount', ('n', '<count>', 'how many trades to output')),
)
def run(self):
if self.flags.help:
print(self.usage)
return 0
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())

1
requirements.txt

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

Loading…
Cancel
Save