]> git.phdru.name Git - dotfiles.git/blob - bin/wget-download
Add script `wget-download`
[dotfiles.git] / bin / wget-download
1 #! /usr/bin/env python
2
3 from cgi import parse_qsl
4 from getopt import getopt, GetoptError
5 import os, posixpath
6 import sys, urllib
7 from m_lib.defenc import default_encoding
8
9 def usage():
10     sys.exit('Usage: %s [-e|--encoding=encoding] [-n|--newwin|-t|--tab] URL' % sys.argv[0])
11
12 try:
13     options, arguments = getopt(sys.argv[1:], 'e:', ['encoding='])
14 except GetoptError:
15     usage()
16
17 if len(arguments) != 1:
18     usage()
19
20 encoding = None
21
22 for option, value in options:
23     if option in ('-e', '--encoding'):
24         encoding = value
25
26 if not encoding:
27     encoding = default_encoding
28
29 url = arguments[0]
30 protocol, request = urllib.splittype(url)
31 user, password, port = None, None, None
32 host, path = urllib.splithost(request)
33 if host:
34    user, host = urllib.splituser(host)
35    if user:
36       user, password = urllib.splitpasswd(user)
37    host, port = urllib.splitport(host)
38    if port: port = int(port)
39 path, tag = urllib.splittag(path)
40 path, query = urllib.splitquery(path)
41 path = urllib.unquote(path)
42
43 if query:
44    qlist = []
45    for name, value in parse_qsl(query):
46       name = unicode(name, default_encoding).encode(encoding)
47       value = unicode(value, default_encoding).encode(encoding)
48       qlist.append((name, value))
49
50 url = protocol + "://"
51 if user:
52    url += urllib.quote(unicode(user, default_encoding).encode(encoding))
53    if password:
54       url += ':' + urllib.quote(unicode(password, default_encoding).encode(encoding))
55    url += '@'
56 if host:
57    url += host.decode(encoding).encode('idna')
58    if port:
59       url += ':%d' % port
60 if path:
61    if protocol == "file":
62       url += urllib.quote(path)
63    else:
64       url += urllib.quote(unicode(path, default_encoding).encode(encoding))
65 #if query:
66 #   url += '?' + urllib.urlencode(qlist)
67
68 name = posixpath.basename(path)
69 os.system('exec wget-wrapper -O "%s" "%s"' % (name, url))