From 8687d91f21c016cb389d09ad97cad7f7bb40e752 Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 16 Mar 2018 07:42:01 -0500 Subject: [PATCH] Fix issues #13 and #14 crashing due to failed connections --- DownloaderForReddit/Core/Content.py | 43 +++++++++++++-------- DownloaderForReddit/Extractors/Extractor.py | 13 +++++++ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/DownloaderForReddit/Core/Content.py b/DownloaderForReddit/Core/Content.py index 5c487291..81870478 100644 --- a/DownloaderForReddit/Core/Content.py +++ b/DownloaderForReddit/Core/Content.py @@ -104,26 +104,22 @@ def __init__(self, url, user, post_title, subreddit, submission_id, number_in_se def run(self): self.check_save_path_subreddit() - response = requests.get(self.url, stream=True) - if response.status_code == 200: - try: + try: + response = requests.get(self.url, stream=True) + if response.status_code == 200: with open(self.filename, 'wb') as file: for chunk in response.iter_content(1024): file.write(chunk) self.set_file_modified_date() - except: - self.handle_exception() - self.queue.put('Saved %s' % self.filename) - self.downloaded = True - return None - else: - self.handle_unsuccessful_response(response.status_code) - - def handle_exception(self): - """Handles logging and output in case of a failed save due to a general exception.""" - self.logger.error('Failed to save content: Exception while saving file', - extra={'save_path': self.filename}, exc_info=True) - self.queue.put('Failed to save content: %s' % self.filename) + self.queue.put('Saved: %s' % self.filename) + self.downloaded = True + return None + else: + self.handle_unsuccessful_response(response.status_code) + except ConnectionError: + self.handle_connection_error() + except: + self.handle_exception() def handle_unsuccessful_response(self, status_code): """Handles logging and output in case of a failed response from the server.""" @@ -133,6 +129,21 @@ def handle_unsuccessful_response(self, status_code): self.queue.put('Failed Download: File %s%s posted by %s failed to download...try link to download ' 'manually: %s\n' % (self.submission_id, self.number_in_seq, self.user, self.url)) + def handle_connection_error(self): + """Handles logging and output in case of a failed connection attempt to the server""" + self.logger.error('Failed to establish a connection', + extra={'url': self.url, 'user': self.user, 'submission_id': self.submission_id, + 'number_in_seq': self.number_in_seq, 'extension': self.file_ext, + 'created': self.date_created}, exc_info=True) + self.queue.put('Failed Download: Failed to establish a connection to url: %s\n' + 'User: %s, Subreddit: %s, Title: %s' % (self.url, self.user, self.subreddit, self.post_title)) + + def handle_exception(self): + """Handles logging and output in case of a failed save due to a general exception.""" + self.logger.error('Failed to save content: Exception while saving file', + extra={'save_path': self.filename}, exc_info=True) + self.queue.put('Failed to save content: %s' % self.filename) + @staticmethod def clean_filename(name): """Ensures each file name does not contain forbidden characters and is within the character limit""" diff --git a/DownloaderForReddit/Extractors/Extractor.py b/DownloaderForReddit/Extractors/Extractor.py index 9028d7f8..f5c6120a 100644 --- a/DownloaderForReddit/Extractors/Extractor.py +++ b/DownloaderForReddit/Extractors/Extractor.py @@ -68,6 +68,19 @@ def extract(self, post): (post.url, post.author, post.subreddit, post.title)) self.logger.error('Failed to find extractor for domain', extra={'url': post.url, 'reddit_object': self.reddit_object.json}) + except ConnectionError: + self.reddit_object.failed_extracts.append('Failed to establish a connection to domain: %s\nThis post has' + 'been saved and download will be attempted again next time' % + post.url) + self.logger.error('Failed to establish connection to domain', + extra={'url': post.url, 'reddit_object': self.reddit_object.json}, exc_info=True) + except: + self.reddit_object.failed_extracts.append('Failed to extract content from post\n' + 'Url: %s, User: %s Subreddit: %s, Title: %s' % + (post.url, post.author, post.subreddit, post.title)) + self.logger.error('Failed to extract content: Unknown error', + extra={'url': post.url, 'reddit_object': self.reddit_object.json}, exc_info=True) + def get_subreddit(self, post): """