-
Notifications
You must be signed in to change notification settings - Fork 0
/
geodataDownloader.py
151 lines (116 loc) · 4.99 KB
/
geodataDownloader.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import urllib3
import io
from zipfile import ZipFile
import os
from os import listdir
import sys
class GeodataDownloader():
def __init__(self, code):
self.code = code
self.folder = './geodata/data/'
self.geoname = 'http://download.geonames.org/export/dump/'
def downloadMainFile(self):
self.msg('Downloading main file... [%s] ' % self.code)
http = urllib3.PoolManager()
# Download and unzip main file
folder = self.folder + 'main/'
filename = folder + '%s.zip' % self.code
if not os.path.exists(folder):
os.makedirs(folder)
if not os.path.exists(filename):
response = http.request('GET', self.geoname + '%s.zip' % self.code, preload_content=False)
with open(filename, 'wb') as f:
f.write(response.data)
# Extract contents of zip file
with ZipFile(filename, 'r') as zipObj:
zipObj.extractall(folder)
# self.stdout.write(self.style.SUCCESS('Country %s seeded successfully!' % code))
self.msg('[OK]', True)
def downloadAltFile(self):
self.msg('Downloading alternatenames file... [%s] ' % self.code)
http = urllib3.PoolManager()
# Download and unzip main file
folder = self.folder + 'alt/'
filename = folder + '%s.zip' % self.code
if not os.path.exists(folder):
os.makedirs(folder)
if not os.path.exists(filename):
response = http.request('GET', self.geoname + 'alternatenames/%s.zip' % self.code, preload_content=False)
with open(filename, 'wb') as f:
f.write(response.data)
# Extract contents of zip file
with ZipFile(filename, 'r') as zipObj:
zipObj.extractall(folder)
# self.stdout.write(self.style.SUCCESS('Country %s seeded successfully!' % code))
self.msg('[OK]', True)
def downloadAdmin1Codes(self):
file = 'admin1CodesASCII.txt'
self.msg('Downloading %s file... [%s] ' % (file, self.code))
http = urllib3.PoolManager()
# Download and unzip main file
folder = self.folder
filename = folder + file
if not os.path.exists(folder):
os.makedirs(folder)
if not os.path.exists(filename):
response = http.request('GET', self.geoname + file, preload_content=False)
with open(filename, 'wb') as f:
f.write(response.data)
# self.stdout.write(self.style.SUCCESS('Country %s seeded successfully!' % code))
self.msg('[OK]', True)
def downloadAdmin2Codes(self):
file = 'admin2Codes.txt'
self.msg('Downloading admin2Codes file... [%s] ' % self.code)
http = urllib3.PoolManager()
# Download and unzip main file
folder = self.folder
filename = folder + file
if not os.path.exists(folder):
os.makedirs(folder)
if not os.path.exists(filename):
response = http.request('GET', self.geoname + file, preload_content=False)
with open(filename, 'wb') as f:
f.write(response.data)
# self.stdout.write(self.style.SUCCESS('Country %s seeded successfully!' % code))
self.msg('[OK]', True)
def downloadTimeZones(self):
file = 'timeZones.txt'
self.msg('Downloading timeZones file... [%s] ' % self.code)
http = urllib3.PoolManager()
# Download and unzip main file
folder = self.folder
filename = folder + file
if not os.path.exists(folder):
os.makedirs(folder)
if not os.path.exists(filename):
response = http.request('GET', self.geoname + file, preload_content=False)
with open(filename, 'wb') as f:
f.write(response.data)
# self.stdout.write(self.style.SUCCESS('Country %s seeded successfully!' % code))
self.msg('[OK]', True)
def downloadFeatureCodes(self):
file = 'featureCodes_%s.txt' % self.code.lower()
self.msg('Downloading featureCodes file... [%s] ' % self.code)
http = urllib3.PoolManager()
# Download and unzip main file
folder = self.folder
filename = folder + file
if not os.path.exists(folder):
os.makedirs(folder)
if not os.path.exists(filename):
response = http.request('GET', self.geoname + file, preload_content=False)
with open(filename, 'wb') as f:
f.write(response.data)
# self.stdout.write(self.style.SUCCESS('Country %s seeded successfully!' % code))
self.msg('[OK]', True)
def msg(self, msg, n=None):
sys.stdout.write(msg)
if n:
sys.stdout.write("\n")
sys.stdout.flush()
def deleteTxt(self, fld=None):
self.msg('Clear cache... [%s] ' % self.code)
for file in listdir(self.folder + fld):
if file.endswith('.txt'):
os.remove(self.folder + fld + file)
self.msg('[OK]', True)