2021-09-12 13:42:57 +00:00
|
|
|
from ..feedformatter import Feed
|
2021-09-12 22:00:24 +00:00
|
|
|
from ..util import parse_iso
|
2021-09-12 13:42:57 +00:00
|
|
|
from .twitch import APIClient
|
|
|
|
|
|
|
|
|
|
|
|
def construct_rss(client: APIClient, login: str) -> str:
|
|
|
|
"""
|
|
|
|
Build a RSS stream for a Twitch user.
|
|
|
|
|
|
|
|
:param client: Twitch API client
|
|
|
|
:param login: user login
|
|
|
|
:returns: RSS stream
|
|
|
|
:raises HTTPException: if one of the requests fail
|
|
|
|
:raises NoSuchUser: if the user does not exist
|
|
|
|
"""
|
|
|
|
user_info = client.user(login)
|
|
|
|
channel_id = user_info["id"]
|
|
|
|
videos = client.videos(channel_id)
|
|
|
|
stream = client.stream(channel_id)
|
|
|
|
|
|
|
|
feed = Feed()
|
|
|
|
user_url = f"https://www.twitch.tv/{user_info['login']}"
|
|
|
|
|
|
|
|
# Set the feed/channel level properties
|
|
|
|
feed.feed["title"] = user_info["display_name"]
|
|
|
|
feed.feed["link"] = user_url
|
2021-09-12 22:00:24 +00:00
|
|
|
feed.feed["author"] = "Feedleware"
|
2021-09-12 13:42:57 +00:00
|
|
|
feed.feed["description"] = user_info["description"]
|
2021-09-12 22:00:24 +00:00
|
|
|
feed.feed["ttl"] = "10"
|
2021-09-12 13:42:57 +00:00
|
|
|
|
|
|
|
if stream is not None:
|
|
|
|
item = {}
|
|
|
|
item["guid"] = stream["id"]
|
|
|
|
item["title"] = "[⏺️ Live] " + stream.get("title", "Untitled Stream")
|
|
|
|
item["link"] = user_url
|
|
|
|
item["description"] = stream.get("game_name", "")
|
|
|
|
item["pubDate"] = parse_iso(stream["started_at"]).timetuple()
|
|
|
|
|
|
|
|
feed.items.append(item)
|
|
|
|
|
|
|
|
for video in videos:
|
|
|
|
if video.get("viewable", "public") != "public":
|
|
|
|
continue
|
|
|
|
|
|
|
|
item = {}
|
|
|
|
|
2021-09-13 13:33:35 +00:00
|
|
|
if video.get("type") not in ("upload", "archive"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if video.get("type") == "archive":
|
2021-09-12 13:42:57 +00:00
|
|
|
if stream is not None and stream["id"] == video["stream_id"]:
|
|
|
|
# Do not add a second item for the active stream
|
|
|
|
continue
|
|
|
|
|
|
|
|
item["guid"] = video["stream_id"]
|
|
|
|
else:
|
|
|
|
item["guid"] = video["id"]
|
|
|
|
|
|
|
|
item["title"] = video.get("title", "Untitled Video")
|
|
|
|
|
|
|
|
link = video.get("url", user_url)
|
|
|
|
thumbnail = video["thumbnail_url"] \
|
|
|
|
.replace("%{width}", "600") \
|
|
|
|
.replace("%{height}", "400")
|
|
|
|
|
|
|
|
item["link"] = link
|
|
|
|
item["description"] = f'<a href="{link}"><img src="{thumbnail}" /></a>'
|
|
|
|
item["pubDate"] = parse_iso(video["published_at"]).timetuple()
|
|
|
|
|
|
|
|
feed.items.append(item)
|
|
|
|
|
|
|
|
return feed.format_rss2_string()
|