From 5ff656ef3e5ccc7357c4d4e6136be2e15e61281b Mon Sep 17 00:00:00 2001 From: Christopher Rosell Date: Thu, 13 Oct 2011 20:47:51 +0200 Subject: Add livestreamer.compat for 2.6 and 2.7 support. --- README | 4 +++- src/livestreamer/compat.py | 26 ++++++++++++++++++++++++++ src/livestreamer/plugins/justintv.py | 8 ++++---- src/livestreamer/plugins/ownedtv.py | 10 +++++----- src/livestreamer/plugins/ustreamtv.py | 6 +++--- 5 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 src/livestreamer/compat.py diff --git a/README b/README index 11f7164..684e2d7 100644 --- a/README +++ b/README @@ -1,5 +1,7 @@ INSTALLING - $ sudo python3 setup.py install + $ sudo python setup.py install + +livestreamer is compatible with python version >= 2.6 and 3.0. USING $ livestreamer --help diff --git a/src/livestreamer/compat.py b/src/livestreamer/compat.py new file mode 100644 index 0000000..142f39c --- /dev/null +++ b/src/livestreamer/compat.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +import sys + +orig_str = str + +def str(s, enc=None): + if sys.version_info[0] == 3: + return orig_str(s, enc) + else: + return orig_str(s) + + +orig_bytes = bytes + +def bytes(s, enc=None): + if sys.version_info[0] == 3: + return orig_bytes(s, enc) + else: + return orig_bytes(s) + +try: + import urllib.request as urllib +except ImportError: + import urllib2 as urllib + diff --git a/src/livestreamer/plugins/justintv.py b/src/livestreamer/plugins/justintv.py index 6607ae4..04ed74d 100644 --- a/src/livestreamer/plugins/justintv.py +++ b/src/livestreamer/plugins/justintv.py @@ -2,9 +2,9 @@ from livestreamer.plugins import Plugin, register_plugin from livestreamer.utils import CommandLine +from livestreamer.compat import urllib, str -import urllib.request, urllib.error, urllib.parse -import xml.dom.minidom, re +import xml.dom.minidom, re, sys class JustinTV(object): StreamInfoURL = "http://usher.justin.tv/find/%s.xml?type=any" @@ -14,7 +14,7 @@ class JustinTV(object): return ("justin.tv" in url) or ("twitch.tv" in url) def get_channel_name(self, url): - fd = urllib.request.urlopen(url) + fd = urllib.urlopen(url) data = fd.read() fd.close() @@ -42,7 +42,7 @@ class JustinTV(object): if not channelname: return False - fd = urllib.request.urlopen(self.StreamInfoURL % channelname) + fd = urllib.urlopen(self.StreamInfoURL % channelname) data = fd.read() fd.close() diff --git a/src/livestreamer/plugins/ownedtv.py b/src/livestreamer/plugins/ownedtv.py index e8f004d..b203bb9 100644 --- a/src/livestreamer/plugins/ownedtv.py +++ b/src/livestreamer/plugins/ownedtv.py @@ -2,11 +2,11 @@ from livestreamer.plugins import Plugin, register_plugin from livestreamer.utils import CommandLine +from livestreamer.compat import urllib -import urllib.request, urllib.error, urllib.parse import xml.dom.minidom, re -class RelativeRedirectHandler(urllib.request.HTTPRedirectHandler): +class RelativeRedirectHandler(urllib.HTTPRedirectHandler): def http_error_302(self, req, fp, code, msg, headers): if "location" in headers and headers["location"][0] == "/": absurl = ("{scheme}://{host}{path}").format( @@ -15,10 +15,10 @@ class RelativeRedirectHandler(urllib.request.HTTPRedirectHandler): del headers["location"] headers["location"] = absurl - return urllib.request.HTTPRedirectHandler.http_error_301( + return urllib.HTTPRedirectHandler.http_error_301( self, req, fp, code, msg, headers) -urlopener = urllib.request.build_opener(RelativeRedirectHandler) +urlopener = urllib.build_opener(RelativeRedirectHandler) class OwnedTV(Plugin): @@ -47,7 +47,7 @@ class OwnedTV(Plugin): if not channelid: return False - fd = urllib.request.urlopen(self.ConfigURL.format(channelid)) + fd = urllib.urlopen(self.ConfigURL.format(channelid)) data = fd.read() fd.close() diff --git a/src/livestreamer/plugins/ustreamtv.py b/src/livestreamer/plugins/ustreamtv.py index a33b1d4..90b4b8b 100644 --- a/src/livestreamer/plugins/ustreamtv.py +++ b/src/livestreamer/plugins/ustreamtv.py @@ -2,8 +2,8 @@ from livestreamer.plugins import Plugin, register_plugin from livestreamer.utils import CommandLine +from livestreamer.compat import urllib, str, bytes -import urllib.request, urllib.error, urllib.parse import xml.dom.minidom, re @@ -15,7 +15,7 @@ class UStreamTV(Plugin): return "ustream.tv" in url def get_channel_id(self, url): - fd = urllib.request.urlopen(url) + fd = urllib.urlopen(url) data = fd.read() fd.close() @@ -35,7 +35,7 @@ class UStreamTV(Plugin): if not channelid: return False - fd = urllib.request.urlopen(self.AMFURL.format(channelid)) + fd = urllib.urlopen(self.AMFURL.format(channelid)) data = fd.read() fd.close() -- cgit v1.2.3