From 693ca24106ec98811ae63646311cac357dd9ab9a Mon Sep 17 00:00:00 2001 From: blankie Date: Sat, 4 Feb 2023 17:24:10 +0700 Subject: [PATCH] Ignore banned subreddits --- redditbot.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/redditbot.py b/redditbot.py index 48c90a9..a7c595c 100644 --- a/redditbot.py +++ b/redditbot.py @@ -14,6 +14,7 @@ from itertools import zip_longest from urllib.parse import urlparse, urlunparse, urljoin import yaml import asyncpraw +from asyncprawcore.exceptions import NotFound import aiohttp import aiocron from bs4 import BeautifulSoup @@ -157,9 +158,8 @@ async def main(): timeout = chat_data.get('timeout') give_ups = set() - async def _get_submission(unique_id): + async def _get_submission(unique_id, subreddit): while unique_id not in give_ups: - subreddit = await reddit.subreddit(random.choice(subreddits)) random_post = await subreddit.random() cpid = cpp = None if random_post is None: @@ -224,14 +224,22 @@ async def main(): @aiocron.crontab(cron_duration) async def start_post(): + ignore_subreddits = set() while True: unique_id = time.time() + unignored_subreddits = [i for i in subreddits if i not in ignore_subreddits] + if not unignored_subreddits: + return + subreddit = await reddit.subreddit(random.choice(unignored_subreddits)) try: - result = await asyncio.wait_for(_get_submission(unique_id), timeout) + result = await asyncio.wait_for(_get_submission(unique_id, subreddit), timeout) except asyncio.TimeoutError: give_ups.add(unique_id) logging.error('%s timed out', chat) return + except NotFound: + ignore_subreddits.add(subreddit.display_name) + logging.error('%s returned 404 (banned?)', subreddit.display_name) except Exception: give_ups.add(unique_id) logging.exception(chat)