Download albums as zips

This commit is contained in:
Joscha 2023-04-26 23:23:16 +02:00
parent f547a52640
commit 060e3bd868

View file

@ -12,8 +12,24 @@ Download imgur images that have not yet been downloaded.
"""
def log(image_ids, n, image_id, msg):
print(f"{image_id} ({n+1:_}/{len(image_ids):_}): {msg}")
def log(ids, n, id, msg):
print(f"{id} ({n+1:_}/{len(ids):_}): {msg}")
def download(ids, n, id, path, link):
try:
r = requests.get(link)
if r.status_code == 404:
log(ids, n, id, "Not found (404)")
return
elif r.status_code != 200:
log(ids, n, id, f"Weird status code: {r.status_code}")
return
with open(path, "wb") as f:
f.write(r.content)
log(ids, n, id, "Downloaded")
except Exception as e:
log(ids, n, id, f"Error fetching {link}: {e}")
def main():
@ -37,12 +53,15 @@ def main():
)
args = parser.parse_args()
print("Loading image ids")
print("Loading image and album ids")
with open(args.info) as f:
image_ids = json.load(f)["image_ids"]
data = json.load(f)
image_ids = data["image_ids"]
album_ids = data["album_ids"]
args.dir.mkdir(parents=True, exist_ok=True)
print("Downloading images")
for n, image_id in enumerate(image_ids):
# Yes, I know not all images are pngs. Use file(1) or any other tool of
# your choice to fix the extensions after downloading the files.
@ -54,19 +73,17 @@ def main():
if image_path.exists():
continue
try:
r = requests.get(image_link)
if r.status_code == 404:
log(image_ids, n, image_id, "Not found (404)")
download(image_ids, n, image_id, image_path, image_link)
print("Downloading albums")
for n, album_id in enumerate(album_ids):
album_path = args.dir / f"{album_id}.png"
album_link = f"https://imgur.com/a/{album_id}/zip"
if album_path.exists():
continue
elif r.status_code != 200:
log(image_ids, n, image_id, f"Weird status code: {r.status_code}")
continue
with open(image_path, "wb") as f:
f.write(r.content)
log(image_ids, n, image_id, "Downloaded")
except Exception as e:
log(image_ids, n, image_id, f"Error fetching {image_link}: {e}")
download(album_ids, n, album_id, album_path, album_link)
if __name__ == "__main__":