More cross-platform; (web search broken now..)

This commit is contained in:
octospacc 2023-07-16 17:52:58 +02:00
parent 257487a269
commit 49c2e23d49
3 changed files with 68 additions and 66 deletions

View File

@ -36,9 +36,11 @@
"_Your level of *{Thing}* is... *{Percent}%*._"
]
},
"hash": [
"*Usage*: {0} <Algorithm> <Text to Hash>.\n\n*Available algorithms*: {1}."
],
"hash": {
"usage": [
"*Usage*: {0} &lt;Algorithm&gt; &lt;Text to Hash&gt;.\n\n*Available algorithms*: {1}."
]
},
"eval": [
"This feature is not implemented [Security Issue]."
],

View File

@ -77,15 +77,12 @@ def cEcho(Context, Data=None) -> None:
# CharEscape(choice(Locale.__('time')).format(time.ctime().replace(' ', ' ')), 'MARKDOWN_SPEECH'),
# reply_to_message_id=update.message.message_id)
def cHash(update:Update, context:CallbackContext) -> None:
Cmd = HandleCmd(update)
if not Cmd: return
if len(Cmd.Tokens) >= 3 and Cmd.Tokens[1] in hashlib.algorithms_available:
Alg = Cmd.Tokens[1]
Caption = hashlib.new(Alg, Alg.join(Cmd.Body.split(Alg)[1:]).strip().encode()).hexdigest()
def cHash(Context, Data=None) -> None:
if len(Data.Tokens) >= 3 and Data.Tokens[1] in hashlib.algorithms_available:
Alg = Data.Tokens[1]
SendMsg(Context, {"Text": hashlib.new(Alg, Alg.join(Data.Body.split(Alg)[1:]).strip().encode()).hexdigest()})
else:
Caption = CharEscape(choice(Locale.__('hash')).format(Cmd.Tokens[0], hashlib.algorithms_available), 'MARKDOWN_SPEECH')
update.message.reply_markdown_v2(Caption, reply_to_message_id=update.message.message_id)
SendMsg(Context, {"Text": choice(Locale.__('hash.usage')).format(Data.Tokens[0], hashlib.algorithms_available)})
def cEval(update:Update, context:CallbackContext) -> None:
Cmd = HandleCmd(update)
@ -109,55 +106,45 @@ def cExec(update:Update, context:CallbackContext) -> None:
CharEscape(choice(Locale.__('eval')), 'MARKDOWN_SPEECH'),
reply_to_message_id=update.message.message_id)
def cWeb(update:Update, context:CallbackContext) -> None:
Cmd = HandleCmd(update)
if not Cmd: return
Msg = update.message.text
Toks = Cmd.Tokens
if len(Cmd.Tokens) >= 2:
def cWeb(Context, Data=None) -> None:
if Data.Body:
try:
Key = ParseCmd(Msg).Name
Query = Key.join(Msg.split(Key)[1:]).strip()
QueryUrl = UrlParse.quote(Query)
QueryUrl = UrlParse.quote(Data.Body)
Req = HttpGet(f'https://html.duckduckgo.com/html?q={QueryUrl}')
Caption = f'[🦆🔎 "*{CharEscape(Query, "MARKDOWN")}*"](https://duckduckgo.com/?q={CharEscape(QueryUrl, "MARKDOWN")})\n\n'
#Caption = f'[🦆🔎 "*{CharEscape(Data.Body, "MARKDOWN")}*"](https://duckduckgo.com/?q={CharEscape(QueryUrl, "MARKDOWN")})\n\n'
Caption = '[🦆🔎 "*{Data.Body}*"](https://duckduckgo.com/?q={QueryUrl})\n\n'
Index = 0
for Line in Req.read().decode().replace('\t', ' ').splitlines():
if ' class="result__a" ' in Line and ' href="//duckduckgo.com/l/?uddg=' in Line:
Index += 1
Link = CharEscape(UrlParse.unquote(Line.split(' href="//duckduckgo.com/l/?uddg=')[1].split('&amp;rut=')[0]), 'MARKDOWN')
Title = CharEscape(UrlParse.unquote(Line.split('</a>')[0].split('</span>')[-1].split('>')[1]), 'MARKDOWN')
#Link = CharEscape(UrlParse.unquote(Line.split(' href="//duckduckgo.com/l/?uddg=')[1].split('&amp;rut=')[0]), 'MARKDOWN')
#Title = CharEscape(UrlParse.unquote(Line.split('</a>')[0].split('</span>')[-1].split('>')[1]), 'MARKDOWN')
Link = UrlParse.unquote(Line.split(' href="//duckduckgo.com/l/?uddg=')[1].split('&amp;rut=')[0])
Title = UrlParse.unquote(Line.split('</a>')[0].split('</span>')[-1].split('>')[1])
Domain = Link.split('://')[1].split('/')[0]
Caption += f'{Index}\. [{Title}]({Link}) \[`{Domain}`\]\n\n'
update.message.reply_markdown_v2(Caption, reply_to_message_id=update.message.message_id)
#Caption += f'{Index}\. [{Title}]({Link}) \[`{Domain}`\]\n\n'
Caption += f'{Index}. [{Title}]({Link}) [`{Domain}`]\n\n'
SendMsg(Context, {"Text": Caption})
except Exception:
raise
def cUnsplash(update:Update, context:CallbackContext) -> None:
Cmd = HandleCmd(update)
if not Cmd: return
def cUnsplash(Context, Data=None) -> None:
try:
Req = HttpGet(f'https://source.unsplash.com/random/?{UrlParse.quote(Cmd.Body)}')
update.message.reply_photo(
Req.read(),
caption=MarkdownCode(Req.geturl().split('?')[0], True),
parse_mode='MarkdownV2',
reply_to_message_id=update.message.message_id)
Req = HttpGet(f'https://source.unsplash.com/random/?{UrlParse.quote(Data.Body)}')
SendMsg(Context, {"Text": MarkdownCode(Req.geturl().split('?')[0], True), "Media": Req.read()})
except Exception:
raise
def cSafebooru(update:Update, context:CallbackContext) -> None:
Cmd = HandleCmd(update)
if not Cmd: return
def cSafebooru(Context, Data=None) -> None:
ApiUrl = 'https://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags='
try:
if Cmd.Body:
if Data.Body:
for i in range(7):
ImgUrls = HttpGet(f'{ApiUrl}md5:{RandHexStr(3)}%20{UrlParse.quote(Cmd.Body)}').read().decode().split(' file_url="')[1:]
ImgUrls = HttpGet(f'{ApiUrl}md5:{RandHexStr(3)}%20{UrlParse.quote(Data.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:]
ImgUrls = HttpGet(f'{ApiUrl}{UrlParse.quote(Data.Body)}').read().decode().split(' file_url="')[1:]
ImgXml = choice(ImgUrls)
ImgUrl = ImgXml.split('"')[0]
ImgId = ImgXml.split(' id="')[1].split('"')[0]
@ -169,11 +156,7 @@ def cSafebooru(update:Update, context:CallbackContext) -> None:
ImgId = ImgUrl.split('?')[-1]
break
if ImgUrl:
update.message.reply_photo(
HttpGet(ImgUrl).read(),
caption=f'`{ImgId}`\n' + MarkdownCode(ImgUrl, True),
parse_mode='MarkdownV2',
reply_to_message_id=update.message.message_id)
SendMsg(Context, {"Text": (f'`{ImgId}`\n' + MarkdownCode(ImgUrl, True)), "Media": HttpGet(ImgUrl).read()})
else:
pass
except Exception:

View File

@ -21,7 +21,7 @@ from urllib import parse as UrlParse
from urllib.request import urlopen, Request
# <https://daringfireball.net/projects/markdown/syntax#backslash>
MdEscapes = '\\`*_{}[]()<>#+-.!'
MdEscapes = '\\`*_{}[]()<>#=+-.!|'
Db = {"Chats": {}}
Locale = {"Fallback": {}}
@ -49,12 +49,12 @@ Endpoints = {
#"floor": multifun,
#"hands": multifun,
#"sessocto": multifun,
#"hash": cHash,
"hash": cHash,
#"eval": cEval,
#"exec": cExec,
#"web": cWeb,
#"unsplash": cUnsplash,
#"safebooru": cSafebooru,
"web": cWeb,
"unsplash": cUnsplash,
"safebooru": cSafebooru,
}
def SetupLocale() -> None:
@ -98,6 +98,12 @@ def SetupDb() -> None:
except Exception:
pass
def InDict(Dict:dict, Key:str):
if Key in Dict:
return Dict[Key]
else:
return None
def CharEscape(String:str, Escape:str='') -> str:
if Escape == 'MARKDOWN':
return escape_markdown(String, version=2)
@ -216,20 +222,34 @@ def SendMsg(Context, Data):
Manager = Context['Manager'] if 'Manager' in Context else None
else:
[Event, Manager] = [Context, Context]
TextPlain = MdToTxt(Data['Text'])
if InDict(Data, 'Text'):
TextPlain = MdToTxt(Data['Text'])
TextMarkdown = CharEscape(HtmlUnescape(Data['Text']), InferMdEscape(HtmlUnescape(Data['Text']), TextPlain))
if isinstance(Manager, mastodon.Mastodon):
Manager.status_post(
(TextPlain + '\n\n@' + Event['account']['acct']),
in_reply_to_id=Event['status']['id'],
visibility=('direct' if Event['status']['visibility'] == 'direct' else 'unlisted')
)
if InDict(Data, 'Media'):
Media = Manager.media_post(Data['Media'])
while Media['url'] == 'null':
Media = Manager.media(Media)
if InDict(Data, 'Text'):
Manager.status_post(
status=(TextPlain + '\n\n@' + Event['account']['acct']),
media_ids=(Media if Data['Media'] else None),
in_reply_to_id=Event['status']['id'],
visibility=('direct' if Event['status']['visibility'] == 'direct' else 'unlisted'),
)
elif isinstance(Manager, telegram.Update):
Event.message.reply_markdown_v2(
CharEscape(HtmlUnescape(Data['Text']), InferMdEscape(HtmlUnescape(Data['Text']), TextPlain)),
#Event.message.reply_text(
#TextPlain,
reply_to_message_id=Event.message.message_id
)
if InDict(Data, 'Media'):
Event.message.reply_photo(
Data['Media'],
caption=(TextMarkdown if Data['Text'] else None),
parse_mode='MarkdownV2',
reply_to_message_id=Event.message.message_id,
)
elif InDict(Data, 'Text'):
Event.message.reply_markdown_v2(
TextMarkdown,
reply_to_message_id=Event.message.message_id,
)
def Main() -> None:
SetupDb()
@ -245,12 +265,9 @@ def Main() -> None:
dispatcher.add_handler(CommandHandler('help', cHelp))
dispatcher.add_handler(CommandHandler('source', cSource))
#dispatcher.add_handler(CommandHandler('time', cTime))
dispatcher.add_handler(CommandHandler('hash', cHash))
dispatcher.add_handler(CommandHandler('eval', cEval))
dispatcher.add_handler(CommandHandler('exec', cExec))
dispatcher.add_handler(CommandHandler('web', cWeb))
dispatcher.add_handler(CommandHandler('unsplash', cUnsplash))
dispatcher.add_handler(CommandHandler('safebooru', cSafebooru))
#dispatcher.add_handler(CommandHandler('web', cWeb))
for Cmd in ('hug', 'pat', 'poke', 'cuddle', 'floor', 'hands', 'sessocto'):
dispatcher.add_handler(CommandHandler(Cmd, multifun))