1
0
mirror of https://git.sr.ht/~cadence/NewLeaf synced 2024-11-22 07:37:29 +00:00

Fix for if channel has no videos

This commit is contained in:
Cadence Ember 2020-10-03 01:17:23 +13:00
parent 097b6bf584
commit e1bcc306b3
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17

View File

@ -21,65 +21,73 @@ def extract_channel(ucid):
with requests.get("https://www.youtube.com/{}/{}/videos".format(channel_type, ucid)) as r: with requests.get("https://www.youtube.com/{}/{}/videos".format(channel_type, ucid)) as r:
r.raise_for_status() r.raise_for_status()
yt_initial_data = extract_yt_initial_data(r.content.decode("utf8")) yt_initial_data = extract_yt_initial_data(r.content.decode("utf8"))
header = yt_initial_data["header"]["c4TabbedHeaderRenderer"] header = yt_initial_data["header"]["c4TabbedHeaderRenderer"]
author = header["title"] author = header["title"]
author_id = header["channelId"] author_id = header["channelId"]
author_url = header["navigationEndpoint"]["commandMetadata"]["webCommandMetadata"]["url"] author_url = header["navigationEndpoint"]["commandMetadata"]["webCommandMetadata"]["url"]
subscriber_count = combine_runs(header["subscriberCountText"])
description = yt_initial_data["metadata"]["channelMetadataRenderer"]["description"]
allowed_regions = yt_initial_data["metadata"]["channelMetadataRenderer"]["availableCountryCodes"]
author_banners = [] author_banners = []
if "banner" in header: if "banner" in header:
author_banners = header["banner"]["thumbnails"] author_banners = header["banner"]["thumbnails"]
for t in author_banners: for t in author_banners:
t["url"] = normalise_url_protocol(t["url"]) t["url"] = normalise_url_protocol(t["url"])
author_thumbnails = [] author_thumbnails = []
if "avatar" in header: if "avatar" in header:
author_thumbnails = generate_full_author_thumbnails(header["avatar"]["thumbnails"]) author_thumbnails = generate_full_author_thumbnails(header["avatar"]["thumbnails"])
subscriber_count = combine_runs(header["subscriberCountText"])
description = yt_initial_data["metadata"]["channelMetadataRenderer"]["description"] latest_videos = []
allowed_regions = yt_initial_data["metadata"]["channelMetadataRenderer"]["availableCountryCodes"]
tabs = yt_initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"] tabs = yt_initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]
videos_tab = next(tab["tabRenderer"] for tab in tabs if tab["tabRenderer"]["title"] == "Videos") videos_tab = next(tab["tabRenderer"] for tab in tabs if tab["tabRenderer"]["title"] == "Videos")
videos = ( tab_parts = videos_tab["content"]["sectionListRenderer"]["contents"][0]["itemSectionRenderer"]["contents"][0]
v["gridVideoRenderer"] for v in
videos_tab["content"]["sectionListRenderer"]["contents"][0]["itemSectionRenderer"]["contents"][0]["gridRenderer"]["items"] # check that the channel actually has videos - this may be replaced
) # with messageRenderer.text.simpleText == "This channel has no videos."
latest_videos = [] if "gridVideoRenderer" in tab_parts:
for v in videos: videos = (
live = True v["gridVideoRenderer"] for v in tab_parts["gridRenderer"]["items"]
length_text = "LIVE" )
length_seconds = -1 for v in videos:
for o in v["thumbnailOverlays"]: live = True
if "thumbnailOverlayTimeStatusRenderer" in o: length_text = "LIVE"
length_text = combine_runs(o["thumbnailOverlayTimeStatusRenderer"]["text"]) length_seconds = -1
if o["thumbnailOverlayTimeStatusRenderer"]["style"] != "LIVE": for o in v["thumbnailOverlays"]:
length_seconds = length_text_to_seconds(length_text) if "thumbnailOverlayTimeStatusRenderer" in o:
live = False length_text = combine_runs(o["thumbnailOverlayTimeStatusRenderer"]["text"])
published = 0 if o["thumbnailOverlayTimeStatusRenderer"]["style"] != "LIVE":
published_text = "Live now" length_seconds = length_text_to_seconds(length_text)
if "publishedTimeText" in v: live = False
published_text = v["publishedTimeText"]["simpleText"] published = 0
published = past_text_to_time(published_text) published_text = "Live now"
latest_videos.append({ if "publishedTimeText" in v:
"type": "video", published_text = v["publishedTimeText"]["simpleText"]
"title": combine_runs(v["title"]), published = past_text_to_time(published_text)
"videoId": v["videoId"], latest_videos.append({
"author": author, "type": "video",
"authorId": author_id, "title": combine_runs(v["title"]),
"authorUrl": author_url, "videoId": v["videoId"],
"videoThumbnails": generate_video_thumbnails(v["videoId"]), "author": author,
"description": "", "authorId": author_id,
"descriptionHtml": "", "authorUrl": author_url,
"viewCount": view_count_text_to_number(combine_runs(v["viewCountText"])), "videoThumbnails": generate_video_thumbnails(v["videoId"]),
"second__viewCountText": combine_runs(v["viewCountText"]), "description": "",
"second__viewCountTextShort": combine_runs(v["shortViewCountText"]), "descriptionHtml": "",
"published": published, "viewCount": view_count_text_to_number(combine_runs(v["viewCountText"])),
"publishedText": published_text, "second__viewCountText": combine_runs(v["viewCountText"]),
"lengthSeconds": length_seconds, "second__viewCountTextShort": combine_runs(v["shortViewCountText"]),
"second__lengthText": length_text, "published": published,
"liveNow": live, "publishedText": published_text,
"paid": None, "lengthSeconds": length_seconds,
"premium": None, "second__lengthText": length_text,
"isUpcoming": None "liveNow": live,
}) "paid": None,
"premium": None,
"isUpcoming": None
})
channel = { channel = {
"author": author, "author": author,