From 15c199f61045ce415cc2d9689f8a882a0e0047aa Mon Sep 17 00:00:00 2001 From: Kevin F Date: Tue, 5 Oct 2021 18:40:48 +0000 Subject: [PATCH 01/13] Fix #2 Added version number to index --- CHANGELOG.md | 4 +++- imgin/__init__.py | 10 +++++++--- imgin/get.py | 15 +++++++++++++-- imgin/web/index.html | 1 + 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a79fdb..ced8b0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog -This project uses Semantic Versioning +## 0.0.1 + +Switched from glob to a list file which fixes albums overwriting each other (Reported by SeerLite) ## 0.0.0 diff --git a/imgin/__init__.py b/imgin/__init__.py index a067648..c672767 100644 --- a/imgin/__init__.py +++ b/imgin/__init__.py @@ -4,7 +4,6 @@ monkey.patch_all() from threading import Thread from os import remove, mkdir, path, stat from shutil import rmtree -from glob import glob from uuid import uuid4 @@ -20,12 +19,16 @@ def get_timestamp_of_file(file): return stat(file).st_ctime def album(id): - req_id = str(uuid4()) req = IMAGE_CACHE get("/a/" + id, req) + found_list_file = IMAGE_CACHE + ("/a/" + id).replace('/', '_') - imgs = glob(req + "*") + with open(found_list_file, 'r') as f: + imgs = f.read().split(',') + + for c, img in enumerate(imgs): + imgs[c] = IMAGE_CACHE + imgs[c] # sort image order (file creation time) imgs = sorted(imgs, key=get_timestamp_of_file) @@ -62,6 +65,7 @@ def gallery(id=''): def img(img=''): if not img.endswith("jpeg") and not img.endswith("jpg") and not img.endswith("png"): img = img + ".jpg" + img = img.replace('jpeg', 'jpg') if not path.exists(IMAGE_CACHE + img): get(img, IMAGE_CACHE) return static_file(img, root=IMAGE_CACHE) diff --git a/imgin/get.py b/imgin/get.py index d7de595..d90a908 100644 --- a/imgin/get.py +++ b/imgin/get.py @@ -6,7 +6,7 @@ import requests import bs4 from gevent import sleep -from .config import SINGLE_IMAGE_DELETE_AFTER_SECS +from .config import IMAGE_CACHE, SINGLE_IMAGE_DELETE_AFTER_SECS def delete_file(path): sleep(SINGLE_IMAGE_DELETE_AFTER_SECS) @@ -22,9 +22,12 @@ def error(msg): sys.stderr.flush() def get(url: str, write_dir: str, delete=True): + orig_url = url if not url.startswith('https://imgur.com/'): url = 'https://imgur.com/' + url found_url = '' + found_urls = [] + found_list_file = '' album = False if "gallery" in url: @@ -37,7 +40,7 @@ def get(url: str, write_dir: str, delete=True): if not album: print('Getting img', url) - url = 'https://i.imgur.com/' + url.rsplit('/', 1)[-1].replace('jpeg', 'jpg') + url = 'https://i.imgur.com/' + url.rsplit('/', 1)[-1] with open(f'{write_dir}/{url[-11:]}', 'wb') as img: img.write(requests.get(url).content) if delete: @@ -53,11 +56,19 @@ def get(url: str, write_dir: str, delete=True): continue if found_url.endswith('ico.jpg'): continue + found_urls.append(found_url[-11:]) print(f"Downloading image {count}: {found_url}") print("Writing image", f"{write_dir}{found_url[-11:]}") with open(f"{write_dir}{found_url[-11:]}", "wb") as f: f.write(requests.get(found_url).content) + if delete: Thread(target=delete_file, args=[f"{write_dir}{found_url[-11:]}"]).start() + # Write the found urls to a file with the name of the album so the viewer endpoint can get them + found_list_file = IMAGE_CACHE + orig_url.replace('/', '_') + with open(found_list_file, 'w') as f: + f.write(','.join(found_urls)) + Thread(target=delete_file, args=[found_list_file]).start() + diff --git a/imgin/web/index.html b/imgin/web/index.html index 5b89fc8..6c08cc2 100644 --- a/imgin/web/index.html +++ b/imgin/web/index.html @@ -39,6 +39,7 @@

Or, run it with python by installing the requirements.txt and ./run.py. Or use the Dockerfile. Contact me if you want help or find a bug.