Check if a valid token exists or generate a new token
iflen(list_tokens := requests.get(edl_token_urls['list_token'], auth=(get_edl_creds()['username'], get_edl_creds()['password'])).json()) <1:#print('No tokens available. Generating new Earthdata Login Token ...') generate_token_url ="https://urs.earthdata.nasa.gov/api/users/token" generate_token_req = requests.post(edl_token_urls['generate_token'], auth=(get_edl_creds()['username'], get_edl_creds()['password'])) token = generate_token_req.json()withopen("../../../.hidden_dir/edl_token.json", "w") as outfile: json.dump(token, outfile)print(f'Your EDL token information can be found here: {os.path.abspath("../../../.hidden_dir/edl_token.json")}')elif datetime.strptime(list_tokens[0]['expiration_date'], "%m/%d/%Y") < datetime.now():#print('Available token is expired. Generating a new Earthdata Login Token ...') revoke_token = requests.post(f"{edl_token_urls['revoke_token']}?token={list_tokens[0]}", auth=(get_edl_creds()['username'], get_edl_creds()['password'])) generate_token_req = requests.post(edl_token_urls['generate_token'], auth=(get_edl_creds()['username'], get_edl_creds()['password'])) token = generate_token_req.json()withopen("../../../.hidden_dir/edl_token.json", "w") as outfile: json.dump(token, outfile)print(f'Your EDL token information can be found here: {os.path.abspath("../../../.hidden_dir/edl_token.json")}')else:#print('Earthdata Login Token Found ...')withopen("../../../.hidden_dir/edl_token.json", "w") as outfile: json.dump(list_tokens[0], outfile)print(f'Your EDL token information can be found here: {os.path.abspath("../../../.hidden_dir/edl_token.json")}')