Skip to content
Browse files

Add in the ability to download the postal code data as well as the ot…

…her files. Only the main 'allCountries' file is downloaded since it's only 8.8MB which should be small enough as it is. Also include an option to disable the postalcodes.
  • Loading branch information...
1 parent fa34e44 commit c70f120ae197abb5448f8cf1eebc1d387e083f01 @EspadaV8 EspadaV8 committed
View
1 geonames/management/commands/compress_geonames.py
@@ -5,6 +5,7 @@
from geonames import models
GEONAMES_DATA = os.path.abspath(os.path.join(os.path.dirname(models.__file__), 'data'))
+GEONAMES_DATA_PC = os.path.join(GEONAMES_DATA, 'pc'))
class Command(NoArgsCommand):
View
14 geonames/management/commands/download_geonames.py
@@ -10,6 +10,7 @@
from compress_geonames import GEONAMES_DATA
GEONAMES_DUMPS_URL = 'http://download.geonames.org/export/dump/'
+GEONAMES_PC_DUMPS_URL = 'http://download.geonames.org/export/zip/'
def download(url, filepath=False):
"""
@@ -68,6 +69,8 @@ class Command(NoArgsCommand):
help='Disable loading of the Geonames alternate names data.'),
make_option('--no-geonames', action='store_true', dest='no_geonames', default=False,
help='Disable loading of the Geonames data.'),
+ make_option('--no-postalcodes', action='store_true', dest='no_postalcodes', default=False,
+ help='Disable loading of the postal codes data.'),
)
def handle_noargs(self, **options):
@@ -87,5 +90,16 @@ def handle_noargs(self, **options):
print '\nStart download "%s" file' % file
download(urlparse.urljoin(GEONAMES_DUMPS_URL, file), os.path.join(GEONAMES_DATA, file))
+ if options['no_postalcodes'] == False:
+ print '\nLooking for postalcode data to download'
+ postalcodeResponse = urllib2.urlopen(urllib2.Request(url=GEONAMES_PC_DUMPS_URL))
+ postalcodeFiles = re.findall(r'\<a href="(.+\.(?:txt|zip))"\>', postalcodeResponse.read())
+ for file in postalcodeFiles:
+ if file != 'allCountries.zip':
+ continue
+
+ print '\nStart download "%s" file' % file
+ download(urlparse.urljoin(GEONAMES_PC_DUMPS_URL, file), os.path.join(GEONAMES_DATA_PC, file))
+
if options['time']:
print '\nCompleted in %s' % (datetime.datetime.now() - start_time)

0 comments on commit c70f120

Please sign in to comment.
Something went wrong with that request. Please try again.