Skip to content

Commit

Permalink
Fix issues #13 and #14 crashing due to failed connections
Browse files Browse the repository at this point in the history
  • Loading branch information
MalloyDelacroix committed Mar 16, 2018
1 parent 0358164 commit 8687d91
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
43 changes: 27 additions & 16 deletions DownloaderForReddit/Core/Content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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"""
Expand Down
13 changes: 13 additions & 0 deletions DownloaderForReddit/Extractors/Extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 8687d91

Please sign in to comment.