]> code.delx.au - youtube-cgi/blobdiff - youtube.cgi
fixes for recent changes
[youtube-cgi] / youtube.cgi
index 7e7645614e9dcaa159aa9c843185cb485e427adf..56b89371a968318a7851f0209785097039192df5 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python2
 
 from __future__ import division
 
@@ -79,6 +79,9 @@ urlopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
 referrer = ""
 
 def urlopen(url, offset=None):
+       if url.startswith("//"):
+               url = "http:" + url
+
        global referrer
        req = urllib2.Request(url)
        if referrer:
@@ -114,43 +117,25 @@ def append_to_qs(url, params):
        url = urlparse.urlunsplit(r)
        return url
 
-def convert_from_old_itag(player_config):
-       url_data = urlparse.parse_qs(player_config["args"]["url_encoded_fmt_stream_map"])
-       url_data["url"] = []
-       for itag_url in url_data["itag"]:
-               pos = itag_url.find("url=")
-               url_data["url"].append(itag_url[pos+4:])
-       player_config["args"]["url_encoded_fmt_stream_map"] = urllib.urlencode(url_data, True)
-
 def get_player_config(doc):
        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(";")
+                       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+1:p2])
-                       if "ytplayer.config =" in line:
-                               p1 = line.find("ytplayer.config =")
-                               p2 = line.rfind(";")
-                               if p1 >= 0 and p2 > 0:
-                                       return json.loads(line[p1+18: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
+                                       return json.loads(line[p1:p2])
 
 def extract_function(output, script, func_name):
-       p1 = script.find("function " + func_name)
+       p1 = script.find("function " + func_name + "(")
        p2 = script.find("}", p1)
        code = script[p1:p2+1]
        output.append(code)
-       deps = re.findall(R"[^\.]\b([a-zA-Z]+)\(", code)
+       deps = re.findall(R"[^\.][= ]([\$0-9a-zA-Z]+)\(", code)
        deps = set(deps)
        deps.remove(func_name)
        for dep in deps:
@@ -193,6 +178,8 @@ def get_best_video(player_config):
                mimetype = url_data["type"][0].split(";")[0]
                quality = url_data["quality"][0]
 
+               if url_data.has_key("stereo3d"):
+                       continue
                if quality not in QUALITIES:
                        continue
                if mimetype not in MIMETYPES:
@@ -207,9 +194,13 @@ def get_best_video(player_config):
                video_url = url_data["url"][0]
                if "sig" in url_data:
                        signature = url_data["sig"][0]
-               else:
+               elif "s" in url_data:
                        signature = decode_signature(js_url, url_data["s"][0])
-               video_url = append_to_qs(video_url, {"signature": signature})
+               else:
+                       signature = None
+
+               if signature:
+                       video_url = append_to_qs(video_url, {"signature": signature})
 
                best_url = video_url
                best_quality = quality