]> git.phdru.name Git - dotfiles.git/blob - bin/wget-download
bin/compyleall: Do not check error code
[dotfiles.git] / bin / wget-download
1 #! /usr/bin/env python3
2
3 try:
4     from urllib.parse import parse_qsl, urlencode, \
5         quote, quote_plus, unquote, unquote_plus, \
6         splittype, splithost, splituser, splitpasswd, \
7         splitport, splittag, splitquery
8 except ImportError:
9     from cgi import parse_qsl
10     from urllib import urlencode, quote, quote_plus, unquote, unquote_plus, \
11         splittype, splithost, splituser, splitpasswd, \
12         splitport, splittag, splitquery
13 from getopt import getopt, GetoptError
14 import os, posixpath
15 import sys
16 from m_lib.defenc import default_encoding
17
18 def usage():
19     sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL' % sys.argv[0])
20
21 try:
22     options, arguments = getopt(sys.argv[1:], 'e:', ['encoding='])
23 except GetoptError:
24     usage()
25
26 if len(arguments) != 1:
27     usage()
28
29 encoding = None
30
31 for option, value in options:
32     if option in ('-e', '--encoding'):
33         encoding = value
34
35 if not encoding:
36     encoding = default_encoding
37
38 url = arguments[0]
39 protocol, request = splittype(url)
40 user, password, port = None, None, None
41 host, path = splithost(request)
42 if host:
43     user, host = splituser(host)
44     if user:
45         user, password = splitpasswd(user)
46     host, port = splitport(host)
47     if port: port = int(port)
48 path, tag = splittag(path)
49 path, query = splitquery(path)
50 path = unquote(path)
51 if tag: tag = unquote_plus(tag)
52
53 if query:
54     qlist = []
55     for name, value in parse_qsl(query):
56         if isinstance(name, bytes):
57             name = name.decode(default_encoding)
58             value = value.decode(default_encoding)
59         name = name.encode(encoding)
60         value = value.encode(encoding)
61         qlist.append((name, value))
62
63 url = protocol + "://"
64 if user:
65     if isinstance(user, bytes):
66         user = user.decode(default_encoding)
67     url += quote(user.encode(encoding))
68     if password:
69         if isinstance(password, bytes):
70             password = password.decode(default_encoding)
71         url += ':' + quote(password.encode(encoding))
72     url += '@'
73 if host:
74     if isinstance(host, bytes):
75         host = host.decode(encoding)
76     url += host.encode('idna').decode('ascii')
77     if port:
78         url += ':%d' % port
79 if path:
80     if protocol == "file":
81         url += quote(path)
82     else:
83         if isinstance(path, bytes):
84             path = path.decode(default_encoding)
85         url += quote(path.encode(encoding))
86 if query:
87     url += '?' + urlencode(qlist)
88 if tag:
89     if isinstance(tag, bytes):
90         tag = tag.decode(default_encoding)
91     url += '#' + quote_plus(tag.encode(encoding))
92
93 name = posixpath.basename(path)
94 os.system('exec wget-wrapper -O "%s" "%s"' % (name, url))