From c837828a229afeb9a0eaaecf0967a940641529eb Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 20 Jan 2021 17:35:24 +1300 Subject: [PATCH] Captions: Error checking --- extractors/captions.py | 6 ++++++ index.py | 24 ++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/extractors/captions.py b/extractors/captions.py index 00938a5..2df701a 100644 --- a/extractors/captions.py +++ b/extractors/captions.py @@ -29,6 +29,12 @@ def extract_captions_from_video(id): def extract_captions_from_api(id): url = "https://video.google.com/timedtext?hl=en&type=list&v=%s" % id with requests.get(url) as r: + if r.status_code == 404: + return { + "error": "Video unavailable", + "identifier": "NOT_FOUND" + } + r.raise_for_status() transcript = ET.fromstring(r.content.decode("utf8")) diff --git a/index.py b/index.py index d44be30..02ba194 100644 --- a/index.py +++ b/index.py @@ -92,17 +92,25 @@ class Second(object): @cherrypy.tools.json_out() def suggestions(self, *, q, **kwargs): return extract_search_suggestions(q) - + @cherrypy.expose def captions(self, id, **kwargs): - result = extract_captions(id, **kwargs) - if type(result) is dict: - cherrypy.response.headers["content-type"] = "application/json" - return bytes(json.dumps(result), "utf8") - else: - cherrypy.response.headers["content-type"] = "text/vtt; charset=UTF-8" - return result + try: + result = extract_captions(id, **kwargs) + if type(result) is dict: + cherrypy.response.headers["content-type"] = "application/json" + return bytes(json.dumps(result), "utf8") + else: + cherrypy.response.headers["content-type"] = "text/vtt; charset=UTF-8" + return result + except StopIteration: + cherrypy.response.status = "400" + cherrypy.response.headers["content-type"] = "application/json" + return bytes(json.dumps({ + "error": "No captions matching that language or label", + "identifier": "NO_MATCHING_CAPTIONS" + }), "utf8") @cherrypy.expose def vi(self, id, file):