You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.9 KiB
112 lines
3.9 KiB
#! /bin/python3
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
data_to_export = []
|
|
interrupt = False
|
|
while next_page_available == True and interrupt == False :
|
|
# import pdb; pdb.set_trace()
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
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())
|
|
|