diff --git a/youtube.py b/youtube.py index 1a9046f..fbe82e5 100644 --- a/youtube.py +++ b/youtube.py @@ -195,33 +195,48 @@ class YouTube (totem.Plugin): if self.debug: print "Done activating row" - def get_fmt_string (self): - if self.gconf_client.get_int ("/apps/totem/connection_speed") >= 10: - return "&fmt=18" - else: - return "" + def get_fmt_string (self,fmt_map): + fmts = fmt_map.split(",") + for fmt in fmts: + if fmt.split("/")[0] == "22": + return "&fmt=22" + return "" - def resolve_t_param (self, youtube_id): + def resolve_swfArgs (self, youtube_id): """We have to get the t parameter from the actual video page, since Google changed how their URLs work""" - stream = urllib.urlopen ("http://youtube.com/watch?v=" + urllib.quote (youtube_id)) - regexp1 = re.compile ("swfArgs.*\"t\": \"([^\"]+)\"") - regexp2 = re.compile ("") + stream = urllib.urlopen ("http://youtube.com/watch?fmt=22&v=" + urllib.quote (youtube_id)) + regexp_t = re.compile ("swfArgs.*\"t\": \"([^\"]+)\"") + regexp_fmt_map = re.compile ("swfArgs.*\"fmt_map\": \"([^\"]+)\"") + regexp_end = re.compile ("") + + param = {"t":"", "fmt_map":""} + match_count = 0 + while (match_count != 2): + contents = stream.readline() + if(contents == ""): + break + try: + contents.index("swfArgs") + except ValueError: + continue - contents = stream.read () - if contents != "": """Check for the t parameter, which is now in a JavaScript array on the video page""" - matches = regexp1.search (contents) + matches = regexp_t.search (contents) if (matches != None): - stream.close () - return matches.group (1) + param["t"] = matches.group (1) + match_count += 1 - """Check to see if we've come to the end of the tag; in which case, we should give up""" - if (regexp2.search (contents) != None): - stream.close () - return "" + matches = regexp_fmt_map.search (contents) + if (matches != None): + param["fmt_map"] = matches.group (1) + match_count += 1 + + if (regexp_end.search (contents) != None): + break stream.close () - return "" + return param + def on_starting_video (self, treeview, path, user_data): """Display an error if the required GStreamer plugins aren't installed""" @@ -241,7 +256,7 @@ class YouTube (totem.Plugin): youtube_id = model.get_value (iter, 3) """Open the video in the browser""" - os.spawnlp (os.P_NOWAIT, "xdg-open", "xdg-open", "http://www.youtube.com/watch?v=" + urllib.quote (youtube_id) + self.get_fmt_string ()) + os.spawnlp (os.P_NOWAIT, "xdg-open", "xdg-open", "http://www.youtube.com/watch?v=" + urllib.quote (youtube_id) + self.get_fmt_string ("")) def on_button_press_event (self, widget, event): self.button_down = True @@ -331,10 +346,9 @@ class YouTube (totem.Plugin): os.unlink (filename) """Get the video stream MRL""" - t_param = self.resolve_t_param (youtube_id) - - if t_param != "": - mrl = "http://www.youtube.com/get_video?video_id=" + urllib.quote (youtube_id) + "&t=" + urllib.quote (t_param) + self.get_fmt_string () + args = self.resolve_swfArgs (youtube_id) + if args["t"] != "": + mrl = "http://www.youtube.com/get_video?video_id=" + urllib.quote (youtube_id) + "&t=" + urllib.quote (args["t"]) + self.get_fmt_string (args["fmt_map"]) gobject.idle_add (self._append_to_liststore, treeview_name, pixbuf, entry.title.text, mrl, youtube_id, search_token)