-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFBAInvoicesDL.py
More file actions
90 lines (80 loc) · 3.33 KB
/
Copy pathFBAInvoicesDL.py
File metadata and controls
90 lines (80 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import argparse
import requests
############# Get a list of orderIDs which meet some "params" constraints (limit, orderStatus, sort, etc.)
def get_order_list(cookie, params, dl=False):
url = "https://sellercentral.amazon.fr/orders-api/search?" + params
#print(url)
headers = {'cookie':cookie}
response = requests.get(url, headers=headers)
jsonres = response.json()
errors = []
orderlist = []
for i in jsonres["orders"]:
if (dl==True):
print("------------- " + i["sellerOrderId"])
res = download_pdf_from_orderid(i["sellerOrderId"], cookie)
if (res != 0): #retry
res = download_pdf_from_orderid(i["sellerOrderId"], cookie)
if (res !=0):
errors.append(i["sellerOrderId"])
else:
orderlist.append(i["sellerOrderId"])
else:
orderlist.append(i["sellerOrderId"])
return orderlist, errors
############# Dowload PDF invoices
def download_pdf_from_orderid(orderid, cookie):
host = "https://sellercentral.amazon.fr/"
path = "taxdocument/api/get-entity?orderId=" + orderid
headers = {'cookie':cookie}
url = host + path
#print(url)
try:
pdflocation = requests.get(url, headers=headers, timeout=10).json()["taxDocuments"][0]["location"]
#print(pdflocation)
except Exception as e:
print("pdflocation failed. " + orderid + " invoice not downloaded")
print(e)
return -1
url = host + pdflocation
try:
pdfurl = requests.get(url, headers=headers, timeout=10).url.replace("%2F", "/").replace("%3A", ":").split("=")[2].split("&openid.identity")[0]
print(pdfurl)
except Exception as e:
print("pdfurl failed. " + orderid + " invoice not downloaded")
print(e)
return -1
try:
pdf = requests.get(pdfurl, headers=headers, timeout=15)
except Exception as e:
print("pdf failed. " + orderid + " invoice not downloaded")
print(e)
return -1
filename = "Invoice " + orderid + ".pdf"
try:
with open(filename, "wb") as f:
f.write(pdf.content)
except Exception as e:
print("pdf write failed. " + orderid + " invoice not written on FS")
print(e)
return -1
return 0
def main():
parser = argparse.ArgumentParser(
prog = 'FBAInvoicesHelper',
description = 'Get FBA invoices',
epilog = '')
parser.add_argument('cookie', help='Session cookie')
parser.add_argument('params', help='Search constraints (ex: limit=30&sort=order_date_desc)')
parser.add_argument('-dl', '--download', action='store_true', help='Flag to enable download of invoices')
args = parser.parse_args()
if ("cookie" in args and "params"):
print("\n")
orders,errors = get_order_list(args.cookie, args.params, dl=True if (args.download) else False)
print("\n\n------------- Summary: " + str(len(orders)-len(errors)) + "/" + str(len(orders)) + " invoices successfully found.\n")
print("\n- Successfully found invoices iDs:")
print(orders)
print("\n- Errors encoutered for invoices IDs:")
print(errors)
if __name__ == '__main__':
main()