def urlopen(url, offset=None):
if url.startswith("//"):
- url = "http:" + url
+ url = "https:" + url
+ if not url.startswith("http://") and not url.startswith("https://"):
+ url = "https://www.youtube.com" + url
global referrer
req = urllib2.Request(url)
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)
def find_func_name(script):
FUNC_NAME = R"([a-zA-Z0-9$]+)"
FUNC_PARAMS = R"(\([a-zA-Z]+\.s\))"
- PATTERN = FUNC_NAME + FUNC_PARAMS + ";"
+ TERMINATOR = R"[,;\)]"
+ PATTERN = FUNC_NAME + FUNC_PARAMS + TERMINATOR
match = re.search(PATTERN, script)
func_name = match.groups()[0]
"code": json.dumps(extract_js(script)),
}
p = subprocess.Popen(
- "node",
+ "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)