2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-23 18:49:31 +00:00
microbot/ubot/modules/fourchan.py

96 lines
3.4 KiB
Python
Raw Permalink Normal View History

2020-05-24 12:11:55 -04:00
from random import choice, shuffle
2020-04-06 10:24:10 -04:00
2020-09-13 13:50:36 -04:00
from ubot import ldr
2020-04-06 10:24:10 -04:00
BOARD_URL = "https://a.4cdn.org/{0}/threads.json"
POST_URL = "https://a.4cdn.org/{0}/thread/{1}.json"
CONTENT_URL = "https://i.4cdn.org/{0}/{1}{2}"
2024-12-26 17:44:41 -05:00
VALID_ENDS = (".webm", ".jpg", ".png", ".gif", ".mp4")
2020-06-26 15:24:01 -04:00
NSFW_BOARDS = ['aco', 'b', 'bant', 'd', 'e', 'f', 'gif', 'h', 'hc', 'hm', 'hr', 'i', 'ic', 'pol', 'r', 'r9k', 's', 's4s', 'soc', 't', 'trash', 'u', 'wg', 'y']
2020-04-06 10:24:10 -04:00
2020-07-21 09:44:28 -04:00
@ldr.add("4c", pattern_extra="(f|)", userlocking=True, pass_nsfw=True, help="Fetches images from 4chan, requires a board name as an argument.")
2020-04-06 10:24:10 -04:00
async def fourchan(event):
2020-05-24 10:05:15 -04:00
if not event.args:
await event.reply(f"Syntax: {event.prefix}4c(f|) <board name>")
2020-05-24 10:05:15 -04:00
return
2020-10-18 11:12:05 -04:00
board = event.args.lower()
as_file = bool(event.other_args[0])
if event.nsfw_disabled and board in NSFW_BOARDS:
2020-10-10 19:38:21 -04:00
await event.reply("Sorry, that board is NSFW and NSFW commands are disabled!")
2020-06-26 15:24:01 -04:00
return
2020-10-18 11:12:05 -04:00
async with ldr.aioclient.get(BOARD_URL.format(board)) as response:
2020-04-06 10:24:10 -04:00
if response.status == 200:
board_response = await response.json()
op_id = choice(choice(board_response)["threads"])["no"]
else:
2020-06-06 11:04:14 -04:00
await event.reply(f"An error occurred, response code: **{response.status}**")
2020-04-06 10:24:10 -04:00
return
2020-10-18 11:12:05 -04:00
async with ldr.aioclient.get(POST_URL.format(board, op_id)) as response:
2020-04-06 10:24:10 -04:00
if response.status == 200:
post_response = await response.json()
2020-10-18 11:12:05 -04:00
post_info_list = [[i["tim"], i["ext"], i["com"] if "com" in i else None] for i in post_response["posts"] if "tim" in i and i["ext"] in VALID_ENDS]
if not post_info_list:
await event.reply(f"No results for board: **{board}**")
return
post_info = choice(post_info_list)
post_file_url = CONTENT_URL.format(board, post_info[0], post_info[1])
2020-04-06 10:24:10 -04:00
else:
2020-06-06 11:04:14 -04:00
await event.reply(f"An error occurred, response code: **{response.status}**")
2020-04-06 10:24:10 -04:00
return
if not response:
2020-10-18 11:12:05 -04:00
await event.reply(f"No results for board: **{board}**")
2020-04-06 10:24:10 -04:00
return
try:
2023-04-26 10:37:38 -04:00
await event.reply(post_info[2].replace("<br>", "\n") if post_info[2] else None, file=post_file_url, force_document=as_file, parse_mode="html", spoiler=event.chat_db.spoiler_nsfw and board in NSFW_BOARDS)
2020-04-06 10:24:10 -04:00
return
except:
pass
2020-10-18 11:12:05 -04:00
await event.reply(f"Failed to fetch media for board: **{board}**")
2020-05-24 12:11:55 -04:00
@ldr.add_inline_photo("4c", default="4c")
2020-05-24 12:15:43 -04:00
async def fourchan_inline(event):
2020-05-24 12:11:55 -04:00
if not event.args:
2020-08-02 08:23:54 -04:00
return
2020-05-24 12:11:55 -04:00
2020-10-18 11:12:05 -04:00
board = event.args.lower()
async with ldr.aioclient.get(BOARD_URL.format(board)) as response:
2020-05-24 12:11:55 -04:00
if response.status == 200:
board_response = await response.json()
op_id = choice(choice(board_response)["threads"])["no"]
else:
2020-08-02 08:23:54 -04:00
return
2020-05-24 12:11:55 -04:00
post_file_url_list = []
2020-10-18 11:12:05 -04:00
async with ldr.aioclient.get(POST_URL.format(board, op_id)) as response:
2020-05-24 12:11:55 -04:00
if response.status == 200:
post_response = await response.json()
post_info_list = [[i["tim"], i["ext"], i["com"] if "com" in i else None] for i in post_response["posts"] if "tim" in i and i["ext"] in VALID_ENDS]
2020-10-18 11:12:05 -04:00
if not post_info_list:
return
2020-05-24 12:11:55 -04:00
shuffle(post_info_list)
for post_info in post_info_list[:3]:
2020-10-18 11:12:05 -04:00
post_file_url_list += [CONTENT_URL.format(board, post_info[0], post_info[1])]
2020-05-24 12:11:55 -04:00
else:
2020-08-02 08:23:54 -04:00
return
2020-05-24 12:11:55 -04:00
if not response:
2020-08-02 08:23:54 -04:00
return
2020-05-24 12:11:55 -04:00
return post_file_url_list