如何通过验证视频ID来防止youtube-dl中的“ HTTPS NOT SUPPORTED”错误?

时间:2019-07-09 22:30:54

标签: python validation youtube youtube-api pafy

我正在使用pafy模块检索YouTube视频音频的URL。 Pafy本身使用youtube-dl模块连接到youtube。 视频ID位于网址部分:“ / watch?v = videoID”

我存储了这些视频的videoId,但是不时不再有视频可用,因此我需要进行检查。 我尝试实施的检查未能发现一些极端情况,例如该视频对我国的版权禁令。

我已经尝试了2件事。 使用youtube oEmbed API并使用官方youtube API。

import requests


YT_API_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

answers = []
answers2 = []

def isValidVid(vId):

    add = f'https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v={str(vId)}'
    answer = requests.get(add)

    answers.append(answer)

    state = answer.ok
    if not state:
        print(f'{vId} has become invalid ')
    return state



def isValidVid2(vId):

    add = f'https://www.googleapis.com/youtube/v3/videos?part=id&id={vId}&key={YT_API_KEY}'
    answer = requests.get(add)

    answers2.append(answer)

    state = answer.ok
    if not state:
        print(f'{vId} has become invalid ')
    return state

vIds=['x26LZrX_vuI', #copyright blocked in my country
    'tRqCOIsTx8M', #works fine
    ]

for vid in vIds:
    isValidVid(vid)
    isValidVid2(vid)

for a,b in zip(answers,answers2):
    print(a.content.split(','),'\n')
    print(b.content.split(','),'\n'*2)

Runnig的这段摘要会导致以下输出(对不起,看上去很难看):

['b\'{"provider_name":"YouTube"', '"thumbnail_url":"https:\\\\/\\\\/i.ytimg.com\\\\/vi\\\\/x26LZrX_vuI\\\\/hqdefault.jpg"', '"type":"video"', '"thumbnail_width":480', '"width":459', '"version":"1.0"', '"thumbnail_height":360', '"author_url":"https:\\\\/\\\\/www.youtube.com\\\\/user\\\\/whiteassboy9"', '"author_name":"whiteassboy9"', '"height":344', '"provider_url":"https:\\\\/\\\\/www.youtube.com\\\\/"', '"html":"\\\\u003ciframe width=\\\\"459\\\\" height=\\\\"344\\\\" src=\\\\"https:\\\\/\\\\/www.youtube.com\\\\/embed\\\\/x26LZrX_vuI?feature=oembed\\\\" frameborder=\\\\"0\\\\" allow=\\\\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\\\\" allowfullscreen\\\\u003e\\\\u003c\\\\/iframe\\\\u003e"', '"title":"Cradle Of Filth Hallowed Be Thy Name Lyrics"}\'']

['b\'{\\n "kind": "youtube#videoListResponse"', '\\n "etag": "\\\\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/0OApMzmvJ5MISdLQorUKdgn8QAI\\\\""', '\\n "pageInfo": {\\n  "totalResults": 1', '\\n  "resultsPerPage": 1\\n }', '\\n "items": [\\n  {\\n   "kind": "youtube#video"', '\\n   "etag": "\\\\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/Sdc-dVIem9K8Yo5a4co2ZdNy_b8\\\\""', '\\n   "id": "x26LZrX_vuI"\\n  }\\n ]\\n}\\n\'']


['b\'{"version":"1.0"', '"provider_name":"YouTube"', '"thumbnail_url":"https:\\\\/\\\\/i.ytimg.com\\\\/vi\\\\/tRqCOIsTx8M\\\\/hqdefault.jpg"', '"author_url":"https:\\\\/\\\\/www.youtube.com\\\\/user\\\\/Kikuku94"', '"width":459', '"thumbnail_height":360', '"provider_url":"https:\\\\/\\\\/www.youtube.com\\\\/"', '"html":"\\\\u003ciframe width=\\\\"459\\\\" height=\\\\"344\\\\" src=\\\\"https:\\\\/\\\\/www.youtube.com\\\\/embed\\\\/tRqCOIsTx8M?feature=oembed\\\\" frameborder=\\\\"0\\\\" allow=\\\\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\\\\" allowfullscreen\\\\u003e\\\\u003c\\\\/iframe\\\\u003e"', '"height":344', '"type":"video"', '"thumbnail_width":480', '"author_name":"Kikuku94"', '"title":"Metallica - Disposable Heroes (Studio Version)"}\''] 

['b\'{\\n "kind": "youtube#videoListResponse"', '\\n "etag": "\\\\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/HP3fEMhAlfsUHWjDdwxAE4mcxLg\\\\""', '\\n "pageInfo": {\\n  "totalResults": 1', '\\n  "resultsPerPage": 1\\n }', '\\n "items": [\\n  {\\n   "kind": "youtube#video"', '\\n   "etag": "\\\\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/h2d05hbW0K56lbhpO-q8FN9qVdU\\\\""', '\\n   "id": "tRqCOIsTx8M"\\n  }\\n ]\\n}\\n\'']

如果我现在使用pafy在使用任何一种验证功能之后现在都检索音频资源的网址,则会在youtube-dl上引发错误,我认为这与您提供无效视频ID时遇到的错误相同:

mrl = pafy.new(vIds[0]).url

OSError: ERROR: HTTP is not supported.

我认为,如果您有一些我在听的技巧,那么与版权禁令(如私人视频)相比,还有更多的陷阱!

感谢您的帮助!

0 个答案:

没有答案