diff --git a/commonsdownloader/commonsdownloader.py b/commonsdownloader/commonsdownloader.py index 766dff4..581085f 100644 --- a/commonsdownloader/commonsdownloader.py +++ b/commonsdownloader/commonsdownloader.py @@ -6,7 +6,7 @@ import os import logging import argparse -from thumbnaildownload import download_file +from thumbnaildownload import download_file, DownloadException from itertools import izip_longest @@ -76,8 +76,11 @@ def download_files_if_not_in_cache(files_iterator, output_path): if is_file_in_cache(file_name, width, local_cache): logging.info('Skipping file %s' % file_name) continue - download_file(file_name, output_path, width=width) - write_file_to_cache(file_name, width, cache_fh) + try: + download_file(file_name, output_path, width=width) + write_file_to_cache(file_name, width, cache_fh) + except DownloadException, e: + logging.error("Could not download %s: %s", file_name, e.message) class Folder(argparse.Action): diff --git a/commonsdownloader/thumbnaildownload.py b/commonsdownloader/thumbnaildownload.py index aed2b9b..0e921b4 100644 --- a/commonsdownloader/thumbnaildownload.py +++ b/commonsdownloader/thumbnaildownload.py @@ -119,7 +119,13 @@ def download_file(image_name, output_path, width=DEFAULT_WIDTH): logging.info("Requested width is bigger than source - downloading full size") contents, output_file_name = get_full_size_file(image_name) output_file_path = os.path.join(output_path, output_file_name) - with open(output_file_path, 'wb') as f: - logging.debug("Writing as %s" % output_file_path) - f.write(contents) - return output_file_path + try: + with open(output_file_path, 'wb') as f: + logging.debug("Writing as %s" % output_file_path) + f.write(contents) + return output_file_path + except Exception, e: + logging.critical(e.message) + msg = 'An unexpected error occured when downloading %s to %s: %s' % \ + (image_name, output_path, e.message) + raise DownloadException(msg)