3 # winfreed - download a selection of Free Software for MS Windows.
5 # Copyright (C) 2011 Antonio Ospite <ospite@studenti.unina.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 from progressbar import Bar, ETA, FileTransferSpeed, Percentage, ProgressBar
28 # TODO make OUTPUT_DIR and LANGCODE configurable from command line
29 OUTPUT_DIR = 'downloads'
32 # TODO PKG_DIR in $(datadir) or something like that for python projects
37 def get_pkg(json_file):
38 with open(json_file, mode='r') as f:
40 basename = os.path.basename(json_file)
41 package_name = os.path.splitext(basename)[0]
42 pkg['package_name'] = package_name
49 def process_all(path, cb):
50 listing = glob.glob(os.path.join(path, '*.json'))
51 for json_file in listing:
52 pkg = get_pkg(json_file)
54 sys.stderr.write("Error: cannot get a pkg for: %s\n" % json_file)
60 print 'Package: ', pkg['package_name']
61 print 'Program: ', pkg['name']
62 print 'Homepage: ', pkg['homepage']
67 # the "%s" in URLs are meant to be replaced with LANGCODE
69 pkg_url = pkg['URL'] % LANGCODE
74 response = urllib2.urlopen(pkg_url)
75 url = response.geturl()
79 # From http://paste.pound-python.org/show/9545/
80 # TODO: use a proper module to parse HTTP headers
81 if response.info().has_key('Content-Disposition') and len(response.info()['Content-Disposition'].split('filename=')) > 1:
82 # If the response has Content-Disposition, we take file name from it
83 filename = response.info()['Content-Disposition'].split('filename=')[1].decode('utf-8')
84 if filename[0] == '"' or filename[0] == "'":
85 filename = urllib2.unquote(filename.split('"')[1])
87 filename = urllib2.unquote(url.split('/')[-1].decode('utf_8'))
90 sys.stderr.write("Debug (%s): filename: %s url: %s\n" % (pkg['package_name'], filename, url))
93 destfile = os.path.join(OUTPUT_DIR, filename)
94 if os.path.exists(destfile):
95 sys.stderr.write("Warning (%s): %s exists!\n" % (pkg['package_name'], destfile))
98 outfile = open(destfile, mode='w')
100 total_size = response.info().getheader('Content-Length').strip()
101 total_size = int(total_size)
103 widgets = [pkg['name'], ' ', Percentage(), ' ', Bar(marker='=', left='[', right=']'),
104 ' ', ETA(), ' ', FileTransferSpeed()]
105 pbar = ProgressBar(widgets=widgets, maxval=total_size).start()
109 chunk = response.read(CHUNK_SIZE)
113 bytes_so_far += len(chunk)
115 pbar.update(bytes_so_far)
120 process_all(PKG_DIR, show)
124 if os.path.exists(OUTPUT_DIR) == False:
125 os.mkdir(OUTPUT_DIR, 0755)
127 process_all(PKG_DIR, download)
131 usage = "winfreed - download a selection of Free Software for MS Windows.\n\n"
132 usage += "usage: %s <COMMAND>\n\n" % sys.argv[0]
133 usage += "COMMANDS:\n"
134 usage += "\tshow Show info about all the available packages\n"
135 usage += "\tdownload Download all the packages\n"
138 if __name__ == "__main__":
140 if len(sys.argv) < 2:
144 if sys.argv[1] == 'download':
146 elif sys.argv[1] == 'show':