-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewspaper3kli.py
executable file
·74 lines (53 loc) · 2.09 KB
/
newspaper3kli.py
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
#!/usr/bin/env python3
import argparse
import asyncio
import itertools
import os
import sys
from lib.http_client import HttpClient
async def task(http_client, url):
"""
Tiny asynchronous function to download content using http_client
"""
http_client.get_text(url)
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('urls',
nargs='*',
default=[],
help="URL to download content from (single download)")
parser.add_argument('-o', '--output',
type=str,
default=None,
help=('Output path to store the results.'
'Defaults to "Downloads" directory'))
parser.add_argument('-u', '--disable-verify-ssl',
action='store_false',
help="Flag to disable SSL certificate verification.")
parser.add_argument('--keep-html',
action='store_true',
help="Flag to save content with HTML.")
return parser.parse_args()
def main():
args = parse_arguments()
urls = [line.replace("\r", "").replace("\n", "") for line in sys.stdin] \
if not sys.stdin.isatty() \
else args.urls
output_path = (args.output or
# fallback to ${HOME}/Downloads whether using XDG or not
os.path.join(os.getenv("XDG_DOWNLOAD_DIR",
os.path.join(os.getenv("HOME"),
"Downloads")),
"newspaper3k"))
if not os.path.exists(output_path):
os.makedirs(output_path)
loop = asyncio.get_event_loop()
args = [(HttpClient(verify=args.disable_verify_ssl,
keep_html=args.keep_html,
output_path=output_path),
url) for url in urls]
tasks = itertools.starmap(task, args)
loop.run_until_complete(asyncio.gather(*tasks))
loop.close()
if __name__ == '__main__':
main()