mirror of
https://git.sr.ht/~cadence/NewLeaf
synced 2024-11-14 20:17:29 +00:00
Captions: Error checking
This commit is contained in:
parent
8e69928756
commit
c837828a22
@ -29,6 +29,12 @@ def extract_captions_from_video(id):
|
|||||||
def extract_captions_from_api(id):
|
def extract_captions_from_api(id):
|
||||||
url = "https://video.google.com/timedtext?hl=en&type=list&v=%s" % id
|
url = "https://video.google.com/timedtext?hl=en&type=list&v=%s" % id
|
||||||
with requests.get(url) as r:
|
with requests.get(url) as r:
|
||||||
|
if r.status_code == 404:
|
||||||
|
return {
|
||||||
|
"error": "Video unavailable",
|
||||||
|
"identifier": "NOT_FOUND"
|
||||||
|
}
|
||||||
|
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
transcript = ET.fromstring(r.content.decode("utf8"))
|
transcript = ET.fromstring(r.content.decode("utf8"))
|
||||||
|
22
index.py
22
index.py
@ -95,14 +95,22 @@ class Second(object):
|
|||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def captions(self, id, **kwargs):
|
def captions(self, id, **kwargs):
|
||||||
result = extract_captions(id, **kwargs)
|
try:
|
||||||
if type(result) is dict:
|
result = extract_captions(id, **kwargs)
|
||||||
cherrypy.response.headers["content-type"] = "application/json"
|
if type(result) is dict:
|
||||||
return bytes(json.dumps(result), "utf8")
|
cherrypy.response.headers["content-type"] = "application/json"
|
||||||
else:
|
return bytes(json.dumps(result), "utf8")
|
||||||
cherrypy.response.headers["content-type"] = "text/vtt; charset=UTF-8"
|
else:
|
||||||
return result
|
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
|
@cherrypy.expose
|
||||||
def vi(self, id, file):
|
def vi(self, id, file):
|
||||||
|
Loading…
Reference in New Issue
Block a user