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
|
2020-05-03 10:19:39 -04:00
|
|
|
from prawcore import exceptions as redex
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-06-19 14:40:41 -04:00
|
|
|
from ubot.micro_bot import ldr
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
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__())
|
2020-05-03 10:19:39 -04:00
|
|
|
shuffle(hot_list)
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-05-03 10:19:39 -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
|
|
|
|
2020-05-03 10:19:39 -04:00
|
|
|
return None
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def titlefetcherfallback(sub):
|
|
|
|
hot = REDDIT.subreddit(sub).hot()
|
2020-05-03 10:19:39 -04:00
|
|
|
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)
|
|
|
|
|
2020-05-03 10:19:39 -04:00
|
|
|
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
|
|
|
|
2020-05-03 10:19:39 -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):
|
2020-05-03 10:19:39 -04:00
|
|
|
try:
|
|
|
|
post = REDDIT.subreddit(sub).random() or await imagefetcherfallback(sub)
|
|
|
|
post.title
|
|
|
|
except redex.Forbidden:
|
2020-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"**r/{sub}** is private!")
|
2020-05-03 10:19:39 -04:00
|
|
|
return
|
2020-05-03 16:35:02 -04:00
|
|
|
except (redex.NotFound, KeyError):
|
2020-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"**r/{sub}** doesn't exist!")
|
2020-05-03 10:19:39 -04:00
|
|
|
return
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-05-04 20:07:43 -04:00
|
|
|
if post.url and post.url.endswith(VALID_ENDS):
|
|
|
|
image_url = post.url
|
|
|
|
title = post.title
|
|
|
|
break
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
if not image_url:
|
2020-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"Failed to find any valid content on **r/{sub}**!")
|
2023-10-16 09:46:31 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2020-02-23 15:28:50 -05:00
|
|
|
await event.reply(title, file=image_url)
|
2023-10-16 09:46:31 -04:00
|
|
|
except:
|
2020-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"Failed to download content from **r/{sub}**!\nTitle: **{title}**\nURL: {image_url}")
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def titlefetcher(event, sub):
|
2020-05-03 10:19:39 -04:00
|
|
|
try:
|
|
|
|
post = REDDIT.subreddit(sub).random() or await titlefetcherfallback(sub)
|
|
|
|
post.title
|
|
|
|
except redex.Forbidden:
|
2020-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"**r/{sub}** is private!")
|
2020-05-03 10:19:39 -04:00
|
|
|
return
|
2020-05-03 16:35:02 -04:00
|
|
|
except (redex.NotFound, KeyError):
|
2020-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"**r/{sub}** doesn't exist!")
|
2020-05-03 10:19:39 -04:00
|
|
|
return
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-05-03 10:19:39 -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):
|
2020-05-03 10:19:39 -04:00
|
|
|
try:
|
|
|
|
post = REDDIT.subreddit(sub).random() or await bodyfetcherfallback(sub)
|
|
|
|
post.title
|
|
|
|
except redex.Forbidden:
|
2020-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"**r/{sub}** is private!")
|
2020-05-03 10:19:39 -04:00
|
|
|
return
|
2020-05-03 16:35:02 -04:00
|
|
|
except (redex.NotFound, KeyError):
|
2020-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"**r/{sub}** doesn't exist!")
|
2020-05-03 10:19:39 -04:00
|
|
|
return
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
2020-04-04 10:03:40 -04:00
|
|
|
body = None
|
2020-04-04 09:48:13 -04:00
|
|
|
|
2020-05-03 10:19:39 -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-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"Failed to find any valid content on **r/{sub}**!")
|
2020-04-04 09:48:13 -04:00
|
|
|
|
|
|
|
|
2020-06-16 19:16:06 -04:00
|
|
|
@ldr.add("red(i|t|b)", userlocking=True)
|
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:
|
2020-06-06 11:04:14 -04:00
|
|
|
await event.reply(f"Syntax: {ldr.settings.get_config('cmd_prefix') or '.'}red(i|t|b) <subreddit name>")
|
2020-05-04 11:11:23 -04:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-06-16 19:16:06 -04:00
|
|
|
@ldr.add("suffer", userlocking=True)
|
2023-10-16 09:46:31 -04:00
|
|
|
async def makemesuffer(event):
|
|
|
|
await imagefetcher(event, "MakeMeSuffer")
|
|
|
|
|
|
|
|
|
2020-06-16 19:16:06 -04:00
|
|
|
@ldr.add("snafu", userlocking=True)
|
2023-10-16 09:46:31 -04:00
|
|
|
async def coaxedintoasnafu(event):
|
|
|
|
await imagefetcher(event, "CoaxedIntoASnafu")
|
|
|
|
|
|
|
|
|
2020-06-16 19:16:06 -04:00
|
|
|
@ldr.add("aita", userlocking=True)
|
2023-10-16 09:46:31 -04:00
|
|
|
async def amitheasshole(event):
|
2020-05-03 10:19:39 -04:00
|
|
|
await bodyfetcher(event, "AmITheAsshole")
|
|
|
|
|
|
|
|
|
2020-06-16 19:16:06 -04:00
|
|
|
@ldr.add("tifu", userlocking=True)
|
2020-05-03 10:19:39 -04:00
|
|
|
async def todayifuckedup(event):
|
|
|
|
await bodyfetcher(event, "TIFU")
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
|
2020-06-16 19:16:06 -04:00
|
|
|
@ldr.add("jon(x|)", userlocking=True)
|
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)
|
|
|
|
|
|
|
|
|
2020-06-16 19:16:06 -04:00
|
|
|
@ldr.add("tihi", userlocking=True)
|
2023-10-16 09:46:31 -04:00
|
|
|
async def thanksihateit(event):
|
|
|
|
await imagefetcher(event, "TIHI")
|
2020-03-24 11:01:24 -04:00
|
|
|
|
|
|
|
|
2020-06-16 19:16:06 -04:00
|
|
|
@ldr.add("gab", userlocking=True)
|
2020-03-24 11:01:24 -04:00
|
|
|
async def tenma(event):
|
|
|
|
await imagefetcher(event, "tenma")
|
2020-06-20 19:49:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
@ldr.add("pourn", userlocking=True)
|
|
|
|
async def pourn(event):
|
|
|
|
await imagefetcher(event, "PourPainting")
|