108b719832c123f41ef55357b3c2f33d18ada303
[winfreed.git] / winfreed.py
1 #!/usr/bin/env python
2 #
3 # winfreed - download a selection of Free Software for MS Windows.
4 #
5 # Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
6 #
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.
11 #
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.
16 #
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/>.
19
20 import os
21 import sys
22 import glob
23 import json
24 import urllib2
25 from progressbar import Bar, ETA, FileTransferSpeed, Percentage, ProgressBar
26
27 # TODO make OUTPUT_DIR and LANGCODE configurable from command line
28 OUTPUT_DIR = 'downloads'
29 LANGCODE = 'en-US'
30
31 # TODO PKG_DIR in $(datadir) or something like that for python projects
32 PKG_DIR = 'pkgs'
33 CHUNK_SIZE = 8192
34
35 def get_pkg(json_file):
36     with open(json_file, mode='r') as f:
37         pkg = json.load(f)
38         basename = os.path.basename(json_file)
39         package_name = os.path.splitext(basename)[0]
40         pkg['package_name'] = package_name
41         f.close()
42         return pkg
43
44     return None
45
46 def process_all(path, cb):
47     listing = glob.glob(os.path.join(path, '*.json'))
48     for json_file in listing:
49         pkg = get_pkg(json_file)
50         if not pkg:
51             sys.stderr.write("Error: cannot get a pkg for: %s\n" % json_file)
52             continue
53         cb(pkg)
54
55 def show(pkg):
56     print 'Package:  ', pkg['package_name']
57     print 'Program:  ', pkg['name']
58     print 'Homepage: ', pkg['homepage']
59     print
60
61 def download(pkg):
62     # the "%s" in URLs are meant to be replaced with LANGCODE
63     try:
64         url = pkg['URL'] % LANGCODE
65     except:
66         url = pkg['URL']
67         pass
68
69     response = urllib2.urlopen(url)
70     filename = urllib2.unquote(os.path.basename(response.geturl()))
71     if filename == "":
72         sys.stderr.write("Debug (%s): filename: %s url: %s\n" %(pkg['package_name'], filename, response.geturl()))
73         return
74
75     destfile = os.path.join(OUTPUT_DIR, filename)
76     if os.path.exists(destfile):
77         sys.stderr.write("Warning (%s): %s exists!\n" % (pkg['package_name'], destfile))
78         return
79
80     outfile = open(destfile, mode='w')
81
82     total_size = response.info().getheader('Content-Length').strip()
83     total_size = int(total_size)
84
85     widgets = [pkg['name'], ' ', Percentage(), ' ', Bar(marker='=', left='[', right=']'),
86                ' ', ETA(), ' ', FileTransferSpeed()]
87     pbar = ProgressBar(widgets=widgets, maxval=total_size).start()
88
89     bytes_so_far = 0
90     while 1:
91         chunk = response.read(CHUNK_SIZE)
92         if not chunk:
93             break
94
95         bytes_so_far += len(chunk)
96         outfile.write(chunk)
97         pbar.update(bytes_so_far)
98     pbar.finish()
99
100 def show_all():
101     process_all(PKG_DIR, show)
102
103 def download_all():
104     if os.path.exists(OUTPUT_DIR) == False:
105         os.mkdir(OUTPUT_DIR, 0755)
106
107     process_all(PKG_DIR, download)
108
109 def usage():
110     usage = "winfreed - download a selection of Free Software for MS Windows.\n\n"
111     usage += "usage: %s <COMMAND>\n\n" % sys.argv[0]
112     usage += "COMMANDS:\n"
113     usage += "\tshow        Show info about all the available packages\n"
114     usage += "\tdownload    Download all the packages\n"
115     print usage
116
117 if __name__ == "__main__":
118
119     if len(sys.argv) < 2:
120         usage()
121         sys.exit(1)
122
123     if sys.argv[1] == 'download':
124         download_all()
125     elif sys.argv[1] == 'show':
126         show_all()
127     else:
128         usage()
129         sys.exit(1)
130     
131     sys.exit(0)