2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-22 18:19:16 +00:00
microbot/ubot/modules/fourchan.py

83 lines
3.1 KiB
Python
Raw 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}"
2020-04-07 12:11:36 -04:00
VALID_ENDS = (".mp4", ".jpg", ".jpeg", ".png", ".gif")
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:
2020-06-23 15:28:09 -04:00
await event.reply(f"Syntax: {ldr.prefix()}4c(f|) <board name>")
2020-05-24 10:05:15 -04:00
return
2020-06-26 15:24:01 -04:00
if event.nsfw_disabled and event.args 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
as_file = bool(event.other_args[0])
2020-04-06 10:24:10 -04:00
2020-05-09 13:15:08 -04:00
async with ldr.aioclient.get(BOARD_URL.format(event.args)) 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-05-09 13:15:08 -04:00
async with ldr.aioclient.get(POST_URL.format(event.args, op_id)) as response:
2020-04-06 10:24:10 -04:00
if response.status == 200:
post_response = await response.json()
2020-04-07 12:11:36 -04:00
post_info = choice([[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-05-02 16:40:26 -04:00
post_file_url = CONTENT_URL.format(event.args, 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-06-06 11:04:14 -04:00
await event.reply(f"No results for board: **{event.args}**")
2020-04-06 10:24:10 -04:00
return
try:
2020-04-06 11:31:25 -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")
2020-04-06 10:24:10 -04:00
return
except:
pass
2020-06-06 11:04:14 -04:00
await event.reply(f"Failed to fetch media for board: **{event.args}**")
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
async with ldr.aioclient.get(BOARD_URL.format(event.args)) as response:
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 = []
async with ldr.aioclient.get(POST_URL.format(event.args, op_id)) as response:
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]
shuffle(post_info_list)
for post_info in post_info_list[:3]:
post_file_url_list += [CONTENT_URL.format(event.args, post_info[0], post_info[1])]
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