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
# Minimum duration for videos to be listed in the feed
MINIMUM_DURATION = timedelta(seconds=70)
def construct_rss(client: APIClient, channel_id: str) -> str:
"""
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
"""
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()
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"
for video in videos:
if video["duration"] >= MINIMUM_DURATION:
feed.items.append({
"guid": video["id"],
"title": video["title"],
"link": video["url"],
"description": (
f'<a href="{video["url"]}"><img src="{video["thumbnail"]}" /></a><br><br>'
+ video["description"]
),
"pubDate": video["published"].timetuple(),
})
feed.items.append({
"guid": video["id"],
"title": video["title"],
"link": video["url"],
"description": (
f'Durée: {video["duration"]}<br><br>'
+ f'<a href="{video["url"]}"><img src="{video["thumbnail"]}" /></a><br><br>'
+ video["description"]
),
"pubDate": video["published"].timetuple(),
})
return feed.format_rss2_string()