From ac08a639bae77bbaac307d689090e26b60be6387 Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Thu, 4 Mar 2021 09:14:43 +1000 Subject: [PATCH] Tighten exception block --- bulkredditdownloader/downloader.py | 45 ++++++++++++++++-------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/bulkredditdownloader/downloader.py b/bulkredditdownloader/downloader.py index 56be776..c9149a8 100644 --- a/bulkredditdownloader/downloader.py +++ b/bulkredditdownloader/downloader.py @@ -194,29 +194,32 @@ class RedditDownloader: def _download_submission(self, submission: praw.models.Submission): if self.download_filter.check_url(submission.url): logger.debug('Attempting to download submission {}'.format(submission.id)) + try: downloader_class = DownloadFactory.pull_lever(submission.url) downloader = downloader_class(submission) - if self.args.no_download: - logger.info('Skipping download for submission {}'.format(submission.id)) - else: - content = downloader.find_resources(self.authenticator) - for res in content: - destination = self.file_name_formatter.format_path(res, self.download_directory) - if destination.exists(): - logger.debug('File already exists: {}'.format(destination)) - else: - if res.hash.hexdigest() not in self.master_hash_list and not self.args.no_dupes: - # TODO: consider making a hard link/symlink here - destination.parent.mkdir(parents=True, exist_ok=True) - with open(destination, 'wb') as file: - file.write(res.content) - logger.debug('Written file to {}'.format(destination)) - self.master_hash_list.append(res.hash.hexdigest()) - logger.debug('Hash added to master list: {}'.format(res.hash.hexdigest())) - else: - logger.debug(f'Resource from {res.url} downloaded elsewhere') - - logger.info('Downloaded submission {}'.format(submission.name)) except NotADownloadableLinkError as e: logger.error('Could not download submission {}: {}'.format(submission.name, e)) + return + + if self.args.no_download: + logger.info('Skipping download for submission {}'.format(submission.id)) + else: + content = downloader.find_resources(self.authenticator) + for res in content: + destination = self.file_name_formatter.format_path(res, self.download_directory) + if destination.exists(): + logger.debug('File already exists: {}'.format(destination)) + else: + if res.hash.hexdigest() not in self.master_hash_list and not self.args.no_dupes: + # TODO: consider making a hard link/symlink here + destination.parent.mkdir(parents=True, exist_ok=True) + with open(destination, 'wb') as file: + file.write(res.content) + logger.debug('Written file to {}'.format(destination)) + self.master_hash_list.append(res.hash.hexdigest()) + logger.debug('Hash added to master list: {}'.format(res.hash.hexdigest())) + else: + logger.debug(f'Resource from {res.url} downloaded elsewhere') + + logger.info('Downloaded submission {}'.format(submission.name))