1 #! /usr/bin/env python3
3 from getopt import getopt, GetoptError
4 from urllib.parse import urlsplit, parse_qsl, quote, quote_plus, urlencode
8 from m_lib.defenc import default_encoding
12 sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL'
17 options, arguments = getopt(sys.argv[1:], 'e:', ['encoding='])
21 if len(arguments) != 1:
26 for option, value in options:
27 if option in ('-e', '--encoding'):
31 encoding = default_encoding
35 split_results = urlsplit(url)
36 protocol, netloc, path, query, tag = split_results
37 user = split_results.username
38 password = split_results.password
39 host = split_results.hostname
40 port = split_results.port
44 for name, value in parse_qsl(query):
45 if isinstance(name, bytes):
46 name = name.decode(default_encoding)
47 value = value.decode(default_encoding)
48 name = name.encode(encoding)
49 value = value.encode(encoding)
50 qlist.append((name, value))
52 url = protocol + "://"
54 if isinstance(user, bytes):
55 user = user.decode(default_encoding)
56 url += quote(user.encode(encoding))
58 if isinstance(password, bytes):
59 password = password.decode(default_encoding)
60 url += ':' + quote(password.encode(encoding))
63 if isinstance(host, bytes):
64 host = host.decode(encoding)
65 url += host.encode('idna').decode('ascii')
69 if protocol == "file":
72 if isinstance(path, bytes):
73 path = path.decode(default_encoding)
74 url += quote(path.encode(encoding))
76 url += '?' + urlencode(qlist)
78 if isinstance(tag, bytes):
79 tag = tag.decode(default_encoding)
80 url += '#' + quote_plus(tag.encode(encoding))
82 name = posixpath.basename(path)
83 os.system('exec wget-wrapper -O "%s" "%s"' % (name, url))