From e4c07414d077964613da5b7870a49f0882f703b1 Mon Sep 17 00:00:00 2001 From: Christopher Rosell Date: Thu, 24 May 2012 15:07:03 +0200 Subject: Add more plugin exceptions. --- src/livestreamer/__init__.py | 5 ++- src/livestreamer/cli.py | 12 +++---- src/livestreamer/plugins/__init__.py | 9 +++++- src/livestreamer/plugins/justintv.py | 4 +-- src/livestreamer/plugins/ownedtv.py | 61 ++++++++++++++++++----------------- src/livestreamer/plugins/ustreamtv.py | 34 ++++++++++--------- 6 files changed, 70 insertions(+), 55 deletions(-) diff --git a/src/livestreamer/__init__.py b/src/livestreamer/__init__.py index 9eb2264..64886a8 100644 --- a/src/livestreamer/__init__.py +++ b/src/livestreamer/__init__.py @@ -11,12 +11,15 @@ def resolve_url(url): if plugin.can_handle_url(url): obj = plugin(url) return obj - return None + + raise plugins.NoPluginError() def get_plugins(): return plugins.get_plugins() PluginError = plugins.PluginError +NoStreamsError = plugins.NoStreamsError +NoPluginError = plugins.NoPluginError plugins.load_plugins(plugins) diff --git a/src/livestreamer/cli.py b/src/livestreamer/cli.py index 6d7aac5..d8d1bc0 100644 --- a/src/livestreamer/cli.py +++ b/src/livestreamer/cli.py @@ -19,18 +19,18 @@ def exit(msg): sys.exit() def handle_url(args): - channel = livestreamer.resolve_url(args.url) - - if not channel: - exit(("No plugin can handle url: {0}").format(args.url)) + try: + channel = livestreamer.resolve_url(args.url) + except livestreamer.NoPluginError: + exit(("No plugin can handle URL: {0}").format(args.url)) try: streams = channel.get_streams() except livestreamer.PluginError as err: - exit(("Error from plugin while retrieving streams: {0}").format(err)) + exit(str(err)) if len(streams) == 0: - exit(("No streams found on url: {0}").format(args.url)) + exit(("No streams found on this URL: {0}").format(args.url)) keys = list(streams.keys()) keys.sort() diff --git a/src/livestreamer/plugins/__init__.py b/src/livestreamer/plugins/__init__.py index d24b4de..ba5810b 100644 --- a/src/livestreamer/plugins/__init__.py +++ b/src/livestreamer/plugins/__init__.py @@ -30,6 +30,7 @@ class Plugin(object): if rank in streams: streams["best"] = streams[rank] break + return streams def _get_streams(self): @@ -38,13 +39,19 @@ class Plugin(object): class PluginError(Exception): pass +class NoStreamsError(PluginError): + def __init__(self, url): + PluginError.__init__(self, ("No streams found on this URL: {0}").format(url)) + +class NoPluginError(PluginError): + pass + def load_plugins(plugins): for loader, name, ispkg in pkgutil.iter_modules(plugins.__path__): file, pathname, desc = imp.find_module(name, plugins.__path__) imp.load_module(name, file, pathname, desc) return plugins_loaded - def get_plugins(): return plugins_loaded diff --git a/src/livestreamer/plugins/justintv.py b/src/livestreamer/plugins/justintv.py index 620db63..43e343c 100644 --- a/src/livestreamer/plugins/justintv.py +++ b/src/livestreamer/plugins/justintv.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from livestreamer.plugins import Plugin, PluginError, register_plugin +from livestreamer.plugins import Plugin, PluginError, NoStreamsError, register_plugin from livestreamer.stream import RTMPStream from livestreamer.utils import swfverify, urlget from livestreamer.compat import urllib, str @@ -125,7 +125,7 @@ class JustinTV(Plugin): channelname = self._get_channel_name(self.url) if not channelname: - return {} + raise NoStreamsError(self.url) return self._get_streaminfo(channelname) diff --git a/src/livestreamer/plugins/ownedtv.py b/src/livestreamer/plugins/ownedtv.py index 5279777..36887f7 100644 --- a/src/livestreamer/plugins/ownedtv.py +++ b/src/livestreamer/plugins/ownedtv.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from livestreamer.compat import urllib -from livestreamer.plugins import Plugin, PluginError, register_plugin +from livestreamer.plugins import Plugin, PluginError, NoStreamsError, register_plugin from livestreamer.stream import RTMPStream from livestreamer.utils import urlget @@ -36,10 +36,11 @@ class OwnedTV(Plugin): def _get_channel_id(self, url): data = urlget(url, opener=urlopener) + match = re.search(b'flashvars.config = "livecfg/(\d+)', data) if match: return int(match.group(1)) - + match = re.search(b"document.location.hash='/live/(\d+)'", data) if match: return int(match.group(1)) @@ -50,37 +51,39 @@ class OwnedTV(Plugin): def _get_streams(self): channelid = self._get_channel_id(self.url) - streams = {} - - if channelid: - data = urlget(self.ConfigURL.format(channelid)) - try: - dom = xml.dom.minidom.parseString(data) - except Exception as err: - raise PluginError(("Unable to parse config XML: {0})").format(err)) + if not channelid: + raise NoStreamsError(self.url) - channels = dom.getElementsByTagName("channels")[0] - clip = channels.getElementsByTagName("clip")[0] + data = urlget(self.ConfigURL.format(channelid)) - for item in clip.getElementsByTagName("item"): - base = item.getAttribute("base") - if not base: - continue + try: + dom = xml.dom.minidom.parseString(data) + except Exception as err: + raise PluginError(("Unable to parse config XML: {0})").format(err)) - 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 - }) + streams = {} + 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 063923e..2e2e122 100644 --- a/src/livestreamer/plugins/ustreamtv.py +++ b/src/livestreamer/plugins/ustreamtv.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from livestreamer.compat import str, bytes -from livestreamer.plugins import Plugin, PluginError, register_plugin +from livestreamer.plugins import Plugin, PluginError, NoStreamsError, register_plugin from livestreamer.stream import RTMPStream from livestreamer.utils import urlget @@ -32,21 +32,23 @@ class UStreamTV(Plugin): streams = {} channelid = self._get_channel_id(self.url) - if channelid: - data = urlget(self.AMFURL.format(channelid)) - - 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 + if not channelid: + raise NoStreamsError(self.url) + + data = urlget(self.AMFURL.format(channelid)) + + 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 -- cgit v1.2.3