youtube: List only long-form videos

This commit is contained in:
Mattéo Delabre 2025-02-03 13:49:16 -05:00
parent 6f7852eedb
commit 74946e744f
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
1 changed files with 16 additions and 16 deletions

View File

@ -3,10 +3,6 @@ from ..feedformatter import Feed
from .youtube import APIClient from .youtube import APIClient
# Minimum duration for videos to be listed in the feed
MINIMUM_DURATION = timedelta(seconds=70)
def construct_rss(client: APIClient, channel_id: str) -> str: def construct_rss(client: APIClient, channel_id: str) -> str:
""" """
Build a RSS stream for a YouTube channel. Build a RSS stream for a YouTube channel.
@ -18,7 +14,11 @@ def construct_rss(client: APIClient, channel_id: str) -> str:
:raises NoSuchChannel: if the channel does not exist :raises NoSuchChannel: if the channel does not exist
""" """
channel_info = client.channel(channel_id) channel_info = client.channel(channel_id)
videos = client.playlist(channel_info["playlist"])
# Retrieve channel playlist which excludes short videos
# see <https://stackoverflow.com/a/76602819/3452708>
videos_playlist = "UULF" + channel_id.removeprefix("UC")
videos = client.playlist(videos_playlist)
feed = Feed() feed = Feed()
channel_url = f"https://www.youtube.com/channel/{channel_id}" channel_url = f"https://www.youtube.com/channel/{channel_id}"
@ -31,16 +31,16 @@ def construct_rss(client: APIClient, channel_id: str) -> str:
feed.feed["ttl"] = "30" feed.feed["ttl"] = "30"
for video in videos: for video in videos:
if video["duration"] >= MINIMUM_DURATION: feed.items.append({
feed.items.append({ "guid": video["id"],
"guid": video["id"], "title": video["title"],
"title": video["title"], "link": video["url"],
"link": video["url"], "description": (
"description": ( f'Durée: {video["duration"]}<br><br>'
f'<a href="{video["url"]}"><img src="{video["thumbnail"]}" /></a><br><br>' + f'<a href="{video["url"]}"><img src="{video["thumbnail"]}" /></a><br><br>'
+ video["description"] + video["description"]
), ),
"pubDate": video["published"].timetuple(), "pubDate": video["published"].timetuple(),
}) })
return feed.format_rss2_string() return feed.format_rss2_string()