X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=bin%2Fwget-download;fp=bin%2Fwget-download;h=dbd6ef5bfd4cf13390630223ac3f093740ccd86a;hb=3fdf2f7baaa3a67880de95948d6ee76bc698fbc9;hp=da7ea395a36077f75e16201eb4825532f3e377e9;hpb=0477080a03713846f689faf58a3d0df4028e32fc;p=dotfiles.git diff --git a/bin/wget-download b/bin/wget-download index da7ea39..dbd6ef5 100755 --- a/bin/wget-download +++ b/bin/wget-download @@ -1,9 +1,18 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 -from cgi import parse_qsl +try: + from urllib.parse import parse_qsl, urlencode, \ + quote, quote_plus, unquote, unquote_plus, \ + splittype, splithost, splituser, splitpasswd, \ + splitport, splittag, splitquery +except ImportError: + from cgi import parse_qsl + from urllib import urlencode, quote, quote_plus, unquote, unquote_plus, \ + splittype, splithost, splituser, splitpasswd, \ + splitport, splittag, splitquery from getopt import getopt, GetoptError import os, posixpath -import sys, urllib +import sys from m_lib.defenc import default_encoding def usage(): @@ -27,43 +36,59 @@ if not encoding: encoding = default_encoding url = arguments[0] -protocol, request = urllib.splittype(url) +protocol, request = splittype(url) user, password, port = None, None, None -host, path = urllib.splithost(request) +host, path = splithost(request) if host: - user, host = urllib.splituser(host) - if user: - user, password = urllib.splitpasswd(user) - host, port = urllib.splitport(host) - if port: port = int(port) -path, tag = urllib.splittag(path) -path, query = urllib.splitquery(path) -path = urllib.unquote(path) + user, host = splituser(host) + if user: + user, password = splitpasswd(user) + host, port = splitport(host) + if port: port = int(port) +path, tag = splittag(path) +path, query = splitquery(path) +path = unquote(path) +if tag: tag = unquote_plus(tag) if query: - qlist = [] - for name, value in parse_qsl(query): - name = unicode(name, default_encoding).encode(encoding) - value = unicode(value, default_encoding).encode(encoding) - qlist.append((name, value)) + qlist = [] + for name, value in parse_qsl(query): + if isinstance(name, bytes): + name = name.decode(default_encoding) + value = value.decode(default_encoding) + name = name.encode(encoding) + value = value.encode(encoding) + qlist.append((name, value)) url = protocol + "://" if user: - url += urllib.quote(unicode(user, default_encoding).encode(encoding)) - if password: - url += ':' + urllib.quote(unicode(password, default_encoding).encode(encoding)) - url += '@' + if isinstance(user, bytes): + user = user.decode(default_encoding) + url += quote(user.encode(encoding)) + if password: + if isinstance(password, bytes): + password = password.decode(default_encoding) + url += ':' + quote(password.encode(encoding)) + url += '@' if host: - url += host.decode(encoding).encode('idna') - if port: - url += ':%d' % port + if isinstance(host, bytes): + host = host.decode(encoding) + url += host.encode('idna').decode('ascii') + if port: + url += ':%d' % port if path: - if protocol == "file": - url += urllib.quote(path) - else: - url += urllib.quote(unicode(path, default_encoding).encode(encoding)) -#if query: -# url += '?' + urllib.urlencode(qlist) + if protocol == "file": + url += quote(path) + else: + if isinstance(path, bytes): + path = path.decode(default_encoding) + url += quote(path.encode(encoding)) +if query: + url += '?' + urlencode(qlist) +if tag: + if isinstance(tag, bytes): + tag = tag.decode(default_encoding) + url += '#' + quote_plus(tag.encode(encoding)) name = posixpath.basename(path) os.system('exec wget-wrapper -O "%s" "%s"' % (name, url))