Skip to content
This repository has been archived by the owner on Jan 14, 2019. It is now read-only.

Commit

Permalink
Add in the ability to download the postal code data as well as the ot…
Browse files Browse the repository at this point in the history
…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
Andrew Smith committed Nov 15, 2011
1 parent fa34e44 commit c70f120
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions geonames/management/commands/compress_geonames.py
Expand Up @@ -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):

Expand Down
14 changes: 14 additions & 0 deletions geonames/management/commands/download_geonames.py
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand All @@ -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.