Fixed bug in puller causing bailout during connection error

This commit is contained in:
Jeremy Karst 2024-05-18 19:06:50 -04:00
parent a47c8b95d0
commit 133a4f563c

View file

@ -15,20 +15,20 @@ directory_url = r"https://data.ngdc.noaa.gov/platforms/solar-space-observing-sat
stored_images_dir = r"..\Data"
# directory_url = r"https://data.ngdc.noaa.gov/platforms/solar-space-observing-satellites/goes/goes16/l2/data/suvi-l2-ci094/"
# stored_images_dir = r"Z:\NOAA GOES Data\Data\goes16\l2\suvi-l2-ci094"
ignore_folder_names = ["l1b", "goes17", "2017", "2018", "2019", "2020", "2021", "2022"]
ignore_folder_names = ["l1b", "goes17", "2017", "2018", "2019", "2020"]
file_database_path = r"..\file_database.json"
fetch_interval = 60*60
nworkers = 16
randomize_order = True
fetch_interval = 0 # 60*60
nworkers = 8
randomize_order = False
def recursive_find_links(url, attempt_count = 3, randomize_order = True):
links_regex_pattern = r'(?<=<a href=")([^ ?:]*)(?=">.*\d{4}-\d{2}-\d{2} \d{2}:\d{2})' # Find href links that do not contain question marks or whitespace
times_regex_pattern = r'(?<=<td align="right">)(\d{4}-\d{2}-\d{2} \d{2}:\d{2})(?= )' # Find timestamps in UTC in the format YYYY-MM-DD HH:mm
with urllib.request.urlopen(url) as response:
attempts = 0
html_content = None
while attempts < attempt_count:
try:
with urllib.request.urlopen(url) as response:
html_content = response.read().decode('utf-8')
break
except Exception as e:
@ -61,7 +61,7 @@ def recursive_find_links(url, attempt_count = 3, randomize_order = True):
if _link.split(r"/")[-2] in ignore_folder_names:
continue
else:
yield from recursive_find_links(urllib.parse.urljoin(url,_link), randomize_order)
yield from recursive_find_links(urllib.parse.urljoin(url,_link), attempt_count, randomize_order)
else:
yield urllib.parse.urljoin(url,_link), _time
@ -120,7 +120,7 @@ if __name__ == "__main__":
already_had_image_count = 0
failed_image_count = 0
urllen = len(directory_url)
for l, t in tqdm.tqdm(recursive_find_links(directory_url, randomize_order), desc="Downloading files"):
for l, t in tqdm.tqdm(recursive_find_links(directory_url, randomize_order = randomize_order), desc="Downloading files"):
# Collect completed jobs and record completion status
while True:
try:
@ -152,7 +152,14 @@ if __name__ == "__main__":
with open(file_database_path, 'w') as f:
f.write(json.dumps(file_info_cache))
print(f"Downloaded {fetched_image_count} | Already had {already_had_image_count} | Failed {failed_image_count}")
if fetch_interval > 0:
print(f"Run complete!, sleeping for {fetch_interval} seconds.")
with open(file_database_path, 'w') as f:
f.write(json.dumps(file_info_cache))
time.sleep(fetch_interval)
else:
print("Run complete!, exiting...")
break
except KeyboardInterrupt:
print("Saving file database and shutting down.")