python-urlopen 0.1.2__tar.gz → 0.1.3__tar.gz
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.
- {python_urlopen-0.1.2 → python_urlopen-0.1.3}/PKG-INFO +1 -3
- {python_urlopen-0.1.2 → python_urlopen-0.1.3}/pyproject.toml +1 -3
- {python_urlopen-0.1.2 → python_urlopen-0.1.3}/urlopen/__init__.py +5 -46
- {python_urlopen-0.1.2 → python_urlopen-0.1.3}/LICENSE +0 -0
- {python_urlopen-0.1.2 → python_urlopen-0.1.3}/readme.md +0 -0
- {python_urlopen-0.1.2 → python_urlopen-0.1.3}/urlopen/__main__.py +0 -0
- {python_urlopen-0.1.2 → python_urlopen-0.1.3}/urlopen/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: python-urlopen
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: Python urlopen wrapper.
|
|
5
5
|
Home-page: https://github.com/ChenyangGao/python-modules/tree/main/python-urlopen
|
|
6
6
|
License: MIT
|
|
@@ -20,7 +20,6 @@ Classifier: Programming Language :: Python :: 3 :: Only
|
|
|
20
20
|
Classifier: Topic :: Software Development
|
|
21
21
|
Classifier: Topic :: Software Development :: Libraries
|
|
22
22
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
-
Requires-Dist: brotli
|
|
24
23
|
Requires-Dist: http_response (>=0.0.6)
|
|
25
24
|
Requires-Dist: python-argtools (>=0.0.2)
|
|
26
25
|
Requires-Dist: python-cookietools (>=0.0.8)
|
|
@@ -29,7 +28,6 @@ Requires-Dist: python-filewrap (>=0.2.8)
|
|
|
29
28
|
Requires-Dist: python-http_request (>=0.1.0)
|
|
30
29
|
Requires-Dist: python-undefined (>=0.0.3)
|
|
31
30
|
Requires-Dist: yarl
|
|
32
|
-
Requires-Dist: zstandard
|
|
33
31
|
Project-URL: Repository, https://github.com/ChenyangGao/python-modules/tree/main/python-urlopen
|
|
34
32
|
Description-Content-Type: text/markdown
|
|
35
33
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "python-urlopen"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.3"
|
|
4
4
|
description = "Python urlopen wrapper."
|
|
5
5
|
authors = ["ChenyangGao <wosiwujm@gmail.com>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -27,7 +27,6 @@ include = [
|
|
|
27
27
|
|
|
28
28
|
[tool.poetry.dependencies]
|
|
29
29
|
python = "^3.12"
|
|
30
|
-
brotli = "*"
|
|
31
30
|
http_response = ">=0.0.6"
|
|
32
31
|
python-argtools = ">=0.0.2"
|
|
33
32
|
python-cookietools = ">=0.0.8"
|
|
@@ -36,7 +35,6 @@ python-filewrap = ">=0.2.8"
|
|
|
36
35
|
python-http_request = ">=0.1.0"
|
|
37
36
|
python-undefined = ">=0.0.3"
|
|
38
37
|
yarl = "*"
|
|
39
|
-
zstandard = "*"
|
|
40
38
|
|
|
41
39
|
[tool.poetry.scripts]
|
|
42
40
|
python-urlopen = "urlopen.__main__:main"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# coding: utf-8
|
|
3
3
|
|
|
4
4
|
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
-
__version__ = (0, 1,
|
|
5
|
+
__version__ = (0, 1, 3)
|
|
6
6
|
__all__ = ["urlopen", "request", "download"]
|
|
7
7
|
|
|
8
8
|
import errno
|
|
@@ -10,7 +10,6 @@ import errno
|
|
|
10
10
|
from collections import UserString
|
|
11
11
|
from collections.abc import Buffer, Callable, Generator, Iterable, Mapping
|
|
12
12
|
from copy import copy
|
|
13
|
-
from gzip import decompress as decompress_gzip
|
|
14
13
|
from http.client import HTTPResponse
|
|
15
14
|
from http.cookiejar import CookieJar
|
|
16
15
|
from inspect import isgenerator
|
|
@@ -26,7 +25,6 @@ from urllib.request import (
|
|
|
26
25
|
build_opener, BaseHandler, HTTPCookieProcessor, HTTPSHandler,
|
|
27
26
|
HTTPRedirectHandler, OpenerDirector, Request,
|
|
28
27
|
)
|
|
29
|
-
from zlib import compressobj, DEF_MEM_LEVEL, DEFLATED, MAX_WBITS
|
|
30
28
|
|
|
31
29
|
from argtools import argcount
|
|
32
30
|
from cookietools import cookies_dict_to_str
|
|
@@ -34,7 +32,7 @@ from dicttools import iter_items
|
|
|
34
32
|
from filewrap import bio_skip_iter, bio_chunk_iter, SupportsRead, SupportsWrite
|
|
35
33
|
from http_request import normalize_request_args, SupportsGeturl
|
|
36
34
|
from http_response import (
|
|
37
|
-
get_filename, get_length, is_chunked, is_range_request,
|
|
35
|
+
decompress_response, get_filename, get_length, is_chunked, is_range_request,
|
|
38
36
|
parse_response,
|
|
39
37
|
)
|
|
40
38
|
from yarl import URL
|
|
@@ -62,45 +60,6 @@ class NoRedirectHandler(HTTPRedirectHandler):
|
|
|
62
60
|
return None
|
|
63
61
|
|
|
64
62
|
|
|
65
|
-
def decompress_deflate(data: bytes, compresslevel: int = 9) -> bytes:
|
|
66
|
-
# Fork from: https://stackoverflow.com/questions/1089662/python-inflate-and-deflate-implementations#answer-1089787
|
|
67
|
-
compress = compressobj(
|
|
68
|
-
compresslevel, # level: 0-9
|
|
69
|
-
DEFLATED, # method: must be DEFLATED
|
|
70
|
-
-MAX_WBITS, # window size in bits:
|
|
71
|
-
# -15..-8: negate, suppress header
|
|
72
|
-
# 8..15: normal
|
|
73
|
-
# 16..30: subtract 16, gzip header
|
|
74
|
-
DEF_MEM_LEVEL, # mem level: 1..8/9
|
|
75
|
-
0 # strategy:
|
|
76
|
-
# 0 = Z_DEFAULT_STRATEGY
|
|
77
|
-
# 1 = Z_FILTERED
|
|
78
|
-
# 2 = Z_HUFFMAN_ONLY
|
|
79
|
-
# 3 = Z_RLE
|
|
80
|
-
# 4 = Z_FIXED
|
|
81
|
-
)
|
|
82
|
-
deflated = compress.compress(data)
|
|
83
|
-
deflated += compress.flush()
|
|
84
|
-
return deflated
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
def decompress_response(response: HTTPResponse, /) -> bytes:
|
|
88
|
-
data = response.read()
|
|
89
|
-
content_encoding = response.headers.get("content-encoding")
|
|
90
|
-
match content_encoding:
|
|
91
|
-
case "gzip":
|
|
92
|
-
data = decompress_gzip(data)
|
|
93
|
-
case "deflate":
|
|
94
|
-
data = decompress_deflate(data)
|
|
95
|
-
case "br":
|
|
96
|
-
from brotli import decompress as decompress_br # type: ignore
|
|
97
|
-
data = decompress_br(data)
|
|
98
|
-
case "zstd":
|
|
99
|
-
from zstandard import decompress as decompress_zstd
|
|
100
|
-
data = decompress_zstd(data)
|
|
101
|
-
return data
|
|
102
|
-
|
|
103
|
-
|
|
104
63
|
def urlopen(
|
|
105
64
|
url: string | SupportsGeturl | URL | Request,
|
|
106
65
|
method: string = "GET",
|
|
@@ -279,7 +238,7 @@ def request[T](
|
|
|
279
238
|
return response
|
|
280
239
|
with response:
|
|
281
240
|
if isinstance(parse, bool):
|
|
282
|
-
data = decompress_response(response)
|
|
241
|
+
data = decompress_response(response.read(), response)
|
|
283
242
|
if parse:
|
|
284
243
|
return parse_response(response, data)
|
|
285
244
|
return data
|
|
@@ -287,8 +246,8 @@ def request[T](
|
|
|
287
246
|
if ac == 1:
|
|
288
247
|
return cast(Callable[[HTTPResponse], T], parse)(response)
|
|
289
248
|
else:
|
|
290
|
-
|
|
291
|
-
|
|
249
|
+
data = decompress_response(response.read(), response)
|
|
250
|
+
return cast(Callable[[HTTPResponse, bytes], T], parse)(response, data)
|
|
292
251
|
|
|
293
252
|
|
|
294
253
|
def download(
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|