-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastreact.py
More file actions
342 lines (280 loc) · 10.8 KB
/
Copy pathfastreact.py
File metadata and controls
342 lines (280 loc) · 10.8 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/python3
#
# fastreact.py (url.com).
#
# Copyright (c) 2022 Easton Elliott
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import json
import requests
import argparse
# Needed for colourful output
from pygments import highlight, lexers, formatters
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
# I don't fuck with regex, I let other people do it for me
# The Regex used:
# Thanks to https://www.geeksforgeeks.org/check-if-email-address-valid-or-not-in-python/
import re
_version = "0.1"
# Read the config file and lets set
try:
with open("config.json", "r") as f:
config = json.load(f)
api_url = config["api_url"]
api_version = config["api_version"]
api_token = config["api_token"]
except IOError:
sys.exit("Could not open/read config file :(")
# Initiate the parser
parser = argparse.ArgumentParser()
# Add long and short argument
parser.add_argument(
"--customer", "-c", help="lookup by customer email or ID (ReCharge & Shopify)"
)
parser.add_argument("--order", "-o", help="lookup by order ID (ReCharge & Shopify)")
#parser.add_argument(
# "--email",
# "-e",
# default=False,
# action="store_true",
# help="Filter order by email address",
#)
parser.add_argument("--subscription", "-s", help="lookup by subscription number")
parser.add_argument(
"--colour", "-z", help="Colour all JSON output", action="store_true"
)
# Read arguments from the command line
args = parser.parse_args()
# If we got empty params
if len(sys.argv) == 1:
parser.print_help()
sys.exit("Version %s" % _version)
# Headers needed for ReCharge REST requests
headers = {
"Content-Type": "application/json",
"X-Recharge-Version": api_version,
"X-Recharge-Access-Token": api_token,
}
def http_req_with_retry(destination_url, headers, params=False):
retry_strategy = Retry(
total=3,
status_forcelist=[429, 500, 502, 503, 504],
allowed_methods=["HEAD", "GET", "OPTIONS"],
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)
response = http.get(destination_url, headers=headers)
return response.json()
# Toggle colour output for JSON data (cool! neat!)
colour_flag = args.colour
# Check for customer by ID or email
if args.customer:
print("Set input customer to [ %s ]" % args.customer)
customer = args.customer
if customer.isnumeric():
print("Searching ReCharge customers....")
# Search for ReCharge customer ID first since it will be inhernetly faster
destination_url = api_url + "/customers/" + customer
try:
data = http_req_with_retry(destination_url, headers)
except requests.exceptions.Timeout:
# Maybe set up for a retry, or continue in a retry loop
sys.exit("Timeout, try again")
except requests.exceptions.TooManyRedirects:
sys.exit("Too many redirects, API down?")
# Tell the user their URL was bad and try a different one that isn't so bad
except requests.exceptions.RequestException as e:
raise SystemExit(e)
# No ReCharge data
if "errors" in data:
# No hit so we requery in the hope....
print("Searching Shopify customers....")
# Search Shopify customer ID
destination_url = api_url + "/customers"
try:
data = http_req_with_retry(
destination_url, headers, {"external_customer_id": customer}
)
except requests.exceptions.Timeout:
# Maybe set up for a retry, or continue in a retry loop
sys.exit("Timeout, try again")
except requests.exceptions.TooManyRedirects:
sys.exit("Too many redirects, API down?")
# the request itself was bad :\
except requests.exceptions.RequestException as e:
raise SystemExit(e)
# No results at all
if not data["customers"]:
sys.exit("No customer found by ID, sorry fuck!")
# Customer Hit on Shopify
else:
formatted_json = json.dumps(
data, indent=config["indent_level"], sort_keys=config["data_sort"]
)
if colour_flag:
colourful_json = highlight(
formatted_json,
lexers.JsonLexer(),
formatters.TerminalFormatter(),
)
print(colourful_json)
sys.exit()
else:
print(formatted_json)
sys.exit()
# Hit for ReCharge
if data["customer"]:
formatted_json = json.dumps(
data, indent=config["indent_level"], sort_keys=config["data_sort"]
)
if colour_flag:
colourful_json = highlight(
formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()
)
print(colourful_json)
sys.exit()
else:
print(formatted_json)
sys.exit()
else:
# If we are searching by email address
if re.fullmatch(
r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", customer
):
destination_url = api_url + "/customers"
response = requests.get(
destination_url, params={"email": customer}, headers=headers
)
response.json()
data = response.json()
# No customer ID means nothing found
if not data["customers"]:
sys.exit("No customer found by email, sorry fuck!")
else:
formatted_json = json.dumps(
data, indent=config["indent_level"], sort_keys=config["data_sort"]
)
if colour_flag:
colourful_json = highlight(
formatted_json,
lexers.JsonLexer(),
formatters.TerminalFormatter(),
)
print(colourful_json)
sys.exit()
else:
print(formatted_json)
sys.exit()
else:
sys.exit("Error: No customer found :(")
if args.subscription:
print("Set input subscription to [ %s ]" % args.subscription)
subscription = args.subscription
if subscription.isnumeric():
print("Searching subscriptions....")
# Search for ReCharge subscription ID
destination_url = api_url + "/subscriptions/" + subscription
try:
data = http_req_with_retry(destination_url, headers)
except requests.exceptions.Timeout:
# Maybe set up for a retry, or continue in a retry loop
sys.exit("Timeout, try again")
except requests.exceptions.TooManyRedirects:
sys.exit("Too many redirects, API down?")
# Tell the user their URL was bad and try a different one
except requests.exceptions.RequestException as e:
# catastrophic error. bail.
raise SystemExit(e)
# No ReCharge data (either nada or Shopify data)
if "errors" in data:
# No hit so we requery in the hope....
# print(data)
# No results at all
sys.exit("No subscription found, sorry fuck!")
# Hit for ReCharge
if data["subscription"]:
formatted_json = json.dumps(
data, indent=config["indent_level"], sort_keys=config["data_sort"]
)
if colour_flag:
colourful_json = highlight(
formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()
)
print(colourful_json)
sys.exit()
else:
print(formatted_json)
sys.exit()
# Search by order ID NOT number
if args.order:
print("Set input order to [ %s ]" % args.order)
order = args.order
if order.isnumeric():
# Search ReCharge orders first
print("Searching ReCharge orders.....")
destination_url = api_url + "/orders/" + order
response = requests.get(destination_url, headers=headers)
response.json()
data = response.json()
# No hit on ReCharge, search by Shopify order ID (not number!)
if "errors" in data:
print("Searching Shopify orders....")
# Search Shopify order ID
destination_url = api_url + "/orders/"
response = requests.get(
destination_url, params={"external_order_id": order}, headers=headers
)
response.json()
data = response.json()
#Hit on Shopify
if data["orders"]:
formatted_json = json.dumps(
data, indent=config["indent_level"], sort_keys=config["data_sort"]
)
if colour_flag:
colourful_json = highlight(
formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()
)
print(colourful_json)
sys.exit()
else:
print(formatted_json)
sys.exit()
# No results on Shopify
if not data["orders"]:
sys.exit("No order found by ID, sorry fuck!")
# Hit on Recharge
if "order" in data:
# sys.exit("hit on rc")
formatted_json = json.dumps(
data, indent=config["indent_level"], sort_keys=config["data_sort"]
)
if colour_flag:
colourful_json = highlight(
formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()
)
print(colourful_json)
sys.exit()
else:
print(formatted_json)
sys.exit()
else:
sys.exit("Error: data error, try again?")
else:
sys.exit("Error: not numeric")