2020-03-16 21:02:05 -04:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2023-10-16 09:46:31 -04:00
|
|
|
|
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-06-19 14:40:41 -04:00
|
|
|
from ubot.micro_bot 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,
|
|
|
|
user_agent='TG_Userbot')
|
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-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-05-03 10:19:39 -04:00
|
|
|
return None
|
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
|
|
|
|
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
|
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:
|
2020-07-17 22:28:03 -04:00
|
|
|
post = await subreddit.random() or 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:
|
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-07-18 09:57:22 -04:00
|
|
|
subreddit = await REDDIT.subreddit(sub)
|
|
|
|
|
2020-05-03 10:19:39 -04:00
|
|
|
try:
|
2020-07-18 09:57:22 -04:00
|
|
|
post = await subreddit.random() or await titlefetcherfallback(subreddit)
|
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
|
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:
|
2020-07-17 22:28:03 -04:00
|
|
|
post = await subreddit.random() or 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-16 13:41:42 -04:00
|
|
|
@ldr.add_list(["redi", "redb", "redt"], userlocking=True, pass_nsfw=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-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:
|
2020-06-23 15:28:09 -04:00
|
|
|
await event.reply(f"Syntax: {ldr.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-06-26 15:02:38 -04:00
|
|
|
@ldr.add("suffer", userlocking=True, pass_nsfw=True)
|
2023-10-16 09:46:31 -04:00
|
|
|
async def makemesuffer(event):
|
|
|
|
await imagefetcher(event, "MakeMeSuffer")
|
|
|
|
|
|
|
|
|
2020-06-26 15:02:38 -04:00
|
|
|
@ldr.add("snafu", userlocking=True, pass_nsfw=True)
|
2023-10-16 09:46:31 -04:00
|
|
|
async def coaxedintoasnafu(event):
|
|
|
|
await imagefetcher(event, "CoaxedIntoASnafu")
|
|
|
|
|
|
|
|
|
2020-06-26 15:02:38 -04:00
|
|
|
@ldr.add("aita", userlocking=True, pass_nsfw=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-26 15:02:38 -04:00
|
|
|
@ldr.add("tifu", userlocking=True, pass_nsfw=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-07-16 13:41:42 -04:00
|
|
|
@ldr.add_list(["jon", "jonx"], userlocking=True, pass_nsfw=True)
|
2023-10-16 09:46:31 -04:00
|
|
|
async def imsorryjon(event):
|
2020-07-16 13:41:42 -04:00
|
|
|
if event.command[-1] == "x":
|
|
|
|
await imagefetcher(event, "ImReallySorryJon")
|
2023-10-16 09:46:31 -04:00
|
|
|
else:
|
2020-07-16 13:41:42 -04:00
|
|
|
await imagefetcher(event, "ImSorryJon")
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
|
2020-06-26 15:02:38 -04:00
|
|
|
@ldr.add("tihi", userlocking=True, pass_nsfw=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-26 15:02:38 -04:00
|
|
|
@ldr.add("gab", userlocking=True, pass_nsfw=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
|
|
|
|
|
|
|
|
2020-06-26 15:02:38 -04:00
|
|
|
@ldr.add("pourn", userlocking=True, pass_nsfw=True)
|
2020-06-20 19:49:18 -04:00
|
|
|
async def pourn(event):
|
|
|
|
await imagefetcher(event, "PourPainting")
|