Skip to content

Instantly share code, notes, and snippets.

@botatooo
Last active September 17, 2022 05:23
Show Gist options
  • Save botatooo/47de9f200111d5ed8377ef26bdebfa82 to your computer and use it in GitHub Desktop.
Save botatooo/47de9f200111d5ed8377ef26bdebfa82 to your computer and use it in GitHub Desktop.
remove dead links from history. download the script to the sharex folder and run it
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment