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/utils.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src/livestreamer/utils.py') diff --git a/src/livestreamer/utils.py b/src/livestreamer/utils.py index 60a4d6a..5ab8a63 100644 --- a/src/livestreamer/utils.py +++ b/src/livestreamer/utils.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from livestreamer.compat import urllib, bytes +from livestreamer.plugins import PluginError import hmac, hashlib, zlib, argparse SWF_KEY = b"Genuine Adobe Flash Player 001" @@ -36,10 +37,26 @@ class ArgumentParser(argparse.ArgumentParser): val = line[split+1:].strip() yield "--%s=%s" % (key, val) +def urlget(url, data=None, timeout=None, opener=None): + try: + if opener is not None: + fd = opener.open(url) + else: + fd = urllib.urlopen(url, data, timeout) + + data = fd.read() + fd.close() + + except IOError as err: + if type(err) is urllib.URLError: + raise PluginError(err.reason) + else: + raise PluginError(err) + + return data + def swfverify(url): - fd = urllib.urlopen(url) - swf = fd.read() - fd.close() + swf = urlget(url) if swf[:3] == b"CWS": swf = b"F" + swf[1:8] + zlib.decompress(swf[8:]) @@ -47,3 +64,4 @@ def swfverify(url): h = hmac.new(SWF_KEY, swf, hashlib.sha256) return h.hexdigest(), len(swf) + -- cgit v1.2.3