Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gallery support #17

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions html/post-div.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ <h2><!--title--></h2>
<div class="body">
<!--body-->
</div>
<div class="preview">
<div class="preview_wrapper">
<!--preview-->
</div>

<!--postend--></div>
<!--postend--></div>
7 changes: 6 additions & 1 deletion html/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
68 changes: 50 additions & 18 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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":
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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(
"<!--preview-->",
f'<img src="{location}">'
)
if extension in VIDEO_EXTENSIONS:
return post_html.replace(
"<!--preview-->",
f'<video controls><source src="{location}"></video>'
)
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'<img src="{location}">')
if extension in VIDEO_EXTENSIONS:
media_html_list.append(f'<video controls><source src="{location}"></video>')
return post_html.replace('<!--preview-->', ''.join([ f"<div class=\"preview\">{item}</div>" for item in media_html_list]))


def create_post_page_html(post, post_html):
Expand Down