From 39c8053bf483bf16c31330736ee524a97ef6964b Mon Sep 17 00:00:00 2001 From: Scott Adams Date: Fri, 12 Jan 2018 17:08:00 +0100 Subject: [PATCH] Fixes #189 Fixes logic so that all "filenames" are downloaded. --- cities/management/commands/cities.py | 59 ++++++++++++++-------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/cities/management/commands/cities.py b/cities/management/commands/cities.py index a71b3598..d50a4a54 100644 --- a/cities/management/commands/cities.py +++ b/cities/management/commands/cities.py @@ -172,37 +172,38 @@ def call_hook(self, hook, *args, **kwargs): return False return True - def download(self, filekey, key_index=None): - if key_index is None: - filename = settings.files[filekey]['filename'] + def download(self, filekey): + if 'filename' in settings.files[filekey]: + filenames = [settings.files[filekey]['filename']] else: - filename = settings.files[filekey]['filenames'][key_index] + filenames = settings.files[filekey]['filenames'] - web_file = None - urls = [e.format(filename=filename) for e in settings.files[filekey]['urls']] - for url in urls: - try: - web_file = urlopen(url) - if 'html' in web_file.headers['Content-Type']: - # TODO: Make this a subclass - raise Exception("Content type of downloaded file was {}".format(web_file.headers['Content-Type'])) - self.logger.debug("Downloaded: {}".format(url)) - break - except Exception: - web_file = None - continue - else: - self.logger.error("Web file not found: %s. Tried URLs:\n%s", filename, '\n'.join(urls)) - - if web_file is not None: - self.logger.debug("Saving: {}/{}".format(self.data_dir, filename)) - if not os.path.exists(self.data_dir): - os.makedirs(self.data_dir) - file = io.open(os.path.join(self.data_dir, filename), 'wb') - file.write(web_file.read()) - file.close() - elif not os.path.exists(os.path.join(self.data_dir, filename)): - raise Exception("File not found and download failed: {} [{}]".format(filename, url)) + for filename in filenames: + web_file = None + urls = [e.format(filename=filename) for e in settings.files[filekey]['urls']] + for url in urls: + try: + web_file = urlopen(url) + if 'html' in web_file.headers['Content-Type']: + # TODO: Make this a subclass + raise Exception("Content type of downloaded file was {}".format(web_file.headers['Content-Type'])) + self.logger.debug("Downloaded: {}".format(url)) + break + except Exception: + web_file = None + continue + else: + self.logger.error("Web file not found: %s. Tried URLs:\n%s", filename, '\n'.join(urls)) + + if web_file is not None: + self.logger.debug("Saving: {}/{}".format(self.data_dir, filename)) + if not os.path.exists(self.data_dir): + os.makedirs(self.data_dir) + file = io.open(os.path.join(self.data_dir, filename), 'wb') + file.write(web_file.read()) + file.close() + elif not os.path.exists(os.path.join(self.data_dir, filename)): + raise Exception("File not found and download failed: {} [{}]".format(filename, url)) def get_data(self, filekey): if 'filename' in settings.files[filekey]: