diff --git a/imgin/__init__.py b/imgin/__init__.py index 85f6cbd..3abc465 100644 --- a/imgin/__init__.py +++ b/imgin/__init__.py @@ -21,8 +21,8 @@ def get_timestamp_of_file(file): def album(id): req = IMAGE_CACHE - get("/a/" + id, req) - found_list_file = IMAGE_CACHE + ("/a/" + id).replace('/', '_') + title, metas = get("a/" + id, req) + found_list_file = IMAGE_CACHE + ("a/" + id).replace('/', '_') with open(found_list_file, 'r') as f: imgs = f.read().split(',') @@ -34,13 +34,13 @@ def album(id): imgs = sorted(imgs, key=get_timestamp_of_file) for c, img in enumerate(imgs): - imgs[c] = img.replace(IMAGE_CACHE, '/') + imgs[c] = (img.replace(IMAGE_CACHE, '/'), metas[c][0], metas[c][1]) with open(f'{template_dir}gallery.html', 'r') as img_view: tpl = SimpleTemplate(img_view) - return tpl.render(imgs=imgs) + return tpl.render(imgs=imgs, title=title) @route('/') @route('') diff --git a/imgin/get.py b/imgin/get.py index 7eb1f69..bff6209 100644 --- a/imgin/get.py +++ b/imgin/get.py @@ -25,15 +25,10 @@ def error(msg): 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 = '' + url = 'https://imgur.com/' + url album = False - if "gallery" in url: - url = url.replace("gallery", "a") - if "/a/" in url: + if url.startswith("https://imgur.com/a/"): album = True if not url.endswith("blog"): url += "/layout/blog" @@ -46,43 +41,63 @@ def get(url: str, write_dir: str, delete=True): img.write(requests.get(url).content) if delete: Thread(target=delete_file, args=[f"{write_dir}/{url[-11:]}"]).start() + return None else: + found_url = '' + found_urls = [] + found_list_file = '' + title = '' + metas = [] print('Detecting album/gallery images (contentUrl)', url) soup = bs4.BeautifulSoup(requests.get(url).text, 'html.parser') - for count, el in enumerate(soup.select('.post-image meta[itemprop="contentUrl"]'), start=1): + try: + title = soup.select('meta[property="og:title"]')[0]['content'] + if title == "Imgur": + title = '' + except (KeyError, IndexError): + title = '' + for count, el in enumerate(soup.select('.post-image-container'), start=1): + if el is None: + continue + minisoup = bs4.BeautifulSoup(str(el), 'html.parser') try: - found_url = "https:" + el['content'] + found_url = "https:" + minisoup.select('.post-image meta[itemprop="contentUrl"]')[0]['content'] if '?1' in found_url: continue - except KeyError: - error("Could not obtain url for detected image (contentUrl)") - continue - if found_url.endswith('ico.jpg'): - continue - found_urls.append(found_url[-11:]) - write(count, found_url, write_dir, delete) - if len(found_urls) == 0: - print('Detecting album/gallery images (id)', url) - for count, el in enumerate(soup.select('.post-image-container'), start=1): + except (KeyError, IndexError): + error("Could not obtain url for detected image (contentUrl), trying id method") try: found_url = "https://i.imgur.com/" + el['id'] + ".jpg" # equivalent to .png except KeyError: error("Could not obtain url for detected image (id)") continue - found_urls.append(found_url[-11:]) - write(count, found_url, write_dir, delete) + 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() + + subtitle = '' + try: + subtitle = minisoup.select('.post-image-title')[0].string + except IndexError: + subtitle = '' + desc = '' + try: + desc = minisoup.select('.post-image-description')[0].string + except IndexError: + desc = '' + date = '' + metas.append((subtitle, desc)) # Write the found urls to a file with the name of the album so the viewer endpoint can get them found_list_file = write_dir + 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() - -def write(count: int, found_url: str, write_dir: str, delete=True): - 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() + return title, metas diff --git a/imgin/web/gallery.html b/imgin/web/gallery.html index 8d7b4ce..694b136 100644 --- a/imgin/web/gallery.html +++ b/imgin/web/gallery.html @@ -4,14 +4,20 @@ - imgin - minimal & private imgur proxy + {{title + " - imgin" if title else "imgin - minimal & private imgur proxy"}} + % if title != '': +

{{title}}

+ % end % for img in imgs: - -
+ % if img[1] != '': +

{{img[1]}}

+ % end +

{{img[2]}}

+ % end