Sylvain Munaut | aa55d30 | 2010-11-12 19:36:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import urllib2 |
| 4 | import os |
| 5 | import sys |
| 6 | import zipfile |
| 7 | |
| 8 | try: |
| 9 | import cStringIO as StringIO |
| 10 | except: |
| 11 | import StringIO |
| 12 | |
| 13 | |
Sylvain Munaut | 8b5e0c7 | 2013-12-13 15:41:01 +0100 | [diff] [blame] | 14 | SRC = "http://www.3gpp.org/ftp/Specs/archive/06_series/06.06/0606-421.zip" |
Sylvain Munaut | aa55d30 | 2010-11-12 19:36:21 +0100 | [diff] [blame] | 15 | |
| 16 | |
| 17 | def get_zipfile(data): |
| 18 | return zipfile.ZipFile(StringIO.StringIO(data)) |
| 19 | |
| 20 | |
| 21 | def get_subfile_data(data, filename): |
| 22 | z = get_zipfile(data) |
| 23 | return z.read(filename) |
| 24 | |
| 25 | |
| 26 | def process_file(z, e): |
Sylvain Munaut | 8966846 | 2015-12-30 10:56:13 +0100 | [diff] [blame] | 27 | fh = open(os.path.basename(e.filename.lower()), 'w') |
Sylvain Munaut | aa55d30 | 2010-11-12 19:36:21 +0100 | [diff] [blame] | 28 | d = z.read(e).replace('\r','') |
| 29 | fh.write(d) |
| 30 | fh.close() |
| 31 | |
| 32 | |
| 33 | def main(*args): |
| 34 | |
| 35 | # Args |
| 36 | if len(args) != 2: |
| 37 | print "Usage: %s target_dir" % args[0] |
| 38 | return |
| 39 | |
| 40 | tgt = args[1] |
| 41 | |
| 42 | # Create and go to target dir |
| 43 | if not os.path.isdir(tgt): |
| 44 | os.mkdir(tgt) |
| 45 | os.chdir(tgt) |
| 46 | |
| 47 | # Get the original data |
| 48 | u = urllib2.urlopen(SRC) |
| 49 | d = u.read() |
| 50 | |
| 51 | # Get DISK.zip |
| 52 | d = get_subfile_data(d, 'DISK.zip') |
| 53 | |
| 54 | # Get Dir_C.zip |
| 55 | d = get_subfile_data(d, 'Dir_C.zip') |
| 56 | |
| 57 | # Get zip file object |
| 58 | z = get_zipfile(d) |
| 59 | |
| 60 | # Save each file |
| 61 | for e in z.filelist: |
| 62 | process_file(z, e) |
| 63 | |
| 64 | |
| 65 | if __name__ == '__main__': |
| 66 | main(*sys.argv) |