From 4a538c66f305aaac65469445549c15499e2addc0 Mon Sep 17 00:00:00 2001 From: octospacc Date: Sun, 9 Jul 2023 00:39:56 +0200 Subject: [PATCH] Safebooru --- ModWinDog/Mods.py | 38 ++++++++++++++++++++++++++++++++++---- WinDog.py | 11 ++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/ModWinDog/Mods.py b/ModWinDog/Mods.py index 1322d39..bfd61fb 100644 --- a/ModWinDog/Mods.py +++ b/ModWinDog/Mods.py @@ -109,9 +109,7 @@ def cExec(update:Update, context:CallbackContext) -> None: Out = subprocess.run(('sh', '-c', f'export PATH=$PATH:/usr/games; {Cmd}'), stdout=subprocess.PIPE).stdout.decode() # Caption = (re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])').sub('', Out)) #if ExecAllowed[Cmd] else Out) - update.message.reply_markdown_v2( - '```\n' + CharEscape(Caption, 'MARKDOWN').strip() + '\n```', - reply_to_message_id=update.message.message_id) + update.message.reply_markdown_v2(MarkdownCode(Caption, True), reply_to_message_id=update.message.message_id) else: update.message.reply_markdown_v2( CharEscape(choice(Locale.__('eval')), 'MARKDOWN_SPEECH'), @@ -145,8 +143,11 @@ def cUnsplash(update:Update, context:CallbackContext) -> None: Cmd = HandleCmd(update) if not Cmd: return try: + Req = HttpGet(f'https://source.unsplash.com/random/?{UrlParse.quote(Cmd.Body)}') update.message.reply_photo( - HttpGet(f'https://source.unsplash.com/random/?{UrlParse.quote(Cmd.Body)}').read(), + Req.read(), + caption=MarkdownCode(Req.geturl().split('?')[0], True), + parse_mode='MarkdownV2', reply_to_message_id=update.message.message_id) except Exception: raise @@ -154,3 +155,32 @@ def cUnsplash(update:Update, context:CallbackContext) -> None: def cSafebooru(update:Update, context:CallbackContext) -> None: Cmd = HandleCmd(update) if not Cmd: return + ApiUrl = 'https://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags=' + try: + if Cmd.Body: + for i in range(7): + ImgUrls = HttpGet(f'{ApiUrl}md5:{RandHexStr(3)}%20{UrlParse.quote(Cmd.Body)}').read().decode().split(' file_url="')[1:] + if ImgUrls: + break + if not ImgUrls: + ImgUrls = HttpGet(f'{ApiUrl}{UrlParse.quote(Cmd.Body)}').read().decode().split(' file_url="')[1:] + ImgXml = choice(ImgUrls) + ImgUrl = ImgXml.split('"')[0] + ImgId = ImgXml.split(' id="')[1].split('"')[0] + else: + HtmlReq = HttpGet(HttpGet('https://safebooru.org/index.php?page=post&s=random').geturl()) + for Line in HtmlReq.read().decode().replace('\t', ' ').splitlines(): + if ' None: except Exception: pass -def CharEscape(String, Escape='') -> str: +def CharEscape(String:str, Escape:str='') -> str: if Escape == 'MARKDOWN': return escape_markdown(String, version=2) else: @@ -79,6 +79,9 @@ def CharEscape(String, Escape='') -> str: String = String.replace(c, '\\'+c) return String +def MarkdownCode(Text:str, Block:bool): + return '```\n' + CharEscape(Text.strip(), 'MARKDOWN') + '\n```' + def CmdAllowed(update) -> bool: if not TGRestrict: return True @@ -140,6 +143,12 @@ def RandPercent() -> int: Num = str(Num) + '\.' + str(randint(0,9)) + str(randint(0,9)) return Num +def RandHexStr(Len:int) -> str: + Hex = '' + for Char in range(Len): + Hex += choice('0123456789abcdef') + return Hex + #def CmdArgs(Msg:str, Cfg:tuple=None): # Args = [] # Msg = Msg.strip().replace('\t', ' ')