Use asyncpraw

hope it works
This commit is contained in:
blank X 2021-04-04 13:10:56 +07:00
parent 7c5cac9692
commit e1fe1bba28
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
2 changed files with 12 additions and 12 deletions

View File

@ -12,7 +12,7 @@ import traceback
from itertools import zip_longest from itertools import zip_longest
from urllib.parse import urlparse, urlunparse from urllib.parse import urlparse, urlunparse
import yaml import yaml
import praw import asyncpraw
import aiohttp import aiohttp
import aiocron import aiocron
from bs4 import BeautifulSoup 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 = await TelegramClient('redditbot', tg_api_id, tg_api_hash).start(bot_token=bot_token)
client.parse_mode = 'html' client.parse_mode = 'html'
session = aiohttp.ClientSession() 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: try:
if storage_chat and storage_msg_id: if storage_chat and storage_msg_id:
@ -95,13 +95,13 @@ async def main():
timeout = chat_data.get('timeout') timeout = chat_data.get('timeout')
give_ups = set() give_ups = set()
def _get_submission_blocc(unique_id): async def _get_submission(unique_id):
while unique_id not in give_ups: while unique_id not in give_ups:
subreddit = reddit.subreddit(random.choice(subreddits)) subreddit = await reddit.subreddit(random.choice(subreddits))
random_post = subreddit.random() random_post = await subreddit.random()
cpid = cpp = None cpid = cpp = None
if random_post is None: if random_post is None:
for submission in subreddit.hot(): async for submission in subreddit.hot():
if unique_id in give_ups: if unique_id in give_ups:
return return
cpid = getattr(submission, 'crosspost_parent', None) cpid = getattr(submission, 'crosspost_parent', None)
@ -114,7 +114,7 @@ async def main():
nsfw = submission.over_18 nsfw = submission.over_18
spoilers = submission.spoiler spoilers = submission.spoiler
if cpid: if cpid:
cpp = reddit.submission(cpid) cpp = await reddit.submission(cpid)
if not allow_selfposts: if not allow_selfposts:
is_self = cpp.is_self is_self = cpp.is_self
if not (nsfw and allow_nsfw): if not (nsfw and allow_nsfw):
@ -139,7 +139,7 @@ async def main():
nsfw = random_post.over_18 nsfw = random_post.over_18
spoilers = random_post.spoiler spoilers = random_post.spoiler
if cpid and not cpp: if cpid and not cpp:
cpp = reddit.submission(cpid) cpp = await reddit.submission(cpid)
if cpid: if cpid:
if not allow_selfposts: if not allow_selfposts:
is_self = cpp.is_self is_self = cpp.is_self
@ -162,7 +162,7 @@ async def main():
while True: while True:
unique_id = time.time() unique_id = time.time()
try: 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: 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)
@ -243,7 +243,7 @@ async def main():
spoilers = random_post.spoiler spoilers = random_post.spoiler
cpid = getattr(random_post, 'crosspost_parent', None) cpid = getattr(random_post, 'crosspost_parent', None)
if cpid and getattr(random_post, 'crosspost_parent_list', None) and not cpp: 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: if cpp:
random_post = cpp random_post = cpp
if snw and not nsfw: if snw and not nsfw:
@ -436,7 +436,7 @@ async def main():
@register(r'/test (\S+)(?: ([ns]+))?') @register(r'/test (\S+)(?: ([ns]+))?')
async def test_post(e): async def test_post(e):
await e.reply('ok') 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 '' flags = e.pattern_match.group(2) or ''
snw = 'n' in flags snw = 'n' in flags
ssw = 's' in flags ssw = 's' in flags

View File

@ -5,4 +5,4 @@ aiohttp>=3.7.1
hachoir hachoir
PyYAML PyYAML
Pillow Pillow
praw asyncpraw