-
Notifications
You must be signed in to change notification settings - Fork 1
/
psp_cz_import.py
64 lines (54 loc) · 2.02 KB
/
psp_cz_import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import urllib
import os
import logging
from importer.psp_cz import import_all
from optparse import OptionParser
from zipfile import ZipFile
class PspCzImport(object):
files = {'poslanci.zip': 'http://www.psp.cz/eknih/cdrom/opendata/poslanci.zip',
'hl-2010ps.zip': 'http://www.psp.cz/eknih/cdrom/opendata/hl-2010ps.zip'}
def __init__(self, data_dir):
self.data_dir = data_dir if data_dir[-1:] == '/' else data_dir + '/'
def _file_download(self, source, destination):
urllib.urlretrieve(source, destination)
def download_files(self):
for file, url in self.files.iteritems():
logging.info('Downloading ' + url)
file_path = self.data_dir + file
if os.access(file_path, os.F_OK):
os.remove(file_path)
urllib.urlretrieve(url, file_path)
def unzip_files(self):
for file in self.files.keys():
zip = ZipFile(self.data_dir + file)
try:
# FIXME: this is potentially dangerous, should check the
# zip does not use absolute paths or ..
logging.info('Extracting ' + file)
zip.extractall(self.data_dir)
finally:
zip.close()
def all(self):
"""
Downloads zip files from psp.cz, extracts them and imports into the
dtabase.
"""
self.download_files()
self.unzip_files()
logging.info('Starting import of the files...')
import_all(self.data_dir)
def main():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
parser = OptionParser()
parser.add_option("-d", "--dir", dest="data_dir",
help="directory with psp.cz export files")
(options, args) = parser.parse_args()
if not options.data_dir:
parser.error("Parameter --dir must be specified!")
PspCzImport(options.data_dir).all()
if __name__ == '__main__':
main()