Detect channels that do not exist

If error alerts exist, they will be logged. But it is reasonable to
assume that not all errors will be fatal, so we don't necessarily quit
parsing if we find one.

This also normalises the text error of the /latest response for a
missing channel, without changing its identifier.
This commit is contained in:
Cadence Ember 2021-05-02 01:20:53 +12:00
parent 50a4b7af45
commit 57b0a88a2e
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
1 changed files with 11 additions and 1 deletions

View File

@ -22,6 +22,16 @@ def extract_channel(ucid):
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"))
for alert in yt_initial_data.get("alerts", []):
alert_text = combine_runs(alert["alertRenderer"]["text"])
if alert_text == "This channel does not exist.":
return {
"error": alert_text,
"identifier": "NOT_FOUND"
}
else:
print("Seen alert text '{}'".format(alert_text))
header = yt_initial_data["header"]["c4TabbedHeaderRenderer"] if "c4TabbedHeaderRenderer" in yt_initial_data["header"] else {} header = yt_initial_data["header"]["c4TabbedHeaderRenderer"] if "c4TabbedHeaderRenderer" in yt_initial_data["header"] else {}
channel_metadata = yt_initial_data["metadata"]["channelMetadataRenderer"] channel_metadata = yt_initial_data["metadata"]["channelMetadataRenderer"]
@ -146,7 +156,7 @@ def extract_channel_latest(ucid):
if r.status_code == 404: if r.status_code == 404:
cherrypy.response.status = 404 cherrypy.response.status = 404
return { return {
"error": "Channel does not exist.", "error": "This channel does not exist.",
"identifier": "NOT_FOUND" "identifier": "NOT_FOUND"
} }