Skip to content

Instantly share code, notes, and snippets.

@botatooo
Last active October 22, 2022 21:31
Show Gist options
  • Save botatooo/baddcc1c70a0f9abbe399ec81523b044 to your computer and use it in GitHub Desktop.
Save botatooo/baddcc1c70a0f9abbe399ec81523b044 to your computer and use it in GitHub Desktop.
delete all of the imgur images & dead imgur links in your history. download the script to the sharex folder and run it
#!/usr/bin/env python
import sys
import json
import requests
import urllib.request
from urllib.error import HTTPError, URLError
from datetime import datetime
ALSO_CLEAR_DEAD_IMGUR_LINKS = True
HISTORY_PATH = ".\\History.json"
with open(HISTORY_PATH, "r") as f:
history = f.read()
# backup
with open(HISTORY_PATH + "." + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".bak", "w+") as bak:
bak.write(history)
history = json.loads("[" + history + "]")
for item in history:
if not item.get("DeletionURL", "").startswith("https://imgur.com/delete/") or not item.get("URL"):
continue
res = requests.post(item["DeletionURL"], data={"confirm": "true"})
if res.status_code == 200:
print("Deleted " + item["DeletionURL"])
history.remove(item)
else:
print("Failed to delete " + item["DeletionURL"])
if not ALSO_CLEAR_DEAD_IMGUR_LINKS:
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