2
0
mirror of https://github.com/Nick80835/microbot synced 2025-09-01 15:05:48 +00:00

improve reddit performance

This commit is contained in:
Nick80835
2020-07-17 22:28:03 -04:00
parent 7c84f06a26
commit 381a1315c1

View File

@@ -9,29 +9,25 @@ REDDIT = asyncpraw.Reddit(client_id='-fmzwojFG6JkGg',
client_secret=None, client_secret=None,
user_agent='TG_Userbot') user_agent='TG_Userbot')
VALID_ENDS = (".mp4", ".jpg", ".jpeg", ".png", ".gif") VALID_ENDS = (".mp4", ".jpg", ".jpeg", "jpe", ".png", ".gif")
async def imagefetcherfallback(sub): async def imagefetcherfallback(subreddit):
random_rising = await (await REDDIT.subreddit(sub)).random_rising(limit=10) async for post in subreddit.random_rising(limit=10):
for post in random_rising.__iter__():
if post.url and post.url.endswith(VALID_ENDS): if post.url and post.url.endswith(VALID_ENDS):
return post return post
return None return None
async def titlefetcherfallback(sub): async def titlefetcherfallback(subreddit):
random_rising = await (await REDDIT.subreddit(sub)).random_rising(limit=1) async for post in subreddit.random_rising(limit=1):
return list(random_rising.__iter__())[0] return post
async def bodyfetcherfallback(sub): async def bodyfetcherfallback(subreddit):
random_rising = await (await REDDIT.subreddit(sub)).random_rising(limit=10) async for post in subreddit.random_rising(limit=10):
if post.selftext and not post.permalink is post.url:
for post in random_rising.__iter__():
if post.selftext and not post.permalink in post.url:
return post return post
return None return None
@@ -40,19 +36,22 @@ async def bodyfetcherfallback(sub):
async def imagefetcher(event, sub): async def imagefetcher(event, sub):
image_url = False image_url = False
for _ in range(10):
try: try:
post = await (await REDDIT.subreddit(sub)).random() or await imagefetcherfallback(sub) subreddit = await REDDIT.subreddit(sub)
post.title
if event.nsfw_disabled and post.over_18:
continue
except redex.Forbidden: except redex.Forbidden:
await event.reply(f"**r/{sub}** is private!") await event.reply(f"**r/{sub}** is private!")
return return
except (redex.NotFound, KeyError): except (redex.NotFound, KeyError):
await event.reply(f"**r/{sub}** doesn't exist!") await event.reply(f"**r/{sub}** doesn't exist!")
return return
for _ in range(10):
try:
post = await subreddit.random() or await imagefetcherfallback(subreddit)
post.title
if event.nsfw_disabled and post.over_18:
continue
except AttributeError: except AttributeError:
continue continue
@@ -73,8 +72,7 @@ async def imagefetcher(event, sub):
async def titlefetcher(event, sub): async def titlefetcher(event, sub):
try: try:
post = await (await REDDIT.subreddit(sub)).random() or await titlefetcherfallback(sub) subreddit = await REDDIT.subreddit(sub)
post.title
except redex.Forbidden: except redex.Forbidden:
await event.reply(f"**r/{sub}** is private!") await event.reply(f"**r/{sub}** is private!")
return return
@@ -82,20 +80,25 @@ async def titlefetcher(event, sub):
await event.reply(f"**r/{sub}** doesn't exist!") await event.reply(f"**r/{sub}** doesn't exist!")
return return
post = await subreddit.random() or await titlefetcherfallback(subreddit)
await event.reply(post.title) await event.reply(post.title)
async def bodyfetcher(event, sub): async def bodyfetcher(event, sub):
for _ in range(10):
try: try:
post = await (await REDDIT.subreddit(sub)).random() or await bodyfetcherfallback(sub) subreddit = await REDDIT.subreddit(sub)
post.title
except redex.Forbidden: except redex.Forbidden:
await event.reply(f"**r/{sub}** is private!") await event.reply(f"**r/{sub}** is private!")
return return
except (redex.NotFound, KeyError): except (redex.NotFound, KeyError):
await event.reply(f"**r/{sub}** doesn't exist!") await event.reply(f"**r/{sub}** doesn't exist!")
return return
for _ in range(10):
try:
post = await subreddit.random() or await bodyfetcherfallback(subreddit)
post.title
except AttributeError: except AttributeError:
continue continue