From e0e5db85e3eae3fbb74a725ab2bb3671606ede4e Mon Sep 17 00:00:00 2001 From: Thomas <71355143+thomas694@users.noreply.github.com> Date: Sat, 18 Feb 2023 23:51:15 +0100 Subject: [PATCH] Fix: Crashes on praw.exceptions --- bdfr/archiver.py | 20 ++++++++++++-------- bdfr/cloner.py | 5 +++-- bdfr/connector.py | 4 ++-- bdfr/downloader.py | 4 ++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/bdfr/archiver.py b/bdfr/archiver.py index dd4b06e..005e2fc 100644 --- a/bdfr/archiver.py +++ b/bdfr/archiver.py @@ -9,6 +9,7 @@ from time import sleep from typing import Union import dict2xml +import praw.exceptions import praw.models import prawcore import yaml @@ -46,9 +47,9 @@ class Archiver(RedditConnector): continue logger.debug(f"Attempting to archive submission {submission.id}") self.write_entry(submission) - except prawcore.PrawcoreException as e: + except (prawcore.PrawcoreException, praw.exceptions.PRAWException) as e: logger.error(f"Submission {submission.id} failed to be archived due to a PRAW exception: {e}") - except prawcore.PrawcoreException as e: + except (prawcore.PrawcoreException, praw.exceptions.PRAWException) as e: logger.error(f"The submission after {submission.id} failed to download due to a PRAW exception: {e}") logger.debug("Waiting 60 seconds to continue") sleep(60) @@ -56,12 +57,15 @@ class Archiver(RedditConnector): def get_submissions_from_link(self) -> list[list[praw.models.Submission]]: supplied_submissions = [] for sub_id in self.args.link: - if len(sub_id) == 6: - supplied_submissions.append(self.reddit_instance.submission(id=sub_id)) - elif re.match(r"^\w{7}$", sub_id): - supplied_submissions.append(self.reddit_instance.comment(id=sub_id)) - else: - supplied_submissions.append(self.reddit_instance.submission(url=sub_id)) + try: + if len(sub_id) == 6: + supplied_submissions.append(self.reddit_instance.submission(id=sub_id)) + elif re.match(r"^\w{7}$", sub_id): + supplied_submissions.append(self.reddit_instance.comment(id=sub_id)) + else: + supplied_submissions.append(self.reddit_instance.submission(url=sub_id)) + except (prawcore.PrawcoreException, praw.exceptions.PRAWException) as e: + logger.error(f"Error getting submission {sub_id} from link: {e}") return [supplied_submissions] def get_user_data(self) -> list[Iterator]: diff --git a/bdfr/cloner.py b/bdfr/cloner.py index 758e5c8..5e4c141 100644 --- a/bdfr/cloner.py +++ b/bdfr/cloner.py @@ -5,6 +5,7 @@ from collections.abc import Iterable from time import sleep import prawcore +import praw.exceptions from bdfr.archiver import Archiver from bdfr.configuration import Configuration @@ -24,9 +25,9 @@ class RedditCloner(RedditDownloader, Archiver): try: self._download_submission(submission) self.write_entry(submission) - except prawcore.PrawcoreException as e: + except (prawcore.PrawcoreException, praw.exceptions.PRAWException) as e: logger.error(f"Submission {submission.id} failed to be cloned due to a PRAW exception: {e}") - except prawcore.PrawcoreException as e: + except (prawcore.PrawcoreException, praw.exceptions.PRAWException) as e: logger.error(f"The submission after {submission.id} failed to download due to a PRAW exception: {e}") logger.debug("Waiting 60 seconds to continue") sleep(60) diff --git a/bdfr/connector.py b/bdfr/connector.py index 2a87c18..61bb002 100644 --- a/bdfr/connector.py +++ b/bdfr/connector.py @@ -290,7 +290,7 @@ class RedditConnector(metaclass=ABCMeta): else: out.append(self.create_filtered_listing_generator(reddit)) logger.debug(f"Added submissions from subreddit {reddit}") - except (errors.BulkDownloaderException, praw.exceptions.PRAWException) as e: + except (errors.BulkDownloaderException, prawcore.PrawcoreException, praw.exceptions.PRAWException) as e: logger.error(f"Failed to get submissions for subreddit {reddit}: {e}") return out @@ -382,7 +382,7 @@ class RedditConnector(metaclass=ABCMeta): if self.args.saved: logger.debug(f"Retrieving saved posts of user {user}") generators.append(self.reddit_instance.redditor(user).saved(limit=self.args.limit)) - except prawcore.PrawcoreException as e: + except (prawcore.PrawcoreException, praw.exceptions.PRAWException) as e: logger.error(f"User {user} failed to be retrieved due to a PRAW exception: {e}") logger.debug("Waiting 60 seconds to continue") sleep(60) diff --git a/bdfr/downloader.py b/bdfr/downloader.py index 70068d2..19eb18a 100644 --- a/bdfr/downloader.py +++ b/bdfr/downloader.py @@ -47,9 +47,9 @@ class RedditDownloader(RedditConnector): for submission in generator: try: self._download_submission(submission) - except prawcore.PrawcoreException as e: + except (prawcore.PrawcoreException, praw.exceptions.PRAWException) as e: logger.error(f"Submission {submission.id} failed to download due to a PRAW exception: {e}") - except prawcore.PrawcoreException as e: + except (prawcore.PrawcoreException, praw.exceptions.PRAWException) as e: logger.error(f"The submission after {submission.id} failed to download due to a PRAW exception: {e}") logger.debug("Waiting 60 seconds to continue") sleep(60)