Ignore banned subreddits
This commit is contained in:
parent
82d662363c
commit
693ca24106
14
redditbot.py
14
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)
|
||||
|
|
Loading…
Reference in New Issue