- player_config = None
- for script in doc.xpath("//script"):
- if not script.text:
- continue
- for line in script.text.split("\n"):
- if "yt.playerConfig =" in line:
- p1 = line.find("=")
- p2 = line.rfind(";")
- if p1 >= 0 and p2 > 0:
- return json.loads(line[p1+1:p2])
- if "'PLAYER_CONFIG': " in line:
- p1 = line.find(":")
- if p1 >= 0:
- player_config = json.loads(line[p1+1:])
- convert_from_old_itag(player_config)
- return player_config
+ player_config = None
+ for script in doc.xpath("//script"):
+ if not script.text:
+ continue
+ for line in script.text.split("\n"):
+ s = "ytplayer.config = {"
+ if s in line:
+ p1 = line.find(s) + len(s) - 1
+ p2 = line.find("};", p1) + 1
+ if p1 >= 0 and p2 > 0:
+ return json.loads(line[p1:p2])
+
+def extract_function(output, script, func_name):
+ p1 = script.find("function " + func_name + "(")
+ p2 = script.find("}", p1)
+ code = script[p1:p2+1]
+ output.append(code)
+ deps = re.findall(R"[^\.][= ]([\$0-9a-zA-Z]+)\(", code)
+ deps = set(deps)
+ deps.remove(func_name)
+ for dep in deps:
+ extract_function(output, script, dep)
+
+def find_func_name(script):
+ FUNC_NAME = R"([a-zA-Z0-9$]+)"
+ FUNC_PARAMS = R"(\([a-zA-Z]+\.s\))"
+ PATTERN = FUNC_NAME + FUNC_PARAMS + ";"
+
+ match = re.search(PATTERN, script)
+ func_name = match.groups()[0]
+ return func_name
+
+def decode_signature(js_url, s):
+ script = urlopen(js_url).read()
+ func_name = find_func_name(script)
+
+ codes = []
+ extract_function(codes, script, func_name)
+
+ p = subprocess.Popen(
+ "js",
+ shell=True,
+ close_fds=True,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE
+ )
+ for code in codes:
+ p.stdin.write(code + "\n")
+ p.stdin.write("console.log(%s('%s'));\n" % (func_name, s))
+ p.stdin.close()
+
+ signature = p.stdout.read().strip()
+ if p.wait() != 0:
+ raise Exception("js failed to execute: %d" % p.returncode)
+
+ return signature