diff --git a/html/post-div.html b/html/post-div.html index b02c71e..d81e8ed 100644 --- a/html/post-div.html +++ b/html/post-div.html @@ -13,8 +13,8 @@
-
+
-
\ No newline at end of file + diff --git a/html/style.css b/html/style.css index c485536..7cb8aee 100644 --- a/html/style.css +++ b/html/style.css @@ -197,10 +197,15 @@ button:hover { align-items: center; } +.preview_wrapper { + display: flex; + overflow: auto; +} + .preview.full img, .preview.full video { max-width: 100vw; max-height: 100vh; width: 100%; height: 100%; object-fit: contain; -} \ No newline at end of file +} diff --git a/utilities.py b/utilities.py index 3a11066..eae0ff6 100644 --- a/utilities.py +++ b/utilities.py @@ -135,7 +135,6 @@ def get_post_html(post): def save_media(post, location): """Takes a post object and tries to download any image/video it might be associated with. If it can, it will return the filename.""" - url = post.url stripped_url = url.split("?")[0] if url.endswith(post.permalink): return None @@ -145,6 +144,42 @@ def save_media(post, location): domain = ".".join(post.url.split("/")[2].split(".")[-2:]) readable_name = list(filter(bool, post.permalink.split("/")))[-1] + # Handle galleries + # When we saved a cross_post, we don't know if it is a gallery. + if hasattr(post, "gallery_data") or ("gallery" in url and hasattr(post, "crosspost_parent_list")): + + if not hasattr(post, "gallery_data") and hasattr(post, "crosspost_parent_list"): + for crosspost in post.crosspost_parent_list: + if crosspost["gallery_data"] is not None: + # hard hack + post.gallery_data = crosspost["gallery_data"] + post.media_metadata = crosspost["media_metadata"] + break + if not hasattr(post, "gallery_data"): return None + + images = [ ] + for item in sorted(post.gallery_data['items'], key=lambda x: x['id']): + media_id = item['media_id'] + meta = post.media_metadata[media_id] + source = meta['s'] + if meta['e'] == 'Image': + url = source['u'] + elif meta['e'] == 'AnimatedImage': + url = source['gif'] + else: + return None + stripped_url = url.split("?")[0] + extension = stripped_url.split(".")[-1].lower() + filename = f"{readable_name}_{post.id}_{media_id}.{extension}" + try: + response = requests.get(url) + with open(os.path.join(location, "media", filename), "wb") as f: + f.write(response.content) + images.append(filename) + except: + print(f"Failed to download {url}") + return images if len(images) > 0 else None + # If it's an imgur gallery, forget it if domain == "imgur.com" and "gallery" in url: return None @@ -159,7 +194,7 @@ def save_media(post, location): if media_type.startswith("image") or media_type.startswith("video"): with open(os.path.join(location, "media", filename), "wb") as f: f.write(response.content) - return filename + return [ filename ] # Is this a v.redd.it link? if domain == "redd.it": @@ -171,7 +206,7 @@ def save_media(post, location): extension = name.split(".")[-1] filename = f"{readable_name}_{post.id}.{extension}" os.rename(name, os.path.join(location, "media", filename)) - return filename + return [ filename ] except: os.chdir(current) return None @@ -199,7 +234,7 @@ def save_media(post, location): filename = f"{readable_name}_{post.id}.{extension}" with open(os.path.join(location, "media", filename), "wb") as f: f.write(response.content) - return filename + return [ filename ] # Try to use youtube_dl if it's one of the possible domains if domain in PLATFORMS: @@ -218,26 +253,23 @@ def save_media(post, location): return for f in os.listdir(os.path.join(location, "media")): if f.startswith(f"{readable_name}_{post.id}"): - return f + return [ f ] def add_media_preview_to_html(post_html, media): """Takes post HTML and returns a modified version with the preview inserted.""" - extension = media.split(".")[-1] - location = "/".join(["media", media]) - if extension in IMAGE_EXTENSIONS: - return post_html.replace( - "", - f'' - ) - if extension in VIDEO_EXTENSIONS: - return post_html.replace( - "", - f'' - ) - return post_html + media_html_list = [] + + for m in media: + extension = m.split(".")[-1] + location = "/".join(["media", m]) + if extension in IMAGE_EXTENSIONS: + media_html_list.append(f'') + if extension in VIDEO_EXTENSIONS: + media_html_list.append(f'') + return post_html.replace('', ''.join([ f"
{item}
" for item in media_html_list])) def create_post_page_html(post, post_html):