python-urlopen 0.0.1.2__tar.gz → 0.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.
@@ -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.1.3
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.1.3"
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"
@@ -7,8 +7,8 @@ __doc__ = "python url downloader"
7
7
  from argparse import ArgumentParser, RawTextHelpFormatter
8
8
 
9
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")
10
+ parser.add_argument("url", nargs="?", help="URL to be downloaded")
11
+ parser.add_argument("-o", "--output-file", help="file path to be downloaded, if omitted, print into stdout")
12
12
  parser.add_argument("-r", "--resume", action="store_true", help="skip downloaded data")
13
13
  parser.add_argument("-hs", "--headers", help="dictionary of HTTP Headers to send with")
14
14
  parser.add_argument("-v", "--version", action="store_true", help="print the current version")
@@ -17,9 +17,11 @@ if args.version:
17
17
  from urlopen import __version__
18
18
  print(".".join(map(str, __version__)))
19
19
  raise SystemExit(0)
20
+ url = args.url
21
+ if not url:
22
+ parser.parse_args(["-h"])
20
23
 
21
24
  from collections import deque
22
- from os import makedirs
23
25
  from time import perf_counter
24
26
 
25
27
  from urlopen import download
@@ -45,35 +47,35 @@ def progress(total=None):
45
47
  print(f"\r\x1b[K{read_num} | {speed:.2f} MB/s", end="", flush=True)
46
48
  dq.append((read_num, cur_t))
47
49
 
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:
50
+ def main():
57
51
  headers = args.headers
58
52
  if headers is not None:
59
53
  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()
54
+
55
+ output_file = args.output_file
56
+ if output_file:
57
+ from os.path import dirname
58
+ dir_ = dirname(output_file)
59
+ if dir_:
60
+ from os import makedirs
61
+ makedirs(dir_, exist_ok=True)
62
+
63
+ download(
64
+ url,
65
+ output_file,
66
+ resume=args.resume,
67
+ make_reporthook=progress,
68
+ headers=headers,
69
+ )
70
+ else:
71
+ from sys import stdout
72
+
73
+ download(
74
+ url,
75
+ stdout,
76
+ headers=headers,
77
+ )
78
+
79
+ if __name__ == "__main__":
80
+ main()
79
81