From b6102d0b4f93aa8b163784a66ae95a93690fab92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl?= Date: Wed, 10 Sep 2025 09:38:27 +0200 Subject: [PATCH] updating youtube --- post_rss_to_ghost.py | 55 ++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/post_rss_to_ghost.py b/post_rss_to_ghost.py index 179fbde..c9ac18a 100644 --- a/post_rss_to_ghost.py +++ b/post_rss_to_ghost.py @@ -18,14 +18,29 @@ from urllib.parse import urlparse, parse_qs # ------------- YouTube helpers ------------- -YOUTUBE_EMBED_TMPL = ( - '
' - '
' -) +def fetch_youtube_oembed_html(youtube_url: str, timeout: int = 10) -> str | None: + """ + Get YouTube oEmbed HTML exactly as provided and wrap it as a Ghost embed card. + """ + try: + resp = requests.get( + "https://www.youtube.com/oembed", + params={"url": youtube_url, "format": "json"}, + headers={"User-Agent": "ghost-bot/1.0"}, + timeout=timeout, + ) + resp.raise_for_status() + data = resp.json() + html = data.get("html") + if not html: + return None + # Wrap in Ghost embed card container; do NOT alter the iframe attributes. + return f'
{html}
' + except Exception: + return None + +def youtube_thumbnail_url(video_id: str) -> str: + return f"https://i.ytimg.com/vi/{video_id}/hqdefault.jpg" def extract_youtube_id(url: str) -> Optional[str]: try: @@ -242,13 +257,18 @@ class GhostTask: # --- YouTube embed / fallback vid = post.get("yt_videoid") or extract_youtube_id(linkURL) if vid: - # iframe (web) + thumbnail (email-safe) + lien - thumb = f"https://i.ytimg.com/vi/{vid}/hqdefault.jpg" - parts.append(YOUTUBE_EMBED_TMPL.format(vid=vid)) - parts.append(f'

Voir sur YouTube

') - parts.append(f'

YouTube thumbnail

') - if not first_image: - first_image = thumb + watch_url = f"https://www.youtube.com/watch?v={vid}" + + # Try provider HTML via oEmbed (as Ghost does) + embed_html = fetch_youtube_oembed_html(watch_url, timeout=10) + if embed_html: + parts.append(embed_html) + else: + # Fallback: leave the plain URL on its own line so Ghost may still auto-embed + parts.append(f'\n

{watch_url}

\n') + + # Minimal fallback link (non-intrusive for email/web) + parts.append(f'

Voir sur YouTube

') else: # --- Texte + lien ftext = "" @@ -267,6 +287,7 @@ class GhostTask: if link.get("type") in ("image/jpg", "image/jpeg", "image/png", "image/webp"): imgUrl = link.get("href") if imgUrl: + imgUrl = imgUrl.replace("/250x250/", "/990x320/") if not first_image: first_image = imgUrl parts.append(f'
') @@ -320,7 +341,7 @@ class GhostTask: # (Re)charge les feeds feeds_file = os.environ.get("FEEDS_FILE", "/data/feeds.txt") if not os.path.isfile(feeds_file): - feeds_file = os.environ.get("FEEDS_FILE_FALLBACK", r"c:\workspace\Substack_JV\feeds.txt") + feeds_file = os.environ.get("FEEDS_FILE_FALLBACK", r"f:\workspace\Substack_JV\feeds.txt") feeds: List[RSSfeed] = [] with open(feeds_file, encoding="utf-8") as f: lines = [line.strip() for line in f if line.strip()] @@ -393,7 +414,7 @@ async def main(): feeds: List[RSSfeed] = [] feeds_file = os.environ.get("FEEDS_FILE", "/data/feeds.txt") if not os.path.isfile(feeds_file): - feeds_file = os.environ.get("FEEDS_FILE_FALLBACK", r"c:\workspace\Substack_JV\feeds.txt") + feeds_file = os.environ.get("FEEDS_FILE_FALLBACK", r"f:\workspace\Substack_JV\feeds.txt") with open(feeds_file, encoding="utf-8") as f: for line in f: line = line.strip()