From e3b3bdf86e9e9921ce6c76e8dd587dbe0b41f33e Mon Sep 17 00:00:00 2001 From: Christopher Rosell Date: Sat, 21 Apr 2012 20:35:43 +0200 Subject: Make get_streams always return a dict. --- src/livestreamer/cli.py | 2 +- src/livestreamer/plugins/__init__.py | 8 ++--- src/livestreamer/plugins/justintv.py | 17 ++++++----- src/livestreamer/plugins/ownedtv.py | 57 +++++++++++++++++------------------ src/livestreamer/plugins/ustreamtv.py | 42 ++++++++++++-------------- 5 files changed, 62 insertions(+), 64 deletions(-) diff --git a/src/livestreamer/cli.py b/src/livestreamer/cli.py index 6187f39..179b80d 100644 --- a/src/livestreamer/cli.py +++ b/src/livestreamer/cli.py @@ -26,7 +26,7 @@ def handle_url(args): streams = channel.get_streams() - if not streams: + if len(streams) == 0: exit(("No streams found on url: {0}").format(args.url)) keys = list(streams.keys()) diff --git a/src/livestreamer/plugins/__init__.py b/src/livestreamer/plugins/__init__.py index a4e39cc..fe62179 100644 --- a/src/livestreamer/plugins/__init__.py +++ b/src/livestreamer/plugins/__init__.py @@ -23,15 +23,15 @@ class Plugin(object): self.args = args def get_streams(self): - ranking = ['iphonelow', 'iphonehigh', '240p', '360p', '480p', '720p', - 'hd', '1080p', 'live'] + ranking = ["iphonelow", "iphonehigh", "240p", "360p", "480p", "720p", + "hd", "1080p", "live"] streams = self._get_streams() for rank in reversed(ranking): if rank in streams: - streams['best'] = streams[rank] + streams["best"] = streams[rank] break return streams - + def _get_streams(self): raise NotImplementedError diff --git a/src/livestreamer/plugins/justintv.py b/src/livestreamer/plugins/justintv.py index 16c522c..f8e4e0b 100644 --- a/src/livestreamer/plugins/justintv.py +++ b/src/livestreamer/plugins/justintv.py @@ -69,20 +69,15 @@ class JustinTV(Plugin): res.append(node.data) return "".join(res) - def _get_streams(self): + def _get_streaminfo(self, channelname): def clean_tag(tag): if tag[0] == "_": return tag[1:] else: return tag - randomp = int(random.random() * 999999) - channelname = self._get_channel_name(self.url) - - if not channelname: - return False - metadata = self._get_metadata(channelname) + randomp = int(random.random() * 999999) if "chansub_guid" in metadata: fd = urllib.urlopen(self.StreamInfoURLSub.format(channelname, randomp, metadata["chansub_guid"])) @@ -123,4 +118,12 @@ class JustinTV(Plugin): return streams + def _get_streams(self): + channelname = self._get_channel_name(self.url) + + if not channelname: + return {} + + return self._get_streaminfo(channelname) + register_plugin("justintv", JustinTV) diff --git a/src/livestreamer/plugins/ownedtv.py b/src/livestreamer/plugins/ownedtv.py index e2ccf94..6461c15 100644 --- a/src/livestreamer/plugins/ownedtv.py +++ b/src/livestreamer/plugins/ownedtv.py @@ -48,38 +48,35 @@ class OwnedTV(Plugin): def _get_streams(self): channelid = self._get_channel_id(self.url) - - if not channelid: - return False - - fd = urllib.urlopen(self.ConfigURL.format(channelid)) - data = fd.read() - fd.close() - streams = {} - dom = xml.dom.minidom.parseString(data) - channels = dom.getElementsByTagName("channels")[0] - clip = channels.getElementsByTagName("clip")[0] - streams = {} - for item in clip.getElementsByTagName("item"): - base = item.getAttribute("base") - if not base: - continue - - if base[0] == "$": - ref = re.match("\${(.+)}", base).group(1) - base = self.CDN[ref] - - for streamel in item.getElementsByTagName("stream"): - name = streamel.getAttribute("label").lower().replace(" ", "_") - playpath = streamel.getAttribute("name") - - if not name in streams: - streams[name] = RTMPStream({ - "rtmp": ("{0}/{1}").format(base, playpath), - "live": 1 - }) + if channelid: + fd = urllib.urlopen(self.ConfigURL.format(channelid)) + data = fd.read() + fd.close() + + dom = xml.dom.minidom.parseString(data) + channels = dom.getElementsByTagName("channels")[0] + clip = channels.getElementsByTagName("clip")[0] + + for item in clip.getElementsByTagName("item"): + base = item.getAttribute("base") + if not base: + continue + + if base[0] == "$": + ref = re.match("\${(.+)}", base).group(1) + base = self.CDN[ref] + + for streamel in item.getElementsByTagName("stream"): + name = streamel.getAttribute("label").lower().replace(" ", "_") + playpath = streamel.getAttribute("name") + + if not name in streams: + streams[name] = RTMPStream({ + "rtmp": ("{0}/{1}").format(base, playpath), + "live": 1 + }) return streams diff --git a/src/livestreamer/plugins/ustreamtv.py b/src/livestreamer/plugins/ustreamtv.py index 54ea021..63a3134 100644 --- a/src/livestreamer/plugins/ustreamtv.py +++ b/src/livestreamer/plugins/ustreamtv.py @@ -30,29 +30,27 @@ class UStreamTV(Plugin): if match: return str(match.group(1), "ascii") + streams = {} channelid = self._get_channel_id(self.url) - if not channelid: - return False - - fd = urllib.urlopen(self.AMFURL.format(channelid)) - data = fd.read() - fd.close() - - playpath = get_amf_value(data, "streamName") - cdnurl = get_amf_value(data, "cdnUrl") - fmsurl = get_amf_value(data, "fmsUrl") - - if not playpath: - return False - - stream = RTMPStream({ - "rtmp": ("{0}/{1}").format(cdnurl or fmsurl, playpath), - "pageUrl": self.url, - "swfUrl": self.SWFURL, - "live": 1 - }) - - return {"live": stream} + if channelid: + fd = urllib.urlopen(self.AMFURL.format(channelid)) + data = fd.read() + fd.close() + + playpath = get_amf_value(data, "streamName") + cdnurl = get_amf_value(data, "cdnUrl") + fmsurl = get_amf_value(data, "fmsUrl") + + if playpath: + stream = RTMPStream({ + "rtmp": ("{0}/{1}").format(cdnurl or fmsurl, playpath), + "pageUrl": self.url, + "swfUrl": self.SWFURL, + "live": 1 + }) + streams["live"] = stream + + return streams register_plugin("ustreamtv", UStreamTV) -- cgit v1.2.3