47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from datetime import timedelta
|
|
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.
|
|
|
|
: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)
|
|
videos = client.playlist(channel_info["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:
|
|
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(),
|
|
})
|
|
|
|
return feed.format_rss2_string()
|