2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-23 10:38:51 +00:00
microbot/ubot/modules/reddit.py

183 lines
4.5 KiB
Python
Raw Normal View History

2020-03-16 21:02:05 -04:00
# SPDX-License-Identifier: GPL-2.0-or-later
2023-10-16 09:46:31 -04:00
import io
2020-04-04 09:48:13 -04:00
from random import choice, shuffle
2023-10-16 09:46:31 -04:00
import praw
from prawcore import exceptions as redex
2023-10-16 09:46:31 -04:00
from ubot.micro_bot import micro_bot
ldr = micro_bot.loader
REDDIT = praw.Reddit(client_id='-fmzwojFG6JkGg',
client_secret=None,
user_agent='TG_Userbot')
VALID_ENDS = (".mp4", ".jpg", ".jpeg", ".png", ".gif")
async def imagefetcherfallback(sub):
hot = REDDIT.subreddit(sub).hot()
hot_list = list(hot.__iter__())
shuffle(hot_list)
2023-10-16 09:46:31 -04:00
for post in hot_list:
if post.url and post.url.endswith(VALID_ENDS):
return post
2023-10-16 09:46:31 -04:00
return None
2023-10-16 09:46:31 -04:00
async def titlefetcherfallback(sub):
hot = REDDIT.subreddit(sub).hot()
return choice(list(hot.__iter__()))
2023-10-16 09:46:31 -04:00
2020-04-04 09:48:13 -04:00
async def bodyfetcherfallback(sub):
hot = REDDIT.subreddit(sub).hot()
hot_list = list(hot.__iter__())
shuffle(hot_list)
for post in hot_list:
if post.selftext and not post.permalink in post.url:
return post
2020-04-04 09:48:13 -04:00
return None
2020-04-04 09:48:13 -04:00
2023-10-16 09:46:31 -04:00
async def imagefetcher(event, sub):
image_url = False
for _ in range(10):
try:
post = REDDIT.subreddit(sub).random() or await imagefetcherfallback(sub)
post.title
except redex.Forbidden:
await event.reply(f"**r/{sub}**` is private!`")
return
2020-05-03 16:35:02 -04:00
except (redex.NotFound, KeyError):
await event.reply(f"**r/{sub}**` doesn't exist!`")
return
except AttributeError:
continue
2023-10-16 09:46:31 -04:00
if post.url:
if post.url.endswith(VALID_ENDS):
image_url = post.url
title = post.title
break
2020-04-05 16:08:31 -04:00
elif "v.redd.it" in post.url:
image_url = post.media['reddit_video']['fallback_url'].split("?")[0]
title = post.title
break
2023-10-16 09:46:31 -04:00
if not image_url:
await event.reply(f"`Failed to find any valid content on `**r/{sub}**`!`")
return
try:
await event.reply(title, file=image_url)
2023-10-16 09:46:31 -04:00
except:
await event.reply(f"`Failed to download content from `**r/{sub}**`!`\n`Title: `**{title}**\n`URL: `{image_url}")
async def titlefetcher(event, sub):
try:
post = REDDIT.subreddit(sub).random() or await titlefetcherfallback(sub)
post.title
except redex.Forbidden:
await event.reply(f"**r/{sub}**` is private!`")
return
2020-05-03 16:35:02 -04:00
except (redex.NotFound, KeyError):
await event.reply(f"**r/{sub}**` doesn't exist!`")
return
2023-10-16 09:46:31 -04:00
await event.reply(post.title)
2023-10-16 09:46:31 -04:00
2020-04-04 09:48:13 -04:00
async def bodyfetcher(event, sub):
for _ in range(10):
try:
post = REDDIT.subreddit(sub).random() or await bodyfetcherfallback(sub)
post.title
except redex.Forbidden:
await event.reply(f"**r/{sub}**` is private!`")
return
2020-05-03 16:35:02 -04:00
except (redex.NotFound, KeyError):
await event.reply(f"**r/{sub}**` doesn't exist!`")
return
except AttributeError:
continue
body = None
2020-04-04 09:48:13 -04:00
if post.selftext and not post.permalink is post.url:
2020-04-04 09:48:13 -04:00
body = post.selftext
title = post.title
if not body:
continue
2020-04-04 09:49:55 -04:00
await event.reply(f"**{title}**\n\n{body}")
2020-04-04 09:48:13 -04:00
return
2020-04-04 09:49:55 -04:00
await event.reply(f"`Failed to find any valid content on `**r/{sub}**`!`")
2020-04-04 09:48:13 -04:00
2020-05-04 11:11:23 -04:00
@ldr.add("red(i|t|b)")
2023-10-16 09:46:31 -04:00
async def redimg(event):
2020-05-02 16:40:26 -04:00
sub = event.args.replace(" ", "_")
2020-05-04 11:11:23 -04:00
fetch_type = event.pattern_match.group(1)
2023-10-16 09:46:31 -04:00
2020-05-04 11:11:23 -04:00
if not sub:
await event.reply(f"`Syntax: {ldr.settings.get_config('cmd_prefix') or '.'}red(i|t|b) <subreddit name>`")
return
2023-10-16 09:46:31 -04:00
2020-05-04 11:11:23 -04:00
if fetch_type == "i":
await imagefetcher(event, sub)
elif fetch_type == "t":
2023-10-16 09:46:31 -04:00
await titlefetcher(event, sub)
2020-05-04 11:11:23 -04:00
elif fetch_type == "b":
2020-04-04 09:48:13 -04:00
await bodyfetcher(event, sub)
@ldr.add("suffer")
2023-10-16 09:46:31 -04:00
async def makemesuffer(event):
await imagefetcher(event, "MakeMeSuffer")
@ldr.add("snafu")
2023-10-16 09:46:31 -04:00
async def coaxedintoasnafu(event):
await imagefetcher(event, "CoaxedIntoASnafu")
@ldr.add("aita")
2023-10-16 09:46:31 -04:00
async def amitheasshole(event):
await bodyfetcher(event, "AmITheAsshole")
@ldr.add("tifu")
async def todayifuckedup(event):
await bodyfetcher(event, "TIFU")
2023-10-16 09:46:31 -04:00
@ldr.add("jon(x|)")
2023-10-16 09:46:31 -04:00
async def imsorryjon(event):
if "x" in event.pattern_match.group(0):
sub = "ImReallySorryJon"
else:
sub = "ImSorryJon"
await imagefetcher(event, sub)
@ldr.add("tihi")
2023-10-16 09:46:31 -04:00
async def thanksihateit(event):
await imagefetcher(event, "TIHI")
2020-03-24 11:01:24 -04:00
@ldr.add("gab")
2020-03-24 11:01:24 -04:00
async def tenma(event):
await imagefetcher(event, "tenma")