def __get_video_duration(self, video_id):
params = {
"key": self.key,
"part": "contentDetails",
"id": video_id
}
buf = BytesIO()
client = pycurl.Curl()
client.setopt(pycurl.URL, self.SERVICE_HOST + "/youtube/v3/videos?" +
urlencode(params))
client.setopt(pycurl.WRITEFUNCTION, buf.write)
client.perform()
client.close()
body = json.loads(buf.getvalue().decode("utf-8"))
buf.close()
if "error" in body:
raise Exception("query error: {0}".format(body))
if len(body["items"]) == 0:
raise Exception("result not found")
duration = body["items"][0]["contentDetails"]["duration"]
duration_seconds = isodate.parse_duration(duration).seconds
return duration_seconds
评论列表
文章目录