]> git.phdru.name Git - dotfiles.git/commitdiff
Add script `wget-download`
authorOleg Broytman <phd@phdru.name>
Sat, 21 Sep 2019 00:12:09 +0000 (03:12 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 21 Sep 2019 00:12:09 +0000 (03:12 +0300)
.shellrc
bin/wd [new symlink]
bin/wget-download [new file with mode: 0755]

index 513431d56037a9e60c7e5774e003ec8f67957160..044801b10974c82eb3fcef1afe04a628f847e017 100644 (file)
--- 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 \
    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
 
          ! has_completion $cmd && complete -A hostname $cmd
    done
 
diff --git a/bin/wd b/bin/wd
new file mode 120000 (symlink)
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 (executable)
index 0000000..da7ea39
--- /dev/null
@@ -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))