Last active
December 6, 2021 09:42
-
-
Save hussu010/c5d3147f41d6a212a62af7e8678514dd to your computer and use it in GitHub Desktop.
Fetch TNBC accounts created in a duration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from datetime import datetime | |
import json | |
BANK_IP = "54.183.16.194" | |
def fetch_accounts_created_in_a_duration(start_date, end_date): | |
results = [] | |
bank_accounts_endpoit = f"http://{BANK_IP}/accounts?ordering=created_date" | |
next_url = bank_accounts_endpoit | |
while next_url: | |
print(f"Scanning {next_url}") | |
r = requests.get(next_url).json() | |
next_url = r['next'] | |
for accounts in r["results"]: | |
account_created_at = datetime.strptime(accounts['created_date'], '%Y-%m-%dT%H:%M:%S.%fZ') | |
if account_created_at <= end_date: | |
if account_created_at >= start_date: | |
results.append(accounts) | |
else: | |
next_url = None | |
break | |
print("Scan between the duration completed, check the json file") | |
return results | |
def write_json(file, data): | |
""" | |
Write JSON file | |
""" | |
with open(file, 'w') as f: | |
json.dump(data, f, indent=2) | |
def validate_date(date_text): | |
try: | |
formatted_date = datetime.strptime(date_text, '%Y-%m-%d') | |
return True, formatted_date | |
except ValueError: | |
print("Incorrect data format, should be YYYY-MM-DD") | |
return False, 0 | |
def get_dates_from_user(): | |
start_date = input("Enter the date you'd like to fetch the accounts created from (YYYY-MM-DD): ") | |
while True: | |
success, date = validate_date(start_date) | |
if success: | |
parsed_start_date = date | |
break | |
else: | |
start_date = input("Enter the date you'd like to fetch the accounts created from (YYYY-MM-DD): ") | |
end_date = input("Enter the date you'd like to fetch the accounts to (YYYY-MM-DD): ") | |
while True: | |
success, date = validate_date(end_date) | |
if success: | |
parsed_end_date = date | |
break | |
else: | |
end_date = input("Enter the date you'd like to fetch the accounts to (YYYY-MM-DD): ") | |
return parsed_start_date, parsed_end_date | |
def run(): | |
start_date, end_date = get_dates_from_user() | |
start_date_readable = start_date.strftime("%Y_%m_%d") | |
end_date_readable = end_date.strftime("%Y_%m_%d") | |
accounts_created = fetch_accounts_created_in_a_duration(start_date, end_date) | |
json_file_name = f"accounts_created_between_{start_date_readable}_to_{end_date_readable}.json" | |
write_json(json_file_name, accounts_created) | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment