-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwget.py
More file actions
67 lines (62 loc) · 1.86 KB
/
wget.py
File metadata and controls
67 lines (62 loc) · 1.86 KB
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
'''
Downloads a web resource.
Provide no argument to enter interactive mode.
'''
from urllib.request import urlopen
import sys
import os
from os import path
from pathlib import Path
import unicodedata, re
from interactive import listen
from urllib.parse import urlsplit
import platform
if platform.system() == 'Windows':
DEFAULT = 'D:/downloads/'
else:
DEFAULT = '/sdcard/download/'
def main():
if path.samefile(os.getcwd(), str(Path.home())):
os.chdir(DEFAULT)
if len(sys.argv) >= 2:
url = sys.argv[1]
if len(sys.argv) == 3:
filename = sys.argv[2]
else:
filename = None
wget(url, filename)
else:
from console import console
console(globals())
def wget(url, filename = None):
if filename is None:
filename = guessFilename(url)
if path.isfile(filename):
print(filename, 'already exists. Overwrite? ')
if listen('yn') != b'y':
print('canceled. ')
return
print('Downloading...')
try:
response = urlopen(url)
except ValueError:
response = urlopen('https://' + url)
with open(filename, 'wb+') as f:
f.write(response.read())
print('Downloaded to', filename)
def guessFilename(url):
scheme, _, remote_path, _, _ = urlsplit(url)
if not scheme:
url = 'https://' + url
scheme, _, remote_path, _, _ = urlsplit(url)
filename = path.basename(remote_path.strip('/')) or 'index.html'
if '.' not in filename:
filename += '.html'
# Code copied from django: https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename
filename = unicodedata.normalize('NFKD', filename)
filename = re.sub('[^\w\s.-]', '', filename).strip()
filename = re.sub('[-\s]+', '-', filename)
return filename
if __name__ == '__main__':
main()
sys.exit(0)