Merge pull request 'Render album titles, individual image titles, and descriptions' (#5) from austin/imgin:master into master

Reviewed-on: #5
This commit is contained in:
kev 2022-01-16 01:01:58 +00:00
commit 3dec8df927
3 changed files with 59 additions and 38 deletions

View File

@ -21,8 +21,8 @@ def get_timestamp_of_file(file):
def album(id): def album(id):
req = IMAGE_CACHE req = IMAGE_CACHE
get("/a/" + id, req) title, metas = get("a/" + id, req)
found_list_file = IMAGE_CACHE + ("/a/" + id).replace('/', '_') found_list_file = IMAGE_CACHE + ("a/" + id).replace('/', '_')
with open(found_list_file, 'r') as f: with open(found_list_file, 'r') as f:
imgs = f.read().split(',') imgs = f.read().split(',')
@ -34,13 +34,13 @@ def album(id):
imgs = sorted(imgs, key=get_timestamp_of_file) imgs = sorted(imgs, key=get_timestamp_of_file)
for c, img in enumerate(imgs): 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: with open(f'{template_dir}gallery.html', 'r') as img_view:
tpl = SimpleTemplate(img_view) tpl = SimpleTemplate(img_view)
return tpl.render(imgs=imgs) return tpl.render(imgs=imgs, title=title)
@route('/') @route('/')
@route('') @route('')

View File

@ -25,15 +25,10 @@ def error(msg):
def get(url: str, write_dir: str, delete=True): def get(url: str, write_dir: str, delete=True):
orig_url = url orig_url = url
if not url.startswith('https://imgur.com/'): if not url.startswith('https://imgur.com/'):
url = 'https://imgur.com' + url url = 'https://imgur.com/' + url
found_url = ''
found_urls = []
found_list_file = ''
album = False album = False
if "gallery" in url: if url.startswith("https://imgur.com/a/"):
url = url.replace("gallery", "a")
if "/a/" in url:
album = True album = True
if not url.endswith("blog"): if not url.endswith("blog"):
url += "/layout/blog" url += "/layout/blog"
@ -46,38 +41,39 @@ def get(url: str, write_dir: str, delete=True):
img.write(requests.get(url).content) img.write(requests.get(url).content)
if delete: if delete:
Thread(target=delete_file, args=[f"{write_dir}/{url[-11:]}"]).start() Thread(target=delete_file, args=[f"{write_dir}/{url[-11:]}"]).start()
return None
else: else:
found_url = ''
found_urls = []
found_list_file = ''
title = ''
metas = []
print('Detecting album/gallery images (contentUrl)', url) print('Detecting album/gallery images (contentUrl)', url)
soup = bs4.BeautifulSoup(requests.get(url).text, 'html.parser') soup = bs4.BeautifulSoup(requests.get(url).text, 'html.parser')
for count, el in enumerate(soup.select('.post-image meta[itemprop="contentUrl"]'), start=1):
try: try:
found_url = "https:" + el['content'] 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:" + minisoup.select('.post-image meta[itemprop="contentUrl"]')[0]['content']
if '?1' in found_url: if '?1' in found_url:
continue continue
except KeyError: except (KeyError, IndexError):
error("Could not obtain url for detected image (contentUrl)") error("Could not obtain url for detected image (contentUrl), trying id method")
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):
try: try:
found_url = "https://i.imgur.com/" + el['id'] + ".jpg" # equivalent to .png found_url = "https://i.imgur.com/" + el['id'] + ".jpg" # equivalent to .png
except KeyError: except KeyError:
error("Could not obtain url for detected image (id)") error("Could not obtain url for detected image (id)")
continue continue
if found_url.endswith('ico.jpg'):
continue
found_urls.append(found_url[-11:]) found_urls.append(found_url[-11:])
write(count, found_url, write_dir, delete)
# 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(f"Downloading image {count}: {found_url}")
print("Writing image", f"{write_dir}{found_url[-11:]}") print("Writing image", f"{write_dir}{found_url[-11:]}")
@ -86,3 +82,22 @@ def write(count: int, found_url: str, write_dir: str, delete=True):
if delete: if delete:
Thread(target=delete_file, args=[f"{write_dir}{found_url[-11:]}"]).start() 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()
return title, metas

View File

@ -4,14 +4,20 @@
<head> <head>
<meta charself="utf-8"> <meta charself="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>imgin - minimal & private imgur proxy</title> <title>{{title + " - imgin" if title else "imgin - minimal & private imgur proxy"}}</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📷</text></svg>"> <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📷</text></svg>">
<link rel="stylesheet" href="/static/theme.css"> <link rel="stylesheet" href="/static/theme.css">
</head> </head>
<body> <body>
% if title != '':
<h1>{{title}}</h1>
% end
% for img in imgs: % for img in imgs:
<a href="{{img}}"><img src="{{img}}" loading="lazy"></a> % if img[1] != '':
<br> <h2>{{img[1]}}</h2>
% end
<p>{{img[2]}}</p>
<a href="{{img[0]}}"><img src="{{img[0]}}" loading="lazy"></a>
% end % end
<footer> <footer>
<small> <small>