2023-02-05 11:49:51 -05:00
|
|
|
from re import compile
|
|
|
|
|
2020-07-17 14:19:44 -04:00
|
|
|
import asyncpraw
|
|
|
|
from asyncprawcore import exceptions as redex
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-09-13 13:50:36 -04:00
|
|
|
from ubot import ldr
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-07-17 14:19:44 -04:00
|
|
|
REDDIT = asyncpraw.Reddit(client_id='-fmzwojFG6JkGg',
|
|
|
|
client_secret=None,
|
2023-05-07 16:12:12 -04:00
|
|
|
user_agent='TG_Userbot',
|
|
|
|
requestor_kwargs={"session": ldr.aioclient})
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-07-17 22:28:03 -04:00
|
|
|
VALID_ENDS = (".mp4", ".jpg", ".jpeg", "jpe", ".png", ".gif")
|
2023-02-05 11:49:51 -05:00
|
|
|
ext_regex_photo = compile(r"\.(jpg|jpeg|jpe|png)$")
|
|
|
|
ext_regex_video = compile(r"\.(mp4|gif)$")
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
|
2020-07-17 22:28:03 -04:00
|
|
|
async def imagefetcherfallback(subreddit):
|
|
|
|
async for post in subreddit.random_rising(limit=10):
|
2020-05-03 10:19:39 -04:00
|
|
|
if post.url and post.url.endswith(VALID_ENDS):
|
|
|
|
return post
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
|
2020-07-17 22:28:03 -04:00
|
|
|
async def titlefetcherfallback(subreddit):
|
|
|
|
async for post in subreddit.random_rising(limit=1):
|
|
|
|
return post
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-04-04 09:48:13 -04:00
|
|
|
|
2020-07-17 22:28:03 -04:00
|
|
|
async def bodyfetcherfallback(subreddit):
|
|
|
|
async for post in subreddit.random_rising(limit=10):
|
|
|
|
if post.selftext and not post.permalink is post.url:
|
2020-05-03 10:19:39 -04:00
|
|
|
return post
|
2020-04-04 09:48:13 -04:00
|
|
|
|
|
|
|
|
2023-10-16 09:46:31 -04:00
|
|
|
async def imagefetcher(event, sub):
|
|
|
|
image_url = False
|
2020-07-18 09:57:22 -04:00
|
|
|
subreddit = await REDDIT.subreddit(sub)
|
2020-07-17 22:28:03 -04:00
|
|
|
|
2023-10-16 09:46:31 -04:00
|
|
|
for _ in range(10):
|
2020-05-03 10:19:39 -04:00
|
|
|
try:
|
2025-01-06 09:16:46 -05:00
|
|
|
post = await imagefetcherfallback(subreddit)
|
2020-05-03 10:19:39 -04:00
|
|
|
post.title
|
2020-06-26 15:02:38 -04:00
|
|
|
|
|
|
|
if event.nsfw_disabled and post.over_18:
|
|
|
|
continue
|
2020-07-18 09:57:22 -04:00
|
|
|
except redex.Forbidden:
|
|
|
|
await event.reply(f"**r/{sub}** is private!")
|
|
|
|
return
|
|
|
|
except (redex.NotFound, KeyError):
|
|
|
|
await event.reply(f"**r/{sub}** doesn't exist!")
|
|
|
|
return
|
2020-05-03 10:19:39 -04:00
|
|
|
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:
|
2023-04-26 10:37:38 -04:00
|
|
|
await event.reply(title, file=image_url, spoiler=event.chat_db.spoiler_nsfw and post.over_18)
|
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-07-18 09:57:22 -04:00
|
|
|
subreddit = await REDDIT.subreddit(sub)
|
|
|
|
|
2020-05-03 10:19:39 -04:00
|
|
|
try:
|
2025-01-06 09:16:46 -05:00
|
|
|
post = await titlefetcherfallback(subreddit)
|
2020-11-28 10:05:30 -05:00
|
|
|
post.title
|
2020-05-03 10:19:39 -04:00
|
|
|
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
|
2020-11-28 10:05:30 -05:00
|
|
|
except AttributeError:
|
|
|
|
await event.reply(f"Failed to find any valid content on **r/{sub}**!")
|
|
|
|
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):
|
2020-07-18 09:57:22 -04:00
|
|
|
subreddit = await REDDIT.subreddit(sub)
|
2020-07-17 22:28:03 -04:00
|
|
|
|
2020-04-04 09:48:13 -04:00
|
|
|
for _ in range(10):
|
2020-05-03 10:19:39 -04:00
|
|
|
try:
|
2025-01-06 09:16:46 -05:00
|
|
|
post = await bodyfetcherfallback(subreddit)
|
2020-05-03 10:19:39 -04:00
|
|
|
post.title
|
2020-07-18 09:57:22 -04:00
|
|
|
except redex.Forbidden:
|
|
|
|
await event.reply(f"**r/{sub}** is private!")
|
|
|
|
return
|
|
|
|
except (redex.NotFound, KeyError):
|
|
|
|
await event.reply(f"**r/{sub}** doesn't exist!")
|
|
|
|
return
|
2020-05-03 10:19:39 -04:00
|
|
|
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-22 21:53:11 -04:00
|
|
|
|
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-07-21 09:44:28 -04:00
|
|
|
@ldr.add("redi", userlocking=True, pass_nsfw=True, help="Fetches images from Reddit, requires a subreddit name as an argument.")
|
|
|
|
@ldr.add("redb", userlocking=True, pass_nsfw=True, help="Fetches text from Reddit, requires a subreddit name as an argument.")
|
|
|
|
@ldr.add("redt", userlocking=True, pass_nsfw=True, help="Fetches titles from Reddit, requires a subreddit name as an argument.")
|
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-07-16 13:41:42 -04:00
|
|
|
fetch_type = event.command[-1]
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-05-04 11:11:23 -04:00
|
|
|
if not sub:
|
2021-03-24 16:16:13 -04:00
|
|
|
await event.reply(f"Syntax: {event.prefix}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-08-24 09:33:53 -04:00
|
|
|
reddit_images = {
|
|
|
|
"suffer": "MakeMeSuffer",
|
|
|
|
"snafu": "CoaxedIntoASnafu",
|
|
|
|
"jon": "ImSorryJon",
|
|
|
|
"jonx": "ImReallySorryJon",
|
|
|
|
"tihi": "TIHI",
|
|
|
|
"gab": "Tenma",
|
|
|
|
"pourn": "PourPainting",
|
2020-08-24 10:05:25 -04:00
|
|
|
"monke": "Ape",
|
2020-09-13 14:12:45 -04:00
|
|
|
"meme": "DankMemes",
|
2020-12-21 00:01:38 -05:00
|
|
|
"okbr": "OKBuddyRetard",
|
2021-01-10 13:45:09 -05:00
|
|
|
"comedy": "ComedyNecrophilia",
|
2021-01-20 23:39:44 -05:00
|
|
|
"reddit": "RedditMoment",
|
2021-01-20 23:48:22 -05:00
|
|
|
"heartdisease": "Chonkers",
|
2022-08-02 17:30:34 -04:00
|
|
|
"smolcat": "IllegallySmolCats",
|
|
|
|
"ordinary": "AlzheimersGroup"
|
2020-08-24 09:33:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
reddit_bodies = {
|
|
|
|
"aita": "AmITheAsshole",
|
|
|
|
"tifu": "TIFU"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ldr.add_dict(reddit_images, userlocking=True, pass_nsfw=True)
|
|
|
|
async def quick_reddit_image(event):
|
|
|
|
await imagefetcher(event, event.extra)
|
|
|
|
|
|
|
|
|
|
|
|
@ldr.add_dict(reddit_bodies, userlocking=True, pass_nsfw=True)
|
|
|
|
async def quick_reddit_body(event):
|
|
|
|
await bodyfetcher(event, event.extra)
|