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): def log(ids, n, id, msg):
print(f"{image_id} ({n+1:_}/{len(image_ids):_}): {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(): def main():
@ -37,12 +53,15 @@ def main():
) )
args = parser.parse_args() args = parser.parse_args()
print("Loading image ids") print("Loading image and album ids")
with open(args.info) as f: 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) args.dir.mkdir(parents=True, exist_ok=True)
print("Downloading images")
for n, image_id in enumerate(image_ids): for n, image_id in enumerate(image_ids):
# Yes, I know not all images are pngs. Use file(1) or any other tool of # 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. # your choice to fix the extensions after downloading the files.
@ -54,19 +73,17 @@ def main():
if image_path.exists(): if image_path.exists():
continue continue
try: download(image_ids, n, image_id, image_path, image_link)
r = requests.get(image_link)
if r.status_code == 404: print("Downloading albums")
log(image_ids, n, image_id, "Not found (404)") 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 continue
elif r.status_code != 200:
log(image_ids, n, image_id, f"Weird status code: {r.status_code}") download(album_ids, n, album_id, album_path, album_link)
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}")
if __name__ == "__main__": if __name__ == "__main__":