add alternative album detection method

This commit is contained in:
Austin Huang 2022-01-06 18:11:05 -05:00
parent d4ccb41741
commit 41dc8def0e
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F

View File

@ -25,7 +25,7 @@ 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_url = ''
found_urls = [] found_urls = []
found_list_file = '' found_list_file = ''
@ -47,7 +47,7 @@ def get(url: str, write_dir: str, delete=True):
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()
else: else:
print('Detecting album/gallery images', 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): for count, el in enumerate(soup.select('.post-image meta[itemprop="contentUrl"]'), start=1):
try: try:
@ -55,11 +55,29 @@ def get(url: str, write_dir: str, delete=True):
if '?1' in found_url: if '?1' in found_url:
continue continue
except KeyError: except KeyError:
error("Could not obtain url for detected image") error("Could not obtain url for detected image (contentUrl)")
continue continue
if found_url.endswith('ico.jpg'): if found_url.endswith('ico.jpg'):
continue continue
found_urls.append(found_url[-11:]) 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:
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)
# 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:]}")
@ -68,10 +86,3 @@ def get(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()
# 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()