return json.loads(line[p1:p2])
def extract_js(script):
- PREFIX = "(function(){"
- SUFFIX = "})();\n"
+ PREFIX = "var _yt_player={};(function(g){var window=this;"
+ SUFFIX = ";})(_yt_player);\n"
assert script.startswith(PREFIX)
assert script.endswith(SUFFIX)
"code": json.dumps(extract_js(script)),
}
p = subprocess.Popen(
- "js",
+ "nodejs",
shell=True,
close_fds=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
js_decode_script = ("""
- var vm = require("vm");
+ var vm = require('vm');
var sandbox = {
- window: {
- location: {},
- history: {
- pushState: function(){}
- }
+ location: {
+ hash: '',
+ href: '',
+ protocol: 'http:'
+ },
+ history: {
+ pushState: function(){}
},
document: {},
- navigator: {},
+ navigator: {
+ userAgent: ''
+ },
signature: %(signature)s,
- transformed_signature: null
+ transformed_signature: null,
+ g: function(){} // this is _yt_player
};
+ sandbox.window = sandbox;
- var execstring = ";transformed_signature = %(func_name)s(signature);";
- vm.runInNewContext(%(code)s + execstring, sandbox);
+ var code_string = %(code)s + ';';
+ var exec_string = 'transformed_signature = %(func_name)s(signature);';
+ vm.runInNewContext(code_string + exec_string, sandbox);
console.log(sandbox.transformed_signature);
""" % params)