52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from ..feedformatter import Feed
|
|
from ..util import parse_iso
|
|
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)
|
|
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:
|
|
item = {}
|
|
|
|
video_id = video["resourceId"]["videoId"]
|
|
link = f"https://www.youtube.com/watch?v={video_id}"
|
|
thumbnail = ""
|
|
|
|
for size in ("standard", "maxres", *video["thumbnails"].keys()):
|
|
if size in video["thumbnails"]:
|
|
thumbnail = video["thumbnails"][size]["url"]
|
|
|
|
item["guid"] = video["resourceId"]["videoId"]
|
|
item["title"] = video.get("title", "Untitled Video")
|
|
item["link"] = link
|
|
item["description"] = (
|
|
f'<a href="{link}"><img src="{thumbnail}" /></a><br><br>'
|
|
+ video["description"]
|
|
)
|
|
item["pubDate"] = parse_iso(video["publishedAt"]).timetuple()
|
|
|
|
feed.items.append(item)
|
|
|
|
return feed.format_rss2_string()
|