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
36 def get_pkg(json_file):
37 with open(json_file, mode='r') as f:
39 basename = os.path.basename(json_file)
40 package_name = os.path.splitext(basename)[0]
41 pkg['package_name'] = package_name
47 def process_all(path, cb):
48 listing = glob.glob(os.path.join(path, '*.json'))
49 for json_file in listing:
50 pkg = get_pkg(json_file)
52 sys.stderr.write("Error: cannot get a pkg for: %s\n" % json_file)
57 print 'Package: ', pkg['package_name']
58 print 'Program: ', pkg['name']
59 print 'Homepage: ', pkg['homepage']
63 # the "%s" in URLs are meant to be replaced with LANGCODE
65 url = pkg['URL'] % LANGCODE
70 response = urllib2.urlopen(url)
73 if 'Content-Disposition' in response.info():
74 # Use the filename the server tells us if any,
75 # re pattern from http://stackoverflow.com/questions/8035900
76 content_disposition = response.info().getheader('Content-Disposition').strip()
77 filename = re.findall("filename=(\S+)", content_disposition)[0]
80 filename = urllib2.unquote(os.path.basename(response.geturl()))
83 sys.stderr.write("Debug (%s): filename: %s url: %s\n" %(pkg['package_name'], filename, response.geturl()))
86 destfile = os.path.join(OUTPUT_DIR, filename)
87 if os.path.exists(destfile):
88 sys.stderr.write("Warning (%s): %s exists!\n" % (pkg['package_name'], destfile))
91 outfile = open(destfile, mode='w')
93 total_size = response.info().getheader('Content-Length').strip()
94 total_size = int(total_size)
96 widgets = [pkg['name'], ' ', Percentage(), ' ', Bar(marker='=', left='[', right=']'),
97 ' ', ETA(), ' ', FileTransferSpeed()]
98 pbar = ProgressBar(widgets=widgets, maxval=total_size).start()
102 chunk = response.read(CHUNK_SIZE)
106 bytes_so_far += len(chunk)
108 pbar.update(bytes_so_far)
112 process_all(PKG_DIR, show)
115 if os.path.exists(OUTPUT_DIR) == False:
116 os.mkdir(OUTPUT_DIR, 0755)
118 process_all(PKG_DIR, download)
121 usage = "winfreed - download a selection of Free Software for MS Windows.\n\n"
122 usage += "usage: %s <COMMAND>\n\n" % sys.argv[0]
123 usage += "COMMANDS:\n"
124 usage += "\tshow Show info about all the available packages\n"
125 usage += "\tdownload Download all the packages\n"
128 if __name__ == "__main__":
130 if len(sys.argv) < 2:
134 if sys.argv[1] == 'download':
136 elif sys.argv[1] == 'show':