python-urlopen 0.0.1.2__tar.gz → 0.0.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-urlopen
3
- Version: 0.0.1.2
3
+ Version: 0.0.2
4
4
  Summary: Python urlopen wrapper.
5
5
  Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-urlopen
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-urlopen"
3
- version = "0.0.1.2"
3
+ version = "0.0.2"
4
4
  description = "Python urlopen wrapper."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"
@@ -30,6 +30,10 @@ python = "^3.10"
30
30
  http_response = "*"
31
31
  python-filewrap = "*"
32
32
 
33
+ [tool.poetry.scripts]
34
+ python-urlopen = "urlopen.__main__:main"
35
+ urlopen = "urlopen.__main__:main"
36
+
33
37
  [build-system]
34
38
  requires = ["poetry-core"]
35
39
  build-backend = "poetry.core.masonry.api"
@@ -2,7 +2,7 @@
2
2
  # coding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 1)
5
+ __version__ = (0, 0, 2)
6
6
  __all__ = ["urlopen", "download"]
7
7
 
8
8
  import errno
@@ -0,0 +1,91 @@
1
+ #!/usr/bin/env python3
2
+ # coding: utf-8
3
+
4
+ __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
+ __doc__ = "python urlopen"
6
+
7
+ from argparse import ArgumentParser, RawTextHelpFormatter
8
+ from collections import deque
9
+ from time import perf_counter
10
+
11
+ from . import download, __version__
12
+
13
+
14
+ def parse_args():
15
+ parser = ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter)
16
+
17
+ parser.add_argument("url", nargs="?", help="URL to be downloaded")
18
+ parser.add_argument("-o", "--output-file", help="file path to be downloaded, if omitted, print into stdout")
19
+ parser.add_argument("-r", "--resume", action="store_true", help="skip downloaded data")
20
+ parser.add_argument("-hs", "--headers", help="dictionary of HTTP Headers to send with")
21
+ parser.add_argument("-v", "--version", action="store_true", help="print the current version")
22
+
23
+ args = parser.parse_args()
24
+
25
+ if args.version:
26
+ print(".".join(map(str, __version__)))
27
+ raise SystemExit(0)
28
+
29
+ if not args.url:
30
+ parser.parse_args(["-h"])
31
+
32
+
33
+ def headers_str_to_dict(headers: str, /) -> dict[str, str]:
34
+ return dict(
35
+ header.split(": ", 1)
36
+ for header in headers.strip("\n").split("\n")
37
+ )
38
+
39
+
40
+ def progress(total=None):
41
+ dq: deque[tuple[int, float]] = deque(maxlen=64)
42
+ read_num = 0
43
+ dq.append((read_num, perf_counter()))
44
+ while True:
45
+ read_num += yield
46
+ cur_t = perf_counter()
47
+ speed = (read_num - dq[0][0]) / 1024 / 1024 / (cur_t - dq[0][1])
48
+ if total:
49
+ percentage = read_num / total * 100
50
+ print(f"\r\x1b[K{read_num} / {total} | {speed:.2f} MB/s | {percentage:.2f} %", end="", flush=True)
51
+ else:
52
+ print(f"\r\x1b[K{read_num} | {speed:.2f} MB/s", end="", flush=True)
53
+ dq.append((read_num, cur_t))
54
+
55
+
56
+ def main():
57
+ args = parse_args()
58
+ url = args.url
59
+
60
+ headers = args.headers
61
+ if headers is not None:
62
+ headers = headers_str_to_dict(headers)
63
+
64
+ output_file = args.output_file
65
+ if output_file:
66
+ from os.path import dirname
67
+ dir_ = dirname(output_file)
68
+ if dir_:
69
+ from os import makedirs
70
+ makedirs(dir_, exist_ok=True)
71
+
72
+ download(
73
+ url,
74
+ output_file,
75
+ resume=args.resume,
76
+ make_reporthook=progress,
77
+ headers=headers,
78
+ )
79
+ else:
80
+ from sys import stdout
81
+
82
+ download(
83
+ url,
84
+ stdout.buffer,
85
+ headers=headers,
86
+ )
87
+
88
+
89
+ if __name__ == "__main__":
90
+ main()
91
+
@@ -1,79 +0,0 @@
1
- #!/usr/bin/env python3
2
- # coding: utf-8
3
-
4
- __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __doc__ = "python url downloader"
6
-
7
- from argparse import ArgumentParser, RawTextHelpFormatter
8
-
9
- parser = ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter)
10
- parser.add_argument("urls", nargs="*", metavar="url", help="URL(s) to be downloaded (one URL per line), if omitted, read from stdin")
11
- parser.add_argument("-d", "--savedir", default="", help="directory to the downloading files")
12
- parser.add_argument("-r", "--resume", action="store_true", help="skip downloaded data")
13
- parser.add_argument("-hs", "--headers", help="dictionary of HTTP Headers to send with")
14
- parser.add_argument("-v", "--version", action="store_true", help="print the current version")
15
- args = parser.parse_args()
16
- if args.version:
17
- from urlopen import __version__
18
- print(".".join(map(str, __version__)))
19
- raise SystemExit(0)
20
-
21
- from collections import deque
22
- from os import makedirs
23
- from time import perf_counter
24
-
25
- from urlopen import download
26
-
27
- def headers_str_to_dict(headers: str, /) -> dict[str, str]:
28
- return dict(
29
- header.split(": ", 1)
30
- for header in headers.strip("\n").split("\n")
31
- )
32
-
33
- def progress(total=None):
34
- dq: deque[tuple[int, float]] = deque(maxlen=64)
35
- read_num = 0
36
- dq.append((read_num, perf_counter()))
37
- while True:
38
- read_num += yield
39
- cur_t = perf_counter()
40
- speed = (read_num - dq[0][0]) / 1024 / 1024 / (cur_t - dq[0][1])
41
- if total:
42
- percentage = read_num / total * 100
43
- print(f"\r\x1b[K{read_num} / {total} | {speed:.2f} MB/s | {percentage:.2f} %", end="", flush=True)
44
- else:
45
- print(f"\r\x1b[K{read_num} | {speed:.2f} MB/s", end="", flush=True)
46
- dq.append((read_num, cur_t))
47
-
48
- urls = args.urls
49
- if not urls:
50
- from sys import stdin
51
- urls = (l.removesuffix("\n") for l in stdin)
52
- savedir = args.savedir
53
- if savedir:
54
- makedirs(savedir, exist_ok=True)
55
-
56
- try:
57
- headers = args.headers
58
- if headers is not None:
59
- headers = headers_str_to_dict(headers)
60
- for url in urls:
61
- if not url:
62
- continue
63
- try:
64
- file = download(
65
- url,
66
- savedir,
67
- resume=args.resume,
68
- make_reporthook=progress,
69
- headers=headers,
70
- )
71
- print(f"\r\x1b[K\x1b[1;32mDOWNLOADED\x1b[0m \x1b[4;34m{url!r}\x1b[0m\n |_ ⏬ \x1b[4;34m{file!r}\x1b[0m")
72
- except BaseException as e:
73
- print(f"\r\x1b[K\x1b[1;31mERROR\x1b[0m \x1b[4;34m{url!r}\x1b[0m\n |_ 🙅 \x1b[1;31m{type(e).__qualname__}\x1b[0m: {e}")
74
- except (EOFError, KeyboardInterrupt):
75
- pass
76
- except BrokenPipeError:
77
- from sys import stderr
78
- stderr.close()
79
-
File without changes