Skip to content

Commit

Permalink
Merge pull request #55 from corenting/fixes/media-bugfixes
Browse files Browse the repository at this point in the history
fix: fix some media issues
  • Loading branch information
corenting committed Jun 17, 2023
2 parents 50bd05b + 8ddfebe commit 8ad27a0
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 89 deletions.
2 changes: 1 addition & 1 deletion eddrit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from loguru import logger
from eddrit import config

__version__ = "0.5.6"
__version__ = "0.5.7"

logger.remove()
logger.add(sys.stderr, level=config.LOG_LEVEL)
10 changes: 0 additions & 10 deletions eddrit/const.py

This file was deleted.

12 changes: 5 additions & 7 deletions eddrit/reddit/content_parser/flair.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def get_post_flair(api_post_data: Dict[Hashable, Any]) -> Optional[models.Flair]
text_color = (
"black" if api_post_data["link_flair_text_color"] == "dark" else "white"
)
bg_color = api_post_data["link_flair_background_color"]
bg_color = api_post_data["link_flair_background_color"] or "lightblue"

if api_post_data.get("is_original_content", False):
flair_components.append(
Expand Down Expand Up @@ -55,14 +55,12 @@ def get_user_flair(api_post_data: Dict[Hashable, Any]) -> Optional[models.Flair]
flair_components = []

# Background color
bg_color = api_post_data["author_flair_background_color"]
if not bg_color or bg_color == "#ffffff":
bg_color = "#dadada"
bg_color = api_post_data["author_flair_background_color"] or "lightblue"

# Text color
text_color = api_post_data["author_flair_text_color"]
if not text_color:
text_color = "#0000"
text_color = (
"black" if api_post_data["author_flair_text_color"] == "dark" else "white"
)

if api_post_data.get("author_flair_richtext"):
for part in api_post_data.get("author_flair_richtext", []):
Expand Down
25 changes: 9 additions & 16 deletions eddrit/reddit/content_parser/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@


from eddrit import models
from eddrit.utils.media import (
post_is_from_domain,
)
from loguru import logger

from eddrit.reddit.content_parser import video_parsers
from eddrit.utils.urls import get_domain_and_suffix_from_url


def _post_is_an_imgur_gif(api_post_data: Dict[Hashable, Any]) -> bool:
"""Check if a post is an imgur gif by checking domain and url file extension."""
return (
post_is_from_domain(api_post_data["domain"], "imgur.com")
get_domain_and_suffix_from_url(api_post_data["domain"]) == "imgur.com"
and ".gif" in api_post_data["url"]
)

Expand Down Expand Up @@ -111,19 +109,14 @@ def get_post_video_content(
video_parsers.get_reddit_video_preview,
]

# Special case for twitch, the embedly embed
# Content-Security-Policy prevents including it
if post_is_from_domain(api_post_data["domain"], "twitch.tv"):
parsers.append(video_parsers.get_twitch_embed)
post_domain = get_domain_and_suffix_from_url(api_post_data["domain"])

# Special case for imgur gif/gifv, it's easier to get the mp4 directly from the URL
if _post_is_an_imgur_gif(api_post_data):
parsers.append(video_parsers.get_imgur_gif)

# Special case for gfycat, some old links are not embed
# but it can be converted to it.
if post_is_from_domain(api_post_data["domain"], "gfycat.com"):
parsers.append(video_parsers.get_gfycat_embed)
# Special case for some embeds
domains_with_special_embed_handling = (
video_parsers.get_domains_with_special_embed_handling()
)
if post_domain in domains_with_special_embed_handling.keys():
parsers.append(domains_with_special_embed_handling[post_domain])

parsed_results: list[models.PostVideo | models.EmbedPostContent] = []
for parser in parsers:
Expand Down
37 changes: 31 additions & 6 deletions eddrit/reddit/content_parser/video_parsers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import html
from typing import Any, Dict, Hashable
import re
from typing import Any, Callable, Dict, Hashable

import lxml.html
import tldextract

from eddrit import models
from eddrit.utils.media import domain_has_special_embed_handling
from eddrit.utils.middlewares import get_current_host


def _domain_has_special_embed_handling(domain: str) -> bool:
"""Check if the given domain is a domain that has a special code for embed handling."""
_, domain, suffix = tldextract.extract(domain)
return f"{domain}.{suffix}" in get_domains_with_special_embed_handling().keys()


def _cleanup_embed(content: str) -> str:
"""Cleanup embed content for embed posts"""
content_parsed = lxml.html.fromstring(content)
Expand All @@ -20,13 +27,31 @@ def _cleanup_embed(content: str) -> str:
return lxml.html.tostring(content_parsed).decode("utf-8")


def get_domains_with_special_embed_handling() -> dict[str, Callable]:
"""Return dict of domain associated with parsing function
of domains that have a special handling for their embed
and should not be parsed with the generic embed code"""
return {"twitch.tv": get_twitch_embed, "gfycat.com": get_gfycat_embed}


def get_twitch_embed(api_post_data: Dict[Hashable, Any]) -> models.EmbedPostContent:
"""Fetch twitch embed directly as the one in the API has
a Content-Security-Policy preventing including it.
"""
embed_url = api_post_data["url"].replace(
"clips.twitch.tv/", "clips.twitch.tv/embed?clip="
)

# There are two format of clips URLs
if "clips.twitch.tv" in api_post_data:
embed_url = api_post_data["url"].replace(
"clips.twitch.tv/", "clips.twitch.tv/embed?clip="
)
elif regex_clip_id := re.search(
"https://www.twitch.tv/.*/clip/(.*)", api_post_data["url"]
):
clip_id = regex_clip_id.groups(0)[0]
embed_url = f"https://clips.twitch.tv/embed?clip={clip_id}"
else:
raise ValueError("Cannot parse Twitch embed")

parent = get_current_host()
embed_code = f'<iframe src="{embed_url}&parent={parent}" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>'
return models.EmbedPostContent(
Expand Down Expand Up @@ -71,7 +96,7 @@ def get_imgur_gif(api_post_data: Dict[Hashable, Any]) -> models.PostVideo:


def get_embed_content(api_post_data: Dict[Hashable, Any]) -> models.EmbedPostContent:
if domain_has_special_embed_handling(api_post_data["url"]):
if _domain_has_special_embed_handling(api_post_data["url"]):
raise ValueError("The post domain cannot be parsed with get_embed_content")

embed_data = api_post_data["secure_media"]["oembed"]
Expand Down
21 changes: 14 additions & 7 deletions eddrit/reddit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import timeago

from eddrit import models
from eddrit.const import STATIC_RES_PATH_REPLACEMENT
from eddrit.reddit.content_parser.flair import get_post_flair, get_user_flair
from eddrit.reddit.content_parser.media import (
get_post_gallery_content,
Expand All @@ -14,7 +13,16 @@
post_has_video_content,
)
from eddrit.utils.math import pretty_big_num
from eddrit.utils.media import is_media_hosting_domain
from eddrit.utils.urls import get_domain_and_suffix_from_url

# Constant used in templates to be replaced by the static path
STATIC_RES_PATH_REPLACEMENT = "$STATIC_RES_PATH"

# Domains that may be used in post of type link but that are majorly used for image hosting and should be parsed as such
IMAGE_HOSTING_DOMAINS = ["imgur.com"]

# Media domains to display as links (embed that cannot be displayed, scripts needed etc.)
MEDIA_DOMAINS_TO_DISPLAY_AS_LINK = ["tiktok.com"]


def get_post_content(api_post_data: Dict[Hashable, Any]) -> models.PostContentBase:
Expand All @@ -35,13 +43,12 @@ def get_post_content(api_post_data: Dict[Hashable, Any]) -> models.PostContentBa
# Media posts
hint = api_post_data.get("post_hint")
has_video_content = post_has_video_content(api_post_data)
post_domain = get_domain_and_suffix_from_url(api_post_data["domain"])
if (
hint == "image"
or hint == "hosted:video"
or hint == "rich:video"
or (hint == "link" and is_media_hosting_domain(api_post_data["domain"]))
hint in ["image", "hosted:video", "rich:video"]
or (hint == "link" and post_domain in IMAGE_HOSTING_DOMAINS)
or has_video_content
):
) and post_domain not in MEDIA_DOMAINS_TO_DISPLAY_AS_LINK:
# Check if image has video (then consider video) else consider image
if has_video_content:
return get_post_video_content(api_post_data)
Expand Down
23 changes: 0 additions & 23 deletions eddrit/utils/media.py

This file was deleted.

7 changes: 7 additions & 0 deletions eddrit/utils/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import tldextract


def get_domain_and_suffix_from_url(url: str) -> str:
"""Get domain name and suffix from url"""
_, domain, suffix = tldextract.extract(url)
return f"{domain}.{suffix}"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "eddrit"
version = "0.5.6"
version = "0.5.7"
description = "Alternative Reddit frontend"
authors = ["corenting <corenting@gmail.com>"]
license = "MIT"
Expand Down
6 changes: 3 additions & 3 deletions templates/macros/comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
{% macro render_author(comment, subreddit_name) %}
{% set author_tag = '' %}
{% if comment.is_submitter %}
<span class="post-flair post-comment-title-item" style="background-color: blue;">
<span class="post-flair post-comment-title-item" style="color: white; background-color: darkblue;">
<span>
{{ comment.author.name }}
</span>
</span>
{% set author_tag = '<abbr title="Submitter">[S]</abbr>' %}
{% elif comment.is_admin %}
<span class="post-flair post-comment-title-item" style="background-color: red;">
<span class="post-flair post-comment-title-item" style="color: white; background-color: red;">
<span>
{{ comment.author.name }}
</span>
</span>
{% set author_tag = '<abbr title="Reddit admin">[A]</abbr>' %}
{% elif comment.is_moderator %}
<span class="post-flair post-comment-title-item" style="background-color: green;">
<span class="post-flair post-comment-title-item" style="color: white; background-color: green;">
<span>
{{ comment.author.name }}
</span>
Expand Down
2 changes: 1 addition & 1 deletion templates/macros/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
</div>
{% endif %}
{% if post.content.type.value == 'video' %}
<div class="post-content-image-or-video needs-js post-content-video"
<div class="post-content-image-or-video needs-js"
style="{{ 'width: fit-content' if post.content.videos[0].width < post.content.videos[0].height else 'height: fit-content;'}} ">
<video class="video-js" id="video-{{ post.id }}" data-content='{{ post.content | tojson_dataclass }}'/>
</div>
Expand Down
14 changes: 0 additions & 14 deletions tests/utils/test_media.py

This file was deleted.

0 comments on commit 8ad27a0

Please sign in to comment.