1 #! /usr/bin/env python3
4 from urllib.parse import parse_qsl, urlencode, \
5 quote, quote_plus, unquote, unquote_plus, \
6 splittype, splithost, splituser, splitpasswd, \
7 splitport, splittag, splitquery
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
16 from m_lib.defenc import default_encoding
19 sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL' % sys.argv[0])
22 options, arguments = getopt(sys.argv[1:], 'e:', ['encoding='])
26 if len(arguments) != 1:
31 for option, value in options:
32 if option in ('-e', '--encoding'):
36 encoding = default_encoding
39 protocol, request = splittype(url)
40 user, password, port = None, None, None
41 host, path = splithost(request)
43 user, host = splituser(host)
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)
51 if tag: tag = unquote_plus(tag)
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))
63 url = protocol + "://"
65 if isinstance(user, bytes):
66 user = user.decode(default_encoding)
67 url += quote(user.encode(encoding))
69 if isinstance(password, bytes):
70 password = password.decode(default_encoding)
71 url += ':' + quote(password.encode(encoding))
74 if isinstance(host, bytes):
75 host = host.decode(encoding)
76 url += host.encode('idna').decode('ascii')
80 if protocol == "file":
83 if isinstance(path, bytes):
84 path = path.decode(default_encoding)
85 url += quote(path.encode(encoding))
87 url += '?' + urlencode(qlist)
89 if isinstance(tag, bytes):
90 tag = tag.decode(default_encoding)
91 url += '#' + quote_plus(tag.encode(encoding))
93 name = posixpath.basename(path)
94 os.system('exec wget-wrapper -O "%s" "%s"' % (name, url))