···
from base64 import b64encode
···
success(f'flow token => {flow_token}')
info('Fetching final account object...')
259
-
tasks = send_req('post', TASKS_ENDPOINT, headers=request_headers, json=get_tasks_body(flow_token)).json()
263
+
for attempt in range(0, 6):
264
+
debug(f"Attempt #{attempt + 1}")
265
+
tasks = send_req('post', TASKS_ENDPOINT, headers=request_headers, json=get_tasks_body(flow_token)).json()
262
-
open_account_task = next(filter(lambda i: i.get('subtask_id') == "OpenAccount", tasks['subtasks']))
263
-
account = open_account_task['open_account']
264
-
except StopIteration:
265
-
debug("resulting tasks =>", format_json(tasks), override=True)
266
-
error("Unable to acquire guest account credentials as it isn't present in the API response.")
267
-
error("This might be because of a wide variety of reasons, but it most likely is due to your IP being rate-limited.")
268
-
error("Try again with a new IP address or in 24 hours after this attempt.")
268
+
open_account_task = next(filter(lambda i: i.get('subtask_id') == "OpenAccount", tasks['subtasks']))
269
+
account = open_account_task['open_account']
270
+
except StopIteration as e:
271
+
backoff_time += random.randint(5000,10000) / 1000
273
+
warn(f"attempt #{attempt + 1} failed to acquire token, retrying in {backoff_time}s.")
274
+
time.sleep(backoff_time)
271
-
if args.outfile == "-":
272
-
debug("outfile is `-`, printing to stdout")
273
-
print(format_json(account))
277
+
info(f"Attempt #{attempt + 1} succeeded")
278
+
if args.outfile == "-":
279
+
debug("outfile is `-`, printing to stdout")
280
+
print(format_json(account))
278
-
debug(f"attempting to read file: {args.outfile}")
279
-
with open(args.outfile) as f:
280
-
old_data = json.load(f)
281
-
except FileNotFoundError:
282
-
# that's okay, we might be able to create it later.
284
-
except PermissionError:
285
-
# that's not okay. we will need to access the file later anyways.
286
-
error("unable to read file due to a permission error.")
287
-
error("please make sure this script has read and write access to the file.")
288
-
print(format_json(account))
290
-
except json.JSONDecodeError:
291
-
error("could not parse the provided JSON file.")
292
-
if not prompt_bool("Do you want to overwrite the file?", default=None):
293
-
warn("Not overwriting file, printing to stdout instead.")
285
+
debug(f"attempting to read file: {args.outfile}")
286
+
with open(args.outfile) as f:
287
+
old_data = json.load(f)
288
+
except FileNotFoundError:
289
+
# that's okay, we might be able to create it later.
291
+
except PermissionError:
292
+
# that's not okay. we will need to access the file later anyways.
293
+
error("unable to read file due to a permission error.")
294
+
error("please make sure this script has read and write access to the file.")
print(format_json(account))
296
-
debug('assuming old data is an empty array because we are overwriting')
298
-
if type(old_data) != list:
299
-
error("top-level object of the existing JSON file is not a list.")
300
-
error("due to the implementation, the file must be overwritten.")
301
-
if not (prompt_bool("Do you want to overwrite?", default=None)):
302
-
warn("Not overwriting existing data, printing to stdout instead.")
297
+
except json.JSONDecodeError:
298
+
error("could not parse the provided JSON file.")
299
+
if not prompt_bool("Do you want to overwrite the file?", default=None):
300
+
warn("Not overwriting file, printing to stdout instead.")
301
+
print(format_json(account))
303
+
debug('assuming old data is an empty array because we are overwriting')
305
+
if type(old_data) != list:
306
+
error("top-level object of the existing JSON file is not a list.")
307
+
error("due to the implementation, the file must be overwritten.")
308
+
if not (prompt_bool("Do you want to overwrite?", default=None)):
309
+
warn("Not overwriting existing data, printing to stdout instead.")
310
+
print(format_json(account))
312
+
debug("assuming old data is an empty array because we are overwriting")
315
+
old_data.append(account)
318
+
debug("attempting to write file")
319
+
with open(args.outfile, 'w+') as f:
320
+
f.write(format_json(old_data))
321
+
success(f"successfully written to file {args.outfile}")
323
+
except PermissionError:
324
+
error("unable to write to file due to permission error.")
325
+
error("please make sure this script has write access to the file.")
print(format_json(account))
305
-
debug("assuming old data is an empty array because we are overwriting")
308
-
old_data.append(account)
328
+
except Exception as e:
329
+
error("Unable to write to file due to an uncaught error:", e)
330
+
tb = ''.join(traceback.TracebackException.from_exception(e).format())
331
+
debug("exception stacktrace\n" + tb)
332
+
print(format_json(account))
311
-
debug("attempting to write file")
312
-
with open(args.outfile, 'w+') as f:
313
-
f.write(format_json(old_data))
314
-
success(f"successfully written to file {args.outfile}")
316
-
except PermissionError:
317
-
error("unable to write to file due to permission error.")
318
-
error("please make sure this script has write access to the file.")
319
-
print(format_json(account))
321
-
except Exception as e:
322
-
error("Unable to write to file due to an uncaught error:", e)
323
-
tb = ''.join(traceback.TracebackException.from_exception(e).format())
324
-
debug("exception stacktrace\n" + tb)
325
-
print(format_json(account))
335
+
debug("resulting tasks =>", format_json(tasks), override=True)
336
+
error("an unhandled error occurred. the tasks object is printed to avoid losing otherwise successful data.")
337
+
error("please file a bug report and attach the traceback below.")
328
-
debug("resulting tasks =>", format_json(tasks), override=True)
329
-
error("an unhandled error occurred. the tasks object is printed to avoid losing otherwise successful data.")
330
-
error("please file a bug report and attach the traceback below.")
340
+
if exception != None:
341
+
debug("resulting tasks =>", format_json(tasks))
342
+
error("Unable to acquire guest account credentials with 5 attempts as it isn't present in any of the API responses.")
343
+
error("This might be because of a wide variety of reasons, but it most likely is due to your IP being rate-limited.")
344
+
error("Try again with a new IP address or in 24 hours after this attempt.")