From 22dd8a40c8bb9c1c7e845a47df5b84dfe2a8527e Mon Sep 17 00:00:00 2001 From: Christopher Rosell Date: Sat, 21 Apr 2012 21:57:20 +0200 Subject: Added PluginError exception to make error handling nicer. Also added a utility function to do HTTP requests. --- src/livestreamer/plugins/__init__.py | 3 +++ src/livestreamer/plugins/justintv.py | 33 ++++++++++++++++++--------------- src/livestreamer/plugins/ownedtv.py | 19 ++++++++++--------- src/livestreamer/plugins/ustreamtv.py | 13 +++++-------- 4 files changed, 36 insertions(+), 32 deletions(-) (limited to 'src/livestreamer/plugins') diff --git a/src/livestreamer/plugins/__init__.py b/src/livestreamer/plugins/__init__.py index fe62179..d24b4de 100644 --- a/src/livestreamer/plugins/__init__.py +++ b/src/livestreamer/plugins/__init__.py @@ -35,6 +35,9 @@ class Plugin(object): def _get_streams(self): raise NotImplementedError +class PluginError(Exception): + pass + def load_plugins(plugins): for loader, name, ispkg in pkgutil.iter_modules(plugins.__path__): file, pathname, desc = imp.find_module(name, plugins.__path__) diff --git a/src/livestreamer/plugins/justintv.py b/src/livestreamer/plugins/justintv.py index f8e4e0b..620db63 100644 --- a/src/livestreamer/plugins/justintv.py +++ b/src/livestreamer/plugins/justintv.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 -from livestreamer.plugins import Plugin, register_plugin +from livestreamer.plugins import Plugin, PluginError, register_plugin from livestreamer.stream import RTMPStream -from livestreamer.utils import swfverify +from livestreamer.utils import swfverify, urlget from livestreamer.compat import urllib, str import xml.dom.minidom, re, sys, random @@ -28,11 +28,9 @@ class JustinTV(Plugin): self.cookie = args.jtv_cookie def _get_channel_name(self, url): - fd = urllib.urlopen(url) - data = fd.read() - fd.close() - + data = urlget(url) match = re.search(b"live_facebook_embed_player\.swf\?channel=(\w+)", data) + if match: return str(match.group(1), "ascii") @@ -43,12 +41,13 @@ class JustinTV(Plugin): else: req = urllib.Request(self.MetadataURL.format(channel)) + data = urlget(req) - fd = urllib.urlopen(req) - data = fd.read() - fd.close() + try: + dom = xml.dom.minidom.parseString(data) + except Exception as err: + raise PluginError(("Unable to parse config XML: {0})").format(err)) - dom = xml.dom.minidom.parseString(data) meta = dom.getElementsByTagName("meta")[0] metadata = {} @@ -80,19 +79,23 @@ class JustinTV(Plugin): randomp = int(random.random() * 999999) if "chansub_guid" in metadata: - fd = urllib.urlopen(self.StreamInfoURLSub.format(channelname, randomp, metadata["chansub_guid"])) + url = self.StreamInfoURLSub.format(channelname, randomp, metadata["chansub_guid"]) else: - fd = urllib.urlopen(self.StreamInfoURL.format(channelname, randomp)) + url = self.StreamInfoURL.format(channelname, randomp) - data = fd.read() - fd.close() + data = urlget(url) # fix invalid xml data = re.sub(b"<(\d+)", b"<_\g<1>", data) data = re.sub(b"", data) streams = {} - dom = xml.dom.minidom.parseString(data) + + try: + dom = xml.dom.minidom.parseString(data) + except Exception as err: + raise PluginError(("Unable to parse config XML: {0})").format(err)) + nodes = dom.getElementsByTagName("nodes")[0] swfhash, swfsize = swfverify(self.SWFURL) diff --git a/src/livestreamer/plugins/ownedtv.py b/src/livestreamer/plugins/ownedtv.py index 6461c15..c15e8c4 100644 --- a/src/livestreamer/plugins/ownedtv.py +++ b/src/livestreamer/plugins/ownedtv.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 -from livestreamer.plugins import Plugin, register_plugin -from livestreamer.stream import RTMPStream from livestreamer.compat import urllib +from livestreamer.plugins import Plugin, PluginError, register_plugin +from livestreamer.stream import RTMPStream +from livestreamer.utils import urlget import xml.dom.minidom, re @@ -34,9 +35,7 @@ class OwnedTV(Plugin): return "own3d.tv" in url def _get_channel_id(self, url): - fd = urlopener.open(url) - data = fd.read() - fd.close() + data = urlget(url, opener=urlopener) match = re.search(b"document.location.hash='/live/(\d+)'", data) if match: @@ -51,11 +50,13 @@ class OwnedTV(Plugin): streams = {} if channelid: - fd = urllib.urlopen(self.ConfigURL.format(channelid)) - data = fd.read() - fd.close() + 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)) - dom = xml.dom.minidom.parseString(data) channels = dom.getElementsByTagName("channels")[0] clip = channels.getElementsByTagName("clip")[0] diff --git a/src/livestreamer/plugins/ustreamtv.py b/src/livestreamer/plugins/ustreamtv.py index 63a3134..063923e 100644 --- a/src/livestreamer/plugins/ustreamtv.py +++ b/src/livestreamer/plugins/ustreamtv.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 -from livestreamer.plugins import Plugin, register_plugin +from livestreamer.compat import str, bytes +from livestreamer.plugins import Plugin, PluginError, register_plugin from livestreamer.stream import RTMPStream -from livestreamer.compat import urllib, str, bytes +from livestreamer.utils import urlget import xml.dom.minidom, re @@ -15,9 +16,7 @@ class UStreamTV(Plugin): return "ustream.tv" in url def _get_channel_id(self, url): - fd = urllib.urlopen(url) - data = fd.read() - fd.close() + data = urlget(url) match = re.search(b"channelId=(\d+)", data) if match: @@ -34,9 +33,7 @@ class UStreamTV(Plugin): channelid = self._get_channel_id(self.url) if channelid: - fd = urllib.urlopen(self.AMFURL.format(channelid)) - data = fd.read() - fd.close() + data = urlget(self.AMFURL.format(channelid)) playpath = get_amf_value(data, "streamName") cdnurl = get_amf_value(data, "cdnUrl") -- cgit v1.2.3