Ignore banned subreddits

This commit is contained in:
blankie 2023-02-04 17:24:10 +07:00
parent 82d662363c
commit 693ca24106
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 11 additions and 3 deletions

View File

@ -14,6 +14,7 @@ from itertools import zip_longest
from urllib.parse import urlparse, urlunparse, urljoin from urllib.parse import urlparse, urlunparse, urljoin
import yaml import yaml
import asyncpraw import asyncpraw
from asyncprawcore.exceptions import NotFound
import aiohttp import aiohttp
import aiocron import aiocron
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -157,9 +158,8 @@ async def main():
timeout = chat_data.get('timeout') timeout = chat_data.get('timeout')
give_ups = set() give_ups = set()
async def _get_submission(unique_id): async def _get_submission(unique_id, subreddit):
while unique_id not in give_ups: while unique_id not in give_ups:
subreddit = await reddit.subreddit(random.choice(subreddits))
random_post = await subreddit.random() random_post = await subreddit.random()
cpid = cpp = None cpid = cpp = None
if random_post is None: if random_post is None:
@ -224,14 +224,22 @@ async def main():
@aiocron.crontab(cron_duration) @aiocron.crontab(cron_duration)
async def start_post(): async def start_post():
ignore_subreddits = set()
while True: while True:
unique_id = time.time() 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: 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: except asyncio.TimeoutError:
give_ups.add(unique_id) give_ups.add(unique_id)
logging.error('%s timed out', chat) logging.error('%s timed out', chat)
return return
except NotFound:
ignore_subreddits.add(subreddit.display_name)
logging.error('%s returned 404 (banned?)', subreddit.display_name)
except Exception: except Exception:
give_ups.add(unique_id) give_ups.add(unique_id)
logging.exception(chat) logging.exception(chat)