From 14195157de9ef66a8605dff774be10b922401b13 Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Wed, 28 Apr 2021 12:43:11 +1000 Subject: [PATCH] Catch errors for banned or private subreddits --- bdfr/downloader.py | 14 ++++++++++++++ tests/test_downloader.py | 12 ++++++++++++ tests/test_integration.py | 2 ++ 3 files changed, 28 insertions(+) diff --git a/bdfr/downloader.py b/bdfr/downloader.py index 6b69f8c..9dbafc9 100644 --- a/bdfr/downloader.py +++ b/bdfr/downloader.py @@ -239,6 +239,11 @@ class RedditDownloader: for reddit in self._split_args_input(self.args.subreddit): try: reddit = self.reddit_instance.subreddit(reddit) + try: + self._check_subreddit_status(reddit) + except errors.BulkDownloaderException as e: + logger.error(e) + continue if self.args.search: out.append(reddit.search( self.args.search, @@ -460,3 +465,12 @@ class RedditDownloader: for line in file: out.append(line.strip()) return set(out) + + @staticmethod + def _check_subreddit_status(subreddit: praw.models.Subreddit): + try: + assert subreddit.id + except prawcore.NotFound: + raise errors.BulkDownloaderException(f'Source {subreddit.display_name} does not exist or cannot be found') + except prawcore.Forbidden: + raise errors.BulkDownloaderException(f'Source {subreddit.display_name} is private and cannot be scraped') diff --git a/tests/test_downloader.py b/tests/test_downloader.py index 9ec13cf..ef06a77 100644 --- a/tests/test_downloader.py +++ b/tests/test_downloader.py @@ -505,3 +505,15 @@ def test_check_user_existence_banned( downloader_mock.reddit_instance = reddit_instance with pytest.raises(BulkDownloaderException, match='is banned'): RedditDownloader._check_user_existence(downloader_mock, test_redditor_name) + + +@pytest.mark.online +@pytest.mark.reddit +@pytest.mark.parametrize(('test_subreddit_name', 'expected_message'), ( + ('donaldtrump', 'cannot be found'), + ('submitters', 'private and cannot be scraped') +)) +def test_check_subreddit_status(test_subreddit_name: str, expected_message: str, reddit_instance: praw.Reddit): + test_subreddit = reddit_instance.subreddit(test_subreddit_name) + with pytest.raises(BulkDownloaderException, match=expected_message): + RedditDownloader._check_subreddit_status(test_subreddit) diff --git a/tests/test_integration.py b/tests/test_integration.py index 327acc4..003a465 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -255,6 +255,8 @@ def test_cli_archive_long(test_args: list[str], tmp_path: Path): ['--user', 'sdclhgsolgjeroij', '--submitted', '-L', 10], ['--user', 'me', '--upvoted', '-L', 10], ['--user', 'sdclhgsolgjeroij', '--upvoted', '-L', 10], + ['--subreddit', 'submitters', '-L', 10], # Private subreddit + ['--subreddit', 'donaldtrump', '-L', 10], # Banned subreddit )) def test_cli_download_soft_fail(test_args: list[str], tmp_path: Path): runner = CliRunner()