From: Oleg Broytman Date: Sat, 21 Sep 2019 00:12:09 +0000 (+0300) Subject: Add script `wget-download` X-Git-Url: https://git.phdru.name/?p=dotfiles.git;a=commitdiff_plain;h=bc64e79021fc792c18f18a989bc6079a679a7738 Add script `wget-download` --- diff --git a/.shellrc b/.shellrc index 513431d..044801b 100644 --- a/.shellrc +++ b/.shellrc @@ -113,7 +113,7 @@ if test -n "$BASH_VERSION"; then for cmd in dig host mtr nslookup nc netcat nmap p ping ping6 socat \ telnet t tt \ tcptraceroute tcptraceroute6 tracert tracert6 traceroute traceroute6 \ - whois wget wget-m wget-wrapper ww; do + whois wd wget wget-download wget-m wget-wrapper ww; do ! has_completion $cmd && complete -A hostname $cmd done diff --git a/bin/wd b/bin/wd new file mode 120000 index 0000000..06952f8 --- /dev/null +++ b/bin/wd @@ -0,0 +1 @@ +wget-download \ No newline at end of file diff --git a/bin/wget-download b/bin/wget-download new file mode 100755 index 0000000..da7ea39 --- /dev/null +++ b/bin/wget-download @@ -0,0 +1,69 @@ +#! /usr/bin/env python + +from cgi import parse_qsl +from getopt import getopt, GetoptError +import os, posixpath +import sys, urllib +from m_lib.defenc import default_encoding + +def usage(): + sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL' % sys.argv[0]) + +try: + options, arguments = getopt(sys.argv[1:], 'e:', ['encoding=']) +except GetoptError: + usage() + +if len(arguments) != 1: + usage() + +encoding = None + +for option, value in options: + if option in ('-e', '--encoding'): + encoding = value + +if not encoding: + encoding = default_encoding + +url = arguments[0] +protocol, request = urllib.splittype(url) +user, password, port = None, None, None +host, path = urllib.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) + +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)) + +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 host: + url += host.decode(encoding).encode('idna') + 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) + +name = posixpath.basename(path) +os.system('exec wget-wrapper -O "%s" "%s"' % (name, url))