mirror of
https://github.com/Nick80835/microbot
synced 2025-08-31 14:38:04 +00:00
rework reddit a lot and improve error handling
This commit is contained in:
@@ -4,6 +4,7 @@ import io
|
|||||||
from random import choice, shuffle
|
from random import choice, shuffle
|
||||||
|
|
||||||
import praw
|
import praw
|
||||||
|
from prawcore import exceptions as redex
|
||||||
|
|
||||||
from ubot.micro_bot import micro_bot
|
from ubot.micro_bot import micro_bot
|
||||||
|
|
||||||
@@ -19,22 +20,18 @@ VALID_ENDS = (".mp4", ".jpg", ".jpeg", ".png", ".gif")
|
|||||||
async def imagefetcherfallback(sub):
|
async def imagefetcherfallback(sub):
|
||||||
hot = REDDIT.subreddit(sub).hot()
|
hot = REDDIT.subreddit(sub).hot()
|
||||||
hot_list = list(hot.__iter__())
|
hot_list = list(hot.__iter__())
|
||||||
|
shuffle(hot_list)
|
||||||
|
|
||||||
for _ in range(10):
|
for post in hot_list:
|
||||||
post = choice(hot_list)
|
if post.url and post.url.endswith(VALID_ENDS):
|
||||||
|
return post
|
||||||
|
|
||||||
if post.url:
|
return None
|
||||||
if post.url.endswith(VALID_ENDS):
|
|
||||||
return post.url, post.title
|
|
||||||
|
|
||||||
return None, None
|
|
||||||
|
|
||||||
|
|
||||||
async def titlefetcherfallback(sub):
|
async def titlefetcherfallback(sub):
|
||||||
hot = REDDIT.subreddit(sub).hot()
|
hot = REDDIT.subreddit(sub).hot()
|
||||||
hot_list = list(hot.__iter__())
|
return choice(list(hot.__iter__()))
|
||||||
|
|
||||||
return choice(hot_list).title
|
|
||||||
|
|
||||||
|
|
||||||
async def bodyfetcherfallback(sub):
|
async def bodyfetcherfallback(sub):
|
||||||
@@ -42,22 +39,28 @@ async def bodyfetcherfallback(sub):
|
|||||||
hot_list = list(hot.__iter__())
|
hot_list = list(hot.__iter__())
|
||||||
shuffle(hot_list)
|
shuffle(hot_list)
|
||||||
|
|
||||||
for i in hot_list:
|
for post in hot_list:
|
||||||
if i.selftext and not i.permalink in i.url:
|
if post.selftext and not post.permalink in post.url:
|
||||||
return i.selftext, i.title
|
return post
|
||||||
|
|
||||||
return None, None
|
return None
|
||||||
|
|
||||||
|
|
||||||
async def imagefetcher(event, sub):
|
async def imagefetcher(event, sub):
|
||||||
image_url = False
|
image_url = False
|
||||||
|
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
post = REDDIT.subreddit(sub).random()
|
try:
|
||||||
|
post = REDDIT.subreddit(sub).random() or await imagefetcherfallback(sub)
|
||||||
if not post:
|
post.title
|
||||||
image_url, title = await imagefetcherfallback(sub)
|
except redex.Forbidden:
|
||||||
break
|
await event.reply(f"**r/{sub}**` is private!`")
|
||||||
|
return
|
||||||
|
except redex.NotFound:
|
||||||
|
await event.reply(f"**r/{sub}**` doesn't exist!`")
|
||||||
|
return
|
||||||
|
except AttributeError:
|
||||||
|
continue
|
||||||
|
|
||||||
if post.url:
|
if post.url:
|
||||||
if post.url.endswith(VALID_ENDS):
|
if post.url.endswith(VALID_ENDS):
|
||||||
@@ -80,24 +83,36 @@ async def imagefetcher(event, sub):
|
|||||||
|
|
||||||
|
|
||||||
async def titlefetcher(event, sub):
|
async def titlefetcher(event, sub):
|
||||||
post = REDDIT.subreddit(sub).random()
|
try:
|
||||||
|
post = REDDIT.subreddit(sub).random() or await titlefetcherfallback(sub)
|
||||||
|
post.title
|
||||||
|
except redex.Forbidden:
|
||||||
|
await event.reply(f"**r/{sub}**` is private!`")
|
||||||
|
return
|
||||||
|
except redex.NotFound:
|
||||||
|
await event.reply(f"**r/{sub}**` doesn't exist!`")
|
||||||
|
return
|
||||||
|
|
||||||
if not post:
|
await event.reply(post.title)
|
||||||
title = await titlefetcherfallback(sub)
|
|
||||||
else:
|
|
||||||
title = post.title
|
|
||||||
|
|
||||||
await event.reply(title)
|
|
||||||
|
|
||||||
|
|
||||||
async def bodyfetcher(event, sub):
|
async def bodyfetcher(event, sub):
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
post = REDDIT.subreddit(sub).random()
|
try:
|
||||||
|
post = REDDIT.subreddit(sub).random() or await bodyfetcherfallback(sub)
|
||||||
|
post.title
|
||||||
|
except redex.Forbidden:
|
||||||
|
await event.reply(f"**r/{sub}**` is private!`")
|
||||||
|
return
|
||||||
|
except redex.NotFound:
|
||||||
|
await event.reply(f"**r/{sub}**` doesn't exist!`")
|
||||||
|
return
|
||||||
|
except AttributeError:
|
||||||
|
continue
|
||||||
|
|
||||||
body = None
|
body = None
|
||||||
|
|
||||||
if not post:
|
if post.selftext and not post.permalink is post.url:
|
||||||
body, title = await bodyfetcherfallback(sub)
|
|
||||||
elif post.selftext and not post.permalink is post.url:
|
|
||||||
body = post.selftext
|
body = post.selftext
|
||||||
title = post.title
|
title = post.title
|
||||||
|
|
||||||
@@ -152,7 +167,12 @@ async def coaxedintoasnafu(event):
|
|||||||
|
|
||||||
@ldr.add("aita")
|
@ldr.add("aita")
|
||||||
async def amitheasshole(event):
|
async def amitheasshole(event):
|
||||||
await titlefetcher(event, "AmITheAsshole")
|
await bodyfetcher(event, "AmITheAsshole")
|
||||||
|
|
||||||
|
|
||||||
|
@ldr.add("tifu")
|
||||||
|
async def todayifuckedup(event):
|
||||||
|
await bodyfetcher(event, "TIFU")
|
||||||
|
|
||||||
|
|
||||||
@ldr.add("jon(x|)")
|
@ldr.add("jon(x|)")
|
||||||
|
Reference in New Issue
Block a user