2022-12-05 21:00:02 +00:00
|
|
|
#!/usr/bin/env python3
|
2010-11-12 18:36:21 +00:00
|
|
|
|
2022-12-05 21:00:02 +00:00
|
|
|
import logging as log
|
|
|
|
import urllib.request
|
|
|
|
import io
|
2010-11-12 18:36:21 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
|
2022-12-05 21:00:02 +00:00
|
|
|
URL = "http://www.3gpp.org/ftp/Specs/archive/06_series/06.06/0606-421.zip"
|
2010-11-12 18:36:21 +00:00
|
|
|
|
|
|
|
|
2022-12-05 21:00:02 +00:00
|
|
|
def get_zipfile(data: bytes) -> zipfile.ZipFile:
|
|
|
|
return zipfile.ZipFile(io.BytesIO(data), 'r')
|
2010-11-12 18:36:21 +00:00
|
|
|
|
|
|
|
|
2022-12-05 21:00:02 +00:00
|
|
|
def get_subfile_data(data: bytes, filename: str):
|
|
|
|
log.debug('Unpacking \'%s\'', filename)
|
2010-11-12 18:36:21 +00:00
|
|
|
z = get_zipfile(data)
|
|
|
|
return z.read(filename)
|
|
|
|
|
|
|
|
|
|
|
|
def process_file(z, e):
|
2022-12-05 21:00:02 +00:00
|
|
|
log.debug('Processing file \'%s\'', e.filename.lower())
|
|
|
|
fh = open(os.path.basename(e.filename.lower()), 'wb')
|
|
|
|
d = z.read(e).replace(b'\r', b'')
|
2010-11-12 18:36:21 +00:00
|
|
|
fh.write(d)
|
|
|
|
fh.close()
|
|
|
|
|
|
|
|
|
|
|
|
def main(*args):
|
|
|
|
|
|
|
|
# Args
|
|
|
|
if len(args) != 2:
|
2022-12-05 21:00:02 +00:00
|
|
|
print("Usage: %s target_dir" % args[0])
|
2010-11-12 18:36:21 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
tgt = args[1]
|
|
|
|
|
|
|
|
# Create and go to target dir
|
|
|
|
if not os.path.isdir(tgt):
|
|
|
|
os.mkdir(tgt)
|
|
|
|
os.chdir(tgt)
|
|
|
|
|
|
|
|
# Get the original data
|
2022-12-05 21:00:02 +00:00
|
|
|
log.info('Requesting file: %s', URL)
|
|
|
|
with urllib.request.urlopen(URL) as response:
|
|
|
|
log.debug('Response code: %d', response.code)
|
|
|
|
assert response.code == 200
|
|
|
|
|
|
|
|
for h in ('Last-Modified', 'Content-Type', 'Content-Length'):
|
|
|
|
log.debug('%s: %s', h, response.getheader(h))
|
|
|
|
|
|
|
|
log.info('Downloading %d bytes...', response.length)
|
|
|
|
d = response.read()
|
2010-11-12 18:36:21 +00:00
|
|
|
|
|
|
|
# Get DISK.zip
|
|
|
|
d = get_subfile_data(d, 'DISK.zip')
|
|
|
|
|
|
|
|
# Get Dir_C.zip
|
|
|
|
d = get_subfile_data(d, 'Dir_C.zip')
|
|
|
|
|
|
|
|
# Get zip file object
|
|
|
|
z = get_zipfile(d)
|
|
|
|
|
|
|
|
# Save each file
|
|
|
|
for e in z.filelist:
|
|
|
|
process_file(z, e)
|
|
|
|
|
|
|
|
|
2022-12-05 21:00:02 +00:00
|
|
|
log.basicConfig(format='[%(levelname)s] %(filename)s:%(lineno)d %(message)s', level=log.DEBUG)
|
|
|
|
|
2010-11-12 18:36:21 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main(*sys.argv)
|