import json import sys import urllib.request from urllib.error import HTTPError, URLError from datetime import datetime HISTORY_PATH = ".\\History.json" with open(HISTORY_PATH, "r") as f: # backup with open(HISTORY_PATH + "." + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".bak", "w+") as bak: bak.write(f.read()) history = json.loads("[" + f.read() + "]") for item in history: # check if it's been uploaded if not item.get("URL"): continue try: # try fetching the url, and raise HTTPError if an error occured urllib.request.urlopen(item["URL"]) except HTTPError as e: code = e.getcode() # delete item in history dictionary if code == 200: print(f"Succeeded with {item['FileName']}.") elif code == 404: print(f"{item['FileName']} failed, removing..", file=sys.stderr) history.remove(item) else: print(f"{item['FileName']} returned unknown status code ({code}). Ignoring.", file=sys.stderr) except URLError: print("Internet connection failed.") sys.exit(1) # dump new history with open(HISTORY_PATH, "w") as hist: jsonContent = "" for item in history: if history.index(item) != 0: jsonContent += ",\n" jsonContent += json.dumps(item, indent=2) hist.write(jsonContent)