Sorry for the interruption. We have been receiving a large volume of requests from your network.

To continue with your YouTube experience, please fill out the form below.

""" in wpage.text and not wpage.status_code == 429 and 'window["ytInitialPlayerResponse"] = ' in wpage.text and 'window["ytInitialData"] = ' in wpage.text: break else: print("Captcha detected, waiting 30 seconds") sleep(30) wptext = wpage.text initplay = None initdata = None recvids = set() recchans = set() recmixes = set() recplayl = set() pparams = ( ("v", vid), #("lang", langcode), ("action_mde_edit_form", 1), ("bl", "vmp"), ("ui", "hd"), ("tab", "captions"), #('forceedit', 'captions'), ("lang","aa") ) #mysession.headers.update({"cookie": "HSID="+cookies["HSID"]+"; SSID="+cookies["SSID"]+"; SID="+cookies["SID"], "Accept-Language": "en-US",}) ccenabled = not "yet enabled community" in mysession.get("https://www.youtube.com/timedtext_editor", params=pparams).text #print(mysession.get("https://www.youtube.com/timedtext_editor", params=pparams).text) creditdata = {} for line in wptext.splitlines(): if line.strip().startswith('window["ytInitialPlayerResponse"] = '): initplay = loads(line.split('window["ytInitialPlayerResponse"] = ', 1)[1].strip()[:-1]) if not ccenabledonly: if initplay["playabilityStatus"]["status"] == "ERROR": print(vid, "unavailable") return False, {}, recvids, recchans, recmixes, recplayl if "endscreen" in initplay.keys(): if "endscreenRenderer" in initplay["endscreen"].keys(): for el in initplay["endscreen"]["endscreenRenderer"]: if type(el) == Dict: elint = el["endscreenElementRenderer"] if "endscreenElementRenderer" in el.keys(): if elint["style"] == "VIDEO": recvids.add(elint["endpoint"]["watchEndpoint"]["videoId"]) elif elint["style"] == "CHANNEL": try: recchans.add(elint["endpoint"]["browseEndpoint"]["browseId"]) except: print("Channel endscreen error") raise elif elint["style"] == "PLAYLIST": recvids.add(elint["endpoint"]["watchEndpoint"]["videoId"]) recplayl.add(elint["endpoint"]["watchEndpint"]["playlistId"]) """ if "captions" in initplay.keys(): ccenabled = "contribute" in initplay["captions"]["playerCaptionsRenderer"] else: ccenabled = False # if captions information is not present, community contributions are not enabled """ if not ccenabledonly: if "videoDetails" in initplay.keys(): if "channelId" in initplay["videoDetails"].keys(): recchans.add(initplay["videoDetails"]["channelId"]) elif line.strip().startswith('window["ytInitialData"] = '): if not ccenabledonly: initdata = loads(line.split('window["ytInitialData"] = ', 1)[1].strip()[:-1]) if "contents" in initdata.keys(): #prevent exception try: if "results" in initdata["contents"]["twoColumnWatchNextResults"]["secondaryResults"]["secondaryResults"].keys(): for recmd in initdata["contents"]["twoColumnWatchNextResults"]["secondaryResults"]["secondaryResults"]["results"]: #auto is like the others if "compactAutoplayRenderer" in recmd.keys(): recmd = recmd["compactAutoplayRenderer"]["contents"][0] if "compactVideoRenderer" in recmd.keys(): recvids.add(recmd["compactVideoRenderer"]["videoId"]) try: recchans.add(recmd["compactVideoRenderer"]["channelId"]) except KeyError as e: try: recchans.add(recmd["compactVideoRenderer"]["longBylineText"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["browseId"]) except KeyError as e: print("Channel extract error") #raise #print("Unable to extract channel:") #print(recmd["compactVideoRenderer"]) elif "compactPlaylistRenderer" in recmd.keys(): recplayl.add(recmd["compactPlaylistRenderer"]["playlistId"]) if "navigationEndpoint" in recmd["compactPlaylistRenderer"].keys(): recvids.add(recmd["compactPlaylistRenderer"]["navigationEndpoint"]["watchEndpoint"]["videoId"]) if "navigationEndpoint" in recmd["compactPlaylistRenderer"]["shortBylineText"].keys(): recchans.add(recmd["compactPlaylistRenderer"]["shortBylineText"]["navigationEndpoint"]["browseEndpoint"]["browseId"]) elif "compactRadioRenderer" in recmd.keys(): #mix playlist recmixes.add(recmd["compactRadioRenderer"]["playlistId"]) # todo: find out if channels can be suggested except BaseException as e: print(e) print("Exception in discovery, continuing anyway") if initplay and (initdata or ccenabledonly): break return ccenabled, recvids, recchans, recmixes, recplayl if __name__ == "__main__": from sys import argv from requests import session from os.path import isfile from os import environ mysession = session() #HSID, SSID, SID cookies required if "HSID" in environ.keys() and "SSID" in environ.keys() and "SID" in environ.keys(): cookies = {"HSID": environ["HSID"], "SSID": environ["SSID"], "SID": environ["SID"]} elif isfile("config.json"): cookies = loads(open("config.json").read()) else: print("HSID, SSID, and SID cookies from youtube.com are required. Specify in config.json or as environment variables.") assert False if not (cookies["HSID"] and cookies["SSID"] and cookies["SID"]): print("HSID, SSID, and SID cookies from youtube.com are required. Specify in config.json or as environment variables.") assert False mysession.headers.update({"cookie": "HSID="+cookies["HSID"]+"; SSID="+cookies["SSID"]+"; SID="+cookies["SID"], "Accept-Language": "en-US",}) vidl = argv vidl.pop(0) for video in vidl: print(getmetadata(mysession, video))