-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
192 lines (168 loc) · 4.89 KB
/
app.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
import requests
import sys
import re
import time
import json
from datetime import datetime, timedelta
from dateutil import parser
# Shopify store configuration
ACCESS_TOKEN = 'xXXXXXXXXXXXXXXXXXx'
SHOP_NAME = 'xXXXXXXx'
# number of seconds to wait between fetching pages
SLEEP_DURATION = 2
# debug mode
VERBOSE = False
def get_query(date=None, cursor=None):
created_at = ""
if date:
created_at = '''query: "created_at:%s",''' % date
after = ""
if cursor:
after = ''', after: "%s"''' % cursor
return '''\
query {
orders(%s first: 8 %s) {
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
cursor
node {
id
email
name
discountCode
totalPriceSet {
shopMoney {
amount
currencyCode
}
}
createdAt
updatedAt
discountCode
billingAddress {
country
province
city
zip
}
fulfillments {
fulfillmentLineItems(first: 15) {
edges {
node {
lineItem {
id
sku
name
quantity
originalTotalSet {
shopMoney {
amount
currencyCode
}
}
discountAllocations {
allocatedAmountSet {
shopMoney {
amount
currencyCode
}
}
}
}
}
}
}
}
cancelledAt
cancelReason
refunds {
refundLineItems(first: 10) {
edges {
node {
lineItem {
id
name
}
}
}
}
}
}
}
}
}\
''' % (created_at, after)
def get_reports(date=None, cursor=None):
query = get_query(date, cursor)
headers = {'Content-Type': 'application/json', 'X-Shopify-Access-Token': ACCESS_TOKEN}
req = requests.Request('POST', 'https://%s.myshopify.com/admin/api/2020-07/graphql.json' % SHOP_NAME,
json={"query": query}, headers=headers)
prepared = req.prepare()
if VERBOSE:
pretty_print_POST(prepared)
s = requests.Session()
r = s.send(prepared)
if r.status_code != 200:
if r.status_code == 204:
print("No data to get.")
print(r.status_code)
return
else:
print("Error to get json data:")
print(r.status_code)
print(r.text)
print("Exiting now.")
sys.exit(1)
j = r.json()
if j['data'] and \
j['data']['orders'] and \
j['data']['orders']['pageInfo'] and \
j['data']['orders']['pageInfo']['hasNextPage']:
last_pos = len(j['data']['orders']['edges'])
cursor = j['data']['orders']['edges'][last_pos - 1]['cursor']
print("Found next page cursor cursor=%s" % cursor)
else:
cursor = None
handle_data(j)
if cursor:
print("Waiting %s seconds before fetching next page via cursor %s" % (SLEEP_DURATION, cursor))
time.sleep(SLEEP_DURATION)
get_reports(date=date, cursor=cursor)
def handle_data(json_data):
j = json_data
if VERBOSE:
print(json.dumps(json_data, indent=2, sort_keys=True))
if j['data']['orders']['edges'] and len(j['data']['orders']['edges']) > 0:
print("Handling %s orders..." % len(j['data']['orders']['edges']))
for order_node in j['data']['orders']['edges']:
order = order_node['node']
print("Order: %s -> %s" % (order['createdAt'], order['email']))
fulfillments = order['fulfillments']
for fulfillment in fulfillments:
for line_item_node in fulfillment['fulfillmentLineItems']['edges']:
line_item = line_item_node['node']
print("sku: %s -> quantity: %s" % (line_item['lineItem']['sku'], line_item['lineItem']['quantity']))
return
def pretty_print_POST(req):
print('{}\n{}\r\n{}\r\n\r\n{}'.format(
'-----------START-----------',
req.method + ' ' + req.url,
'\r\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
req.body,
))
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Please provide a valid date to run for: YYYY-MM-DD")
sys.exit(1)
else:
# validate that it's a date
try:
d = datetime.strptime(sys.argv[1], '%Y-%m-%d')
get_reports(date=sys.argv[1])
except ValueError:
print("Please provide a valid date to run for: YYYY-MM-DD")
sys.exit(1)