diff options
author | Christopher Rosell <chrippa@tanuki.se> | 2012-04-21 21:57:20 +0200 |
---|---|---|
committer | Christopher Rosell <chrippa@tanuki.se> | 2012-04-21 21:57:20 +0200 |
commit | 22dd8a40c8bb9c1c7e845a47df5b84dfe2a8527e (patch) | |
tree | 2a60947960ccd89fd19ad45e992545dc8454ba8b /src/livestreamer/utils.py | |
parent | e3b3bdf86e9e9921ce6c76e8dd587dbe0b41f33e (diff) |
Added PluginError exception to make error handling nicer.
Also added a utility function to do HTTP requests.
Diffstat (limited to 'src/livestreamer/utils.py')
-rw-r--r-- | src/livestreamer/utils.py | 24 |
1 files changed, 21 insertions, 3 deletions
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) + |