openm3u8 7.0.0__cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
openm3u8/httpclient.py ADDED
@@ -0,0 +1,36 @@
1
+ import gzip
2
+ import ssl
3
+ import urllib.request
4
+ from urllib.parse import urljoin
5
+
6
+
7
+ class DefaultHTTPClient:
8
+ def __init__(self, proxies=None):
9
+ self.proxies = proxies
10
+
11
+ def download(self, uri, timeout=None, headers={}, verify_ssl=True):
12
+ proxy_handler = urllib.request.ProxyHandler(self.proxies)
13
+ https_handler = HTTPSHandler(verify_ssl=verify_ssl)
14
+ opener = urllib.request.build_opener(proxy_handler, https_handler)
15
+ opener.addheaders = headers.items()
16
+ resource = opener.open(uri, timeout=timeout)
17
+ base_uri = urljoin(resource.geturl(), ".")
18
+
19
+ if resource.info().get("Content-Encoding") == "gzip":
20
+ content = gzip.decompress(resource.read()).decode(
21
+ resource.headers.get_content_charset(failobj="utf-8")
22
+ )
23
+ else:
24
+ content = resource.read().decode(
25
+ resource.headers.get_content_charset(failobj="utf-8")
26
+ )
27
+ return content, base_uri
28
+
29
+
30
+ class HTTPSHandler:
31
+ def __new__(self, verify_ssl=True):
32
+ context = ssl.create_default_context()
33
+ if not verify_ssl:
34
+ context.check_hostname = False
35
+ context.verify_mode = ssl.CERT_NONE
36
+ return urllib.request.HTTPSHandler(context=context)
openm3u8/mixins.py ADDED
@@ -0,0 +1,52 @@
1
+ from os.path import dirname
2
+ from urllib.parse import urljoin, urlsplit
3
+
4
+
5
+ class BasePathMixin:
6
+ @property
7
+ def absolute_uri(self):
8
+ if self.uri is None:
9
+ return None
10
+
11
+ ret = urljoin(self.base_uri, self.uri)
12
+ if self.base_uri:
13
+ base_uri_parts = urlsplit(self.base_uri)
14
+ if (not base_uri_parts.scheme) and (not base_uri_parts.netloc):
15
+ return ret
16
+
17
+ if not urlsplit(ret).scheme:
18
+ raise ValueError("There can not be `absolute_uri` with no `base_uri` set")
19
+
20
+ return ret
21
+
22
+ @property
23
+ def base_path(self):
24
+ if self.uri is None:
25
+ return None
26
+ return dirname(self.get_path_from_uri())
27
+
28
+ def get_path_from_uri(self):
29
+ """Some URIs have a slash in the query string."""
30
+ return self.uri.split("?")[0]
31
+
32
+ @base_path.setter
33
+ def base_path(self, newbase_path):
34
+ if self.uri is not None:
35
+ if not self.base_path:
36
+ self.uri = f"{newbase_path}/{self.uri}"
37
+ else:
38
+ self.uri = self.uri.replace(self.base_path, newbase_path)
39
+
40
+
41
+ class GroupedBasePathMixin:
42
+ def _set_base_uri(self, new_base_uri):
43
+ for item in self:
44
+ item.base_uri = new_base_uri
45
+
46
+ base_uri = property(None, _set_base_uri)
47
+
48
+ def _set_base_path(self, newbase_path):
49
+ for item in self:
50
+ item.base_path = newbase_path
51
+
52
+ base_path = property(None, _set_base_path)