Skip to content

Instantly share code, notes, and snippets.

@classAndrew
Last active November 17, 2022 17:58
Show Gist options
  • Save classAndrew/1904064482304f1c939d9e84e3d07477 to your computer and use it in GitHub Desktop.
Save classAndrew/1904064482304f1c939d9e84e3d07477 to your computer and use it in GitHub Desktop.
import os
import logging
import time
import requests
import json
__version__ = "1.0.0"
__author__ = "Andrew Lin"
logging.basicConfig(level=logging.INFO, format="[%(name)s] %(levelname)s %(asctime)s :: %(message)s")
log = logging.getLogger(__file__)
OUT_DIR = "./json_data"
if not os.path.exists(OUT_DIR):
os.mkdir(OUT_DIR)
FLIGHT_URI = "https://s3.us-east-1.amazonaws.com/nasa-jsc-public/Orion/mission/Orion_flight104_mission.txt"
def track_data(rest=60, connect_timeout=30, same_tries=3, same_sleep=1, fail_sleep=5):
N_file = 0
last_date = ""
while True:
try:
r_data = requests.get(FLIGHT_URI, timeout=connect_timeout).json()
if last_date == r_data["File"].get("Date", ""): # this should always be a given parameter
try_i = 0
while try_i < same_tries:
log.warn(f"Same Date Found. Retrying #{try_i}")
r_data = requests.get(FLIGHT_URI).json()
if last_date != r_data.get("Date"):
break # new data received
time.sleep(same_sleep)
try_i += 1
last_date = r_data["File"].get("Date", "") # update last_date
last_date_name = last_date.replace("/", "_").replace(" ", "_")
with open(OUT_DIR+"/"+last_date_name+".json", 'w') as f:
json.dump(r_data, f)
log.info(f"#{N_file} recorded.")
N_file += 1
time.sleep(rest)
except Exception as e:
log.exception(e)
time.sleep(fail_sleep) # if some exception thrown, retry in 5 sec
last_date += "a" # just to make sure it doesn't enter try_i loop on next run
if __name__ == "__main__":
track_data(rest=60, connect_timeout=30, same_tries=3, same_sleep=1, fail_sleep=5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment