/usr/lib/python2.7/dist-packages/livestreamer/utils.py is in python-livestreamer 1.12.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | import json
import re
import zlib
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
from .compat import urljoin, urlparse, parse_qsl, is_py2
from .exceptions import PluginError
def swfdecompress(data):
if data[:3] == b"CWS":
data = b"F" + data[1:8] + zlib.decompress(data[8:])
return data
def verifyjson(json, key):
if not isinstance(json, dict):
raise PluginError("JSON result is not a dict")
if not key in json:
raise PluginError("Missing '{0}' key in JSON".format(key))
return json[key]
def absolute_url(baseurl, url):
if not url.startswith("http"):
return urljoin(baseurl, url)
else:
return url
def prepend_www(url):
"""Changes google.com to www.google.com"""
parsed = urlparse(url)
if parsed.netloc.split(".")[0] != "www":
return parsed.scheme + "://www." + parsed.netloc + parsed.path
else:
return url
def parse_json(data, name="JSON", exception=PluginError, schema=None):
"""Wrapper around json.loads.
Wraps errors in custom exception with a snippet of the data in the message.
"""
try:
json_data = json.loads(data)
except ValueError as err:
snippet = repr(data)
if len(snippet) > 35:
snippet = snippet[:35] + " ..."
else:
snippet = data
raise exception("Unable to parse {0}: {1} ({2})".format(name, err, snippet))
if schema:
json_data = schema.validate(json_data, name=name, exception=exception)
return json_data
def parse_xml(data, name="XML", ignore_ns=False, exception=PluginError, schema=None):
"""Wrapper around ElementTree.fromstring with some extras.
Provides these extra features:
- Handles incorrectly encoded XML
- Allows stripping namespace information
- Wraps errors in custom exception with a snippet of the data in the message
"""
if is_py2 and isinstance(data, unicode):
data = data.encode("utf8")
if ignore_ns:
data = re.sub(" xmlns=\"(.+?)\"", "", data)
try:
tree = ET.fromstring(data)
except Exception as err:
snippet = repr(data)
if len(snippet) > 35:
snippet = snippet[:35] + " ..."
raise exception("Unable to parse {0}: {1} ({2})".format(name, err, snippet))
if schema:
tree = schema.validate(tree, name=name, exception=exception)
return tree
def parse_qsd(data, name="query string", exception=PluginError, schema=None, **params):
"""Parses a query string into a dict.
Unlike parse_qs and parse_qsl, duplicate keys are not preserved in
favor of a simpler return value.
"""
value = dict(parse_qsl(data, **params))
if schema:
value = schema.validate(value, name=name, exception=exception)
return value
def rtmpparse(url):
parse = urlparse(url)
netloc = "{hostname}:{port}".format(hostname=parse.hostname,
port=parse.port or 1935)
split = list(filter(None, parse.path.split("/")))
if len(split) > 2:
app = "/".join(split[:2])
playpath = "/".join(split[2:])
elif len(split) == 2:
app, playpath = split
else:
app = split[0]
if len(parse.query) > 0:
playpath += "?{parse.query}".format(parse=parse)
tcurl = "{scheme}://{netloc}/{app}".format(scheme=parse.scheme,
netloc=netloc,
app=app)
return (tcurl, playpath)
#####################################
# Deprecated functions, do not use. #
#####################################
import requests
def urlget(url, *args, **kwargs):
"""This function is deprecated."""
data = kwargs.pop("data", None)
exception = kwargs.pop("exception", PluginError)
method = kwargs.pop("method", "GET")
session = kwargs.pop("session", None)
timeout = kwargs.pop("timeout", 20)
if data is not None:
method = "POST"
try:
if session:
res = session.request(method, url, timeout=timeout, data=data,
*args, **kwargs)
else:
res = requests.request(method, url, timeout=timeout, data=data,
*args, **kwargs)
res.raise_for_status()
except (requests.exceptions.RequestException, IOError) as rerr:
err = exception("Unable to open URL: {url} ({err})".format(url=url,
err=rerr))
err.err = rerr
raise err
return res
urlopen = urlget
def urlresolve(url):
"""This function is deprecated."""
res = urlget(url, stream=True, allow_redirects=False)
if res.status_code == 302 and "location" in res.headers:
return res.headers["location"]
else:
return url
def res_xml(res, *args, **kw):
"""This function is deprecated."""
return parse_xml(res.text, *args, **kw)
def res_json(res, jsontype="JSON", exception=PluginError):
"""This function is deprecated."""
try:
jsondata = res.json()
except ValueError as err:
if len(res.text) > 35:
snippet = res.text[:35] + "..."
else:
snippet = res.text
raise exception("Unable to parse {0}: {1} ({2})".format(jsontype, err,
snippet))
return jsondata
import hmac
import hashlib
SWF_KEY = b"Genuine Adobe Flash Player 001"
def swfverify(url):
"""This function is deprecated."""
res = urlopen(url)
swf = swfdecompress(res.content)
h = hmac.new(SWF_KEY, swf, hashlib.sha256)
return h.hexdigest(), len(swf)
__all__ = ["urlopen", "urlget", "urlresolve", "swfdecompress", "swfverify",
"verifyjson", "absolute_url", "parse_qsd", "parse_json", "res_json",
"parse_xml", "res_xml", "rtmpparse", "prepend_www"]
|