diff --git a/redditbot.py b/redditbot.py index 7cb90d6..c4ffe0e 100644 --- a/redditbot.py +++ b/redditbot.py @@ -12,7 +12,7 @@ import traceback from itertools import zip_longest from urllib.parse import urlparse, urlunparse import yaml -import praw +import asyncpraw import aiohttp import aiocron from bs4 import BeautifulSoup @@ -54,7 +54,7 @@ async def main(): client = await TelegramClient('redditbot', tg_api_id, tg_api_hash).start(bot_token=bot_token) client.parse_mode = 'html' session = aiohttp.ClientSession() - reddit = praw.Reddit(client_id=reddit_client_id, client_secret=reddit_client_secret, user_agent='linux:redditbot:v1.0.0 (by /u/the_blank_x)') + reddit = asyncpraw.Reddit(client_id=reddit_client_id, client_secret=reddit_client_secret, user_agent='linux:redditbot:v1.0.0 (by /u/the_blank_x)') try: if storage_chat and storage_msg_id: @@ -95,13 +95,13 @@ async def main(): timeout = chat_data.get('timeout') give_ups = set() - def _get_submission_blocc(unique_id): + async def _get_submission(unique_id): while unique_id not in give_ups: - subreddit = reddit.subreddit(random.choice(subreddits)) - random_post = subreddit.random() + subreddit = await reddit.subreddit(random.choice(subreddits)) + random_post = await subreddit.random() cpid = cpp = None if random_post is None: - for submission in subreddit.hot(): + async for submission in subreddit.hot(): if unique_id in give_ups: return cpid = getattr(submission, 'crosspost_parent', None) @@ -114,7 +114,7 @@ async def main(): nsfw = submission.over_18 spoilers = submission.spoiler if cpid: - cpp = reddit.submission(cpid) + cpp = await reddit.submission(cpid) if not allow_selfposts: is_self = cpp.is_self if not (nsfw and allow_nsfw): @@ -139,7 +139,7 @@ async def main(): nsfw = random_post.over_18 spoilers = random_post.spoiler if cpid and not cpp: - cpp = reddit.submission(cpid) + cpp = await reddit.submission(cpid) if cpid: if not allow_selfposts: is_self = cpp.is_self @@ -162,7 +162,7 @@ async def main(): while True: unique_id = time.time() try: - random_post, cpp = await asyncio.wait_for(client.loop.run_in_executor(None, _get_submission_blocc, unique_id), timeout) + random_post, cpp = await asyncio.wait_for(_get_submission(unique_id), timeout) except asyncio.TimeoutError: give_ups.add(unique_id) logging.error('%s timed out', chat) @@ -243,7 +243,7 @@ async def main(): spoilers = random_post.spoiler cpid = getattr(random_post, 'crosspost_parent', None) if cpid and getattr(random_post, 'crosspost_parent_list', None) and not cpp: - cpp = reddit.submission(cpid[3:]) + cpp = await reddit.submission(cpid[3:]) if cpp: random_post = cpp if snw and not nsfw: @@ -436,7 +436,7 @@ async def main(): @register(r'/test (\S+)(?: ([ns]+))?') async def test_post(e): await e.reply('ok') - post = reddit.submission(e.pattern_match.group(1)) + post = await reddit.submission(e.pattern_match.group(1)) flags = e.pattern_match.group(2) or '' snw = 'n' in flags ssw = 's' in flags diff --git a/requirements.txt b/requirements.txt index e6e0aab..349fd31 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,4 @@ aiohttp>=3.7.1 hachoir PyYAML Pillow -praw +asyncpraw