youtubeprovider.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:xmusic-crawler 作者: rockers7414 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号