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

51 lines
1.8 KiB
Python
Raw Normal View History

2020-04-06 10:24:10 -04:00
# SPDX-License-Identifier: GPL-2.0-or-later
from random import choice
from ubot.micro_bot import micro_bot
ldr = micro_bot.loader
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-04-06 10:24:10 -04:00
@ldr.add("4c(f|)")
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: {ldr.settings.get_config('cmd_prefix') or '.'}4c(f|) <board name>`")
return
2020-05-03 20:45:35 -04:00
as_file = bool(event.pattern_match.group(1))
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-04-06 10:32:09 -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-04-06 10:32:09 -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-05-02 16:40:26 -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-05-02 16:40:26 -04:00
await event.reply(f"`Failed to fetch media for board: `**{event.args}**")