From f175241692dfc461d773589653fd55c47508a293 Mon Sep 17 00:00:00 2001 From: Christopher Rosell Date: Wed, 21 Mar 2012 13:10:02 +0100 Subject: Change the way plugins are used. Also use http as default protocol when URL is missing protocol. --- src/livestreamer/__init__.py | 9 ++++++++- src/livestreamer/cli.py | 2 +- src/livestreamer/compat.py | 4 ++++ src/livestreamer/plugins/__init__.py | 22 +++++++++++++--------- src/livestreamer/plugins/justintv.py | 6 ++++-- src/livestreamer/plugins/ownedtv.py | 7 ++++--- src/livestreamer/plugins/ustreamtv.py | 9 +++++---- 7 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/livestreamer/__init__.py b/src/livestreamer/__init__.py index e429e7e..255b5b4 100644 --- a/src/livestreamer/__init__.py +++ b/src/livestreamer/__init__.py @@ -1,13 +1,20 @@ from livestreamer import plugins +from livestreamer.compat import urlparse import os.path RCFILE = os.path.expanduser("~/.livestreamerrc") def resolve_url(url): + parsed = urlparse(url) + + if len(parsed.scheme) == 0: + url = "http://" + url + for name, plugin in plugins.get_plugins().items(): if plugin.can_handle_url(url): - return (name, plugin) + obj = plugin(url) + return (name, obj) return None def get_plugins(): diff --git a/src/livestreamer/cli.py b/src/livestreamer/cli.py index 3239e81..1965e7e 100644 --- a/src/livestreamer/cli.py +++ b/src/livestreamer/cli.py @@ -24,7 +24,7 @@ def handle_url(args): if not plugin: exit(("No plugin can handle url: {0}").format(args.url)) - streams = plugin.get_streams(args.url) + streams = plugin.get_streams() if not streams: exit(("No streams found on url: {0}").format(args.url)) diff --git a/src/livestreamer/compat.py b/src/livestreamer/compat.py index d3d6bdc..c0d798e 100644 --- a/src/livestreamer/compat.py +++ b/src/livestreamer/compat.py @@ -24,3 +24,7 @@ try: except ImportError: import urllib2 as urllib +try: + from urllib.parse import urlparse +except ImportError: + from urlparse import urlparse diff --git a/src/livestreamer/plugins/__init__.py b/src/livestreamer/plugins/__init__.py index 1f4c268..1eaf829 100644 --- a/src/livestreamer/plugins/__init__.py +++ b/src/livestreamer/plugins/__init__.py @@ -6,21 +6,26 @@ import imp plugins_loaded = {} class Plugin(object): - def can_handle_url(self, url): - raise NotImplementedError - - def get_streams(self, channel): - raise NotImplementedError + def __init__(self, url): + self.url = url - def stream_cmdline(self, stream, filename): - raise NotImplementedError + @classmethod + def can_handle_url(self, url): + raise NotImplementedError + @classmethod def handle_parser(self, parser): pass + @classmethod def handle_args(self, args): self.args = args + def get_streams(self): + raise NotImplementedError + + def stream_cmdline(self, stream, filename): + raise NotImplementedError def load_plugins(plugins): for loader, name, ispkg in pkgutil.iter_modules(plugins.__path__): @@ -33,5 +38,4 @@ def get_plugins(): return plugins_loaded def register_plugin(name, klass): - obj = klass() - plugins_loaded[name] = obj + plugins_loaded[name] = klass diff --git a/src/livestreamer/plugins/justintv.py b/src/livestreamer/plugins/justintv.py index 9db0932..233a17b 100644 --- a/src/livestreamer/plugins/justintv.py +++ b/src/livestreamer/plugins/justintv.py @@ -12,9 +12,11 @@ class JustinTV(Plugin): MetadataURL = "http://www.justin.tv/meta/{0}.xml?on_site=true" SWFURL = "http://www.justin.tv/widgets/live_embed_player.swf" + @classmethod def can_handle_url(self, url): return ("justin.tv" in url) or ("twitch.tv" in url) + @classmethod def handle_parser(self, parser): parser.add_argument("--jtv-cookie", metavar="cookie", help="JustinTV cookie to allow access to subscription channels") @@ -60,7 +62,7 @@ class JustinTV(Plugin): res.append(node.data) return "".join(res) - def get_streams(self, url): + def get_streams(self): def clean_tag(tag): if tag[0] == "_": return tag[1:] @@ -68,7 +70,7 @@ class JustinTV(Plugin): return tag randomp = int(random.random() * 999999) - channelname = self._get_channel_name(url) + channelname = self._get_channel_name(self.url) if not channelname: return False diff --git a/src/livestreamer/plugins/ownedtv.py b/src/livestreamer/plugins/ownedtv.py index 87df3ad..779dd2c 100644 --- a/src/livestreamer/plugins/ownedtv.py +++ b/src/livestreamer/plugins/ownedtv.py @@ -29,10 +29,11 @@ class OwnedTV(Plugin): "cdn3": "http://hwcdn.net/u4k2r7c4/fls", } + @classmethod def can_handle_url(self, url): return "own3d.tv" in url - def get_channel_id(self, url): + def _get_channel_id(self, url): fd = urlopener.open(url) data = fd.read() fd.close() @@ -45,8 +46,8 @@ class OwnedTV(Plugin): if match: return int(match.group(1)) - def get_streams(self, url): - channelid = self.get_channel_id(url) + def get_streams(self): + channelid = self._get_channel_id(self.url) if not channelid: return False diff --git a/src/livestreamer/plugins/ustreamtv.py b/src/livestreamer/plugins/ustreamtv.py index 90b4b8b..f191589 100644 --- a/src/livestreamer/plugins/ustreamtv.py +++ b/src/livestreamer/plugins/ustreamtv.py @@ -11,10 +11,11 @@ class UStreamTV(Plugin): AMFURL = "http://cgw.ustream.tv/Viewer/getStream/1/{0}.amf" SWFURL = "http://cdn1.ustream.tv/swf/4/viewer.rsl.210.swf" + @classmethod def can_handle_url(self, url): return "ustream.tv" in url - def get_channel_id(self, url): + def _get_channel_id(self, url): fd = urllib.urlopen(url) data = fd.read() fd.close() @@ -23,14 +24,14 @@ class UStreamTV(Plugin): if match: return int(match.group(1)) - def get_streams(self, url): + def get_streams(self): def get_amf_value(data, key): pattern = ("{0}\W\W\W(.+?)\x00").format(key) match = re.search(bytes(pattern, "ascii"), data) if match: return str(match.group(1), "ascii") - channelid = self.get_channel_id(url) + channelid = self._get_channel_id(self.url) if not channelid: return False @@ -50,7 +51,7 @@ class UStreamTV(Plugin): stream["playpath"] = playpath stream["rtmp"] = cdnurl or fmsurl - stream["url"] = url + stream["url"] = self.url return {"live": stream} -- cgit v1.2.3