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:
commit
3dec8df927
@ -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('')
|
||||
|
77
imgin/get.py
77
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
|
||||
|
@ -4,14 +4,20 @@
|
||||
<head>
|
||||
<meta charself="utf-8">
|
||||
<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="stylesheet" href="/static/theme.css">
|
||||
</head>
|
||||
<body>
|
||||
% if title != '':
|
||||
<h1>{{title}}</h1>
|
||||
% end
|
||||
% for img in imgs:
|
||||
<a href="{{img}}"><img src="{{img}}" loading="lazy"></a>
|
||||
<br>
|
||||
% if img[1] != '':
|
||||
<h2>{{img[1]}}</h2>
|
||||
% end
|
||||
<p>{{img[2]}}</p>
|
||||
<a href="{{img[0]}}"><img src="{{img[0]}}" loading="lazy"></a>
|
||||
% end
|
||||
<footer>
|
||||
<small>
|
||||
|
Loading…
Reference in New Issue
Block a user