from datetime import timedelta from ..feedformatter import Feed from .youtube import APIClient def construct_rss(client: APIClient, channel_id: str) -> str: """ Build a RSS stream for a YouTube channel. :param client: YouTube API client :param channel_id: channel ID :returns: RSS stream :raises HTTPException: if one of the requests fail :raises NoSuchChannel: if the channel does not exist """ channel_info = client.channel(channel_id) # Retrieve channel playlist which excludes short videos # see videos_playlist = "UULF" + channel_id.removeprefix("UC") videos = client.playlist(videos_playlist) feed = Feed() channel_url = f"https://www.youtube.com/channel/{channel_id}" # Set the feed/channel level properties feed.feed["title"] = channel_info["title"] feed.feed["link"] = channel_url feed.feed["author"] = "Feedleware" feed.feed["description"] = channel_info["description"] feed.feed["ttl"] = "30" for video in videos: feed.items.append({ "guid": video["id"], "title": video["title"], "link": video["url"], "description": ( f'Durée: {video["duration"]}

' + f'

' + video["description"] ), "pubDate": video["published"].timetuple(), }) return feed.format_rss2_string()