mirror of
https://gitlab.com/octospacc/WinDog.git
synced 2025-06-05 22:09:20 +02:00
More cross-platform; (web search broken now..)
This commit is contained in:
@ -36,9 +36,11 @@
|
|||||||
"_Your level of *{Thing}* is... *{Percent}%*._"
|
"_Your level of *{Thing}* is... *{Percent}%*._"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": [
|
"hash": {
|
||||||
"*Usage*: {0} <Algorithm> <Text to Hash>.\n\n*Available algorithms*: {1}."
|
"usage": [
|
||||||
],
|
"*Usage*: {0} <Algorithm> <Text to Hash>.\n\n*Available algorithms*: {1}."
|
||||||
|
]
|
||||||
|
},
|
||||||
"eval": [
|
"eval": [
|
||||||
"This feature is not implemented [Security Issue]."
|
"This feature is not implemented [Security Issue]."
|
||||||
],
|
],
|
||||||
|
@ -77,15 +77,12 @@ def cEcho(Context, Data=None) -> None:
|
|||||||
# CharEscape(choice(Locale.__('time')).format(time.ctime().replace(' ', ' ')), 'MARKDOWN_SPEECH'),
|
# CharEscape(choice(Locale.__('time')).format(time.ctime().replace(' ', ' ')), 'MARKDOWN_SPEECH'),
|
||||||
# reply_to_message_id=update.message.message_id)
|
# reply_to_message_id=update.message.message_id)
|
||||||
|
|
||||||
def cHash(update:Update, context:CallbackContext) -> None:
|
def cHash(Context, Data=None) -> None:
|
||||||
Cmd = HandleCmd(update)
|
if len(Data.Tokens) >= 3 and Data.Tokens[1] in hashlib.algorithms_available:
|
||||||
if not Cmd: return
|
Alg = Data.Tokens[1]
|
||||||
if len(Cmd.Tokens) >= 3 and Cmd.Tokens[1] in hashlib.algorithms_available:
|
SendMsg(Context, {"Text": hashlib.new(Alg, Alg.join(Data.Body.split(Alg)[1:]).strip().encode()).hexdigest()})
|
||||||
Alg = Cmd.Tokens[1]
|
|
||||||
Caption = hashlib.new(Alg, Alg.join(Cmd.Body.split(Alg)[1:]).strip().encode()).hexdigest()
|
|
||||||
else:
|
else:
|
||||||
Caption = CharEscape(choice(Locale.__('hash')).format(Cmd.Tokens[0], hashlib.algorithms_available), 'MARKDOWN_SPEECH')
|
SendMsg(Context, {"Text": choice(Locale.__('hash.usage')).format(Data.Tokens[0], hashlib.algorithms_available)})
|
||||||
update.message.reply_markdown_v2(Caption, reply_to_message_id=update.message.message_id)
|
|
||||||
|
|
||||||
def cEval(update:Update, context:CallbackContext) -> None:
|
def cEval(update:Update, context:CallbackContext) -> None:
|
||||||
Cmd = HandleCmd(update)
|
Cmd = HandleCmd(update)
|
||||||
@ -109,55 +106,45 @@ def cExec(update:Update, context:CallbackContext) -> None:
|
|||||||
CharEscape(choice(Locale.__('eval')), 'MARKDOWN_SPEECH'),
|
CharEscape(choice(Locale.__('eval')), 'MARKDOWN_SPEECH'),
|
||||||
reply_to_message_id=update.message.message_id)
|
reply_to_message_id=update.message.message_id)
|
||||||
|
|
||||||
def cWeb(update:Update, context:CallbackContext) -> None:
|
def cWeb(Context, Data=None) -> None:
|
||||||
Cmd = HandleCmd(update)
|
if Data.Body:
|
||||||
if not Cmd: return
|
|
||||||
Msg = update.message.text
|
|
||||||
Toks = Cmd.Tokens
|
|
||||||
if len(Cmd.Tokens) >= 2:
|
|
||||||
try:
|
try:
|
||||||
Key = ParseCmd(Msg).Name
|
QueryUrl = UrlParse.quote(Data.Body)
|
||||||
Query = Key.join(Msg.split(Key)[1:]).strip()
|
|
||||||
QueryUrl = UrlParse.quote(Query)
|
|
||||||
Req = HttpGet(f'https://html.duckduckgo.com/html?q={QueryUrl}')
|
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
|
Index = 0
|
||||||
for Line in Req.read().decode().replace('\t', ' ').splitlines():
|
for Line in Req.read().decode().replace('\t', ' ').splitlines():
|
||||||
if ' class="result__a" ' in Line and ' href="//duckduckgo.com/l/?uddg=' in Line:
|
if ' class="result__a" ' in Line and ' href="//duckduckgo.com/l/?uddg=' in Line:
|
||||||
Index += 1
|
Index += 1
|
||||||
Link = CharEscape(UrlParse.unquote(Line.split(' href="//duckduckgo.com/l/?uddg=')[1].split('&rut=')[0]), 'MARKDOWN')
|
#Link = CharEscape(UrlParse.unquote(Line.split(' href="//duckduckgo.com/l/?uddg=')[1].split('&rut=')[0]), 'MARKDOWN')
|
||||||
Title = CharEscape(UrlParse.unquote(Line.split('</a>')[0].split('</span>')[-1].split('>')[1]), '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('&rut=')[0])
|
||||||
|
Title = UrlParse.unquote(Line.split('</a>')[0].split('</span>')[-1].split('>')[1])
|
||||||
Domain = Link.split('://')[1].split('/')[0]
|
Domain = Link.split('://')[1].split('/')[0]
|
||||||
Caption += f'{Index}\. [{Title}]({Link}) \[`{Domain}`\]\n\n'
|
#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'
|
||||||
|
SendMsg(Context, {"Text": Caption})
|
||||||
except Exception:
|
except Exception:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def cUnsplash(update:Update, context:CallbackContext) -> None:
|
def cUnsplash(Context, Data=None) -> None:
|
||||||
Cmd = HandleCmd(update)
|
|
||||||
if not Cmd: return
|
|
||||||
try:
|
try:
|
||||||
Req = HttpGet(f'https://source.unsplash.com/random/?{UrlParse.quote(Cmd.Body)}')
|
Req = HttpGet(f'https://source.unsplash.com/random/?{UrlParse.quote(Data.Body)}')
|
||||||
update.message.reply_photo(
|
SendMsg(Context, {"Text": MarkdownCode(Req.geturl().split('?')[0], True), "Media": Req.read()})
|
||||||
Req.read(),
|
|
||||||
caption=MarkdownCode(Req.geturl().split('?')[0], True),
|
|
||||||
parse_mode='MarkdownV2',
|
|
||||||
reply_to_message_id=update.message.message_id)
|
|
||||||
except Exception:
|
except Exception:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def cSafebooru(update:Update, context:CallbackContext) -> None:
|
def cSafebooru(Context, Data=None) -> None:
|
||||||
Cmd = HandleCmd(update)
|
|
||||||
if not Cmd: return
|
|
||||||
ApiUrl = 'https://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags='
|
ApiUrl = 'https://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags='
|
||||||
try:
|
try:
|
||||||
if Cmd.Body:
|
if Data.Body:
|
||||||
for i in range(7):
|
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:
|
if ImgUrls:
|
||||||
break
|
break
|
||||||
if not ImgUrls:
|
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)
|
ImgXml = choice(ImgUrls)
|
||||||
ImgUrl = ImgXml.split('"')[0]
|
ImgUrl = ImgXml.split('"')[0]
|
||||||
ImgId = ImgXml.split(' id="')[1].split('"')[0]
|
ImgId = ImgXml.split(' id="')[1].split('"')[0]
|
||||||
@ -169,11 +156,7 @@ def cSafebooru(update:Update, context:CallbackContext) -> None:
|
|||||||
ImgId = ImgUrl.split('?')[-1]
|
ImgId = ImgUrl.split('?')[-1]
|
||||||
break
|
break
|
||||||
if ImgUrl:
|
if ImgUrl:
|
||||||
update.message.reply_photo(
|
SendMsg(Context, {"Text": (f'`{ImgId}`\n' + MarkdownCode(ImgUrl, True)), "Media": HttpGet(ImgUrl).read()})
|
||||||
HttpGet(ImgUrl).read(),
|
|
||||||
caption=f'`{ImgId}`\n' + MarkdownCode(ImgUrl, True),
|
|
||||||
parse_mode='MarkdownV2',
|
|
||||||
reply_to_message_id=update.message.message_id)
|
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
except Exception:
|
except Exception:
|
||||||
|
47
WinDog.py
47
WinDog.py
@ -21,7 +21,7 @@ from urllib import parse as UrlParse
|
|||||||
from urllib.request import urlopen, Request
|
from urllib.request import urlopen, Request
|
||||||
|
|
||||||
# <https://daringfireball.net/projects/markdown/syntax#backslash>
|
# <https://daringfireball.net/projects/markdown/syntax#backslash>
|
||||||
MdEscapes = '\\`*_{}[]()<>#+-.!'
|
MdEscapes = '\\`*_{}[]()<>#=+-.!|'
|
||||||
|
|
||||||
Db = {"Chats": {}}
|
Db = {"Chats": {}}
|
||||||
Locale = {"Fallback": {}}
|
Locale = {"Fallback": {}}
|
||||||
@ -49,12 +49,12 @@ Endpoints = {
|
|||||||
#"floor": multifun,
|
#"floor": multifun,
|
||||||
#"hands": multifun,
|
#"hands": multifun,
|
||||||
#"sessocto": multifun,
|
#"sessocto": multifun,
|
||||||
#"hash": cHash,
|
"hash": cHash,
|
||||||
#"eval": cEval,
|
#"eval": cEval,
|
||||||
#"exec": cExec,
|
#"exec": cExec,
|
||||||
#"web": cWeb,
|
"web": cWeb,
|
||||||
#"unsplash": cUnsplash,
|
"unsplash": cUnsplash,
|
||||||
#"safebooru": cSafebooru,
|
"safebooru": cSafebooru,
|
||||||
}
|
}
|
||||||
|
|
||||||
def SetupLocale() -> None:
|
def SetupLocale() -> None:
|
||||||
@ -98,6 +98,12 @@ def SetupDb() -> None:
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def InDict(Dict:dict, Key:str):
|
||||||
|
if Key in Dict:
|
||||||
|
return Dict[Key]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def CharEscape(String:str, Escape:str='') -> str:
|
def CharEscape(String:str, Escape:str='') -> str:
|
||||||
if Escape == 'MARKDOWN':
|
if Escape == 'MARKDOWN':
|
||||||
return escape_markdown(String, version=2)
|
return escape_markdown(String, version=2)
|
||||||
@ -216,19 +222,33 @@ def SendMsg(Context, Data):
|
|||||||
Manager = Context['Manager'] if 'Manager' in Context else None
|
Manager = Context['Manager'] if 'Manager' in Context else None
|
||||||
else:
|
else:
|
||||||
[Event, Manager] = [Context, Context]
|
[Event, Manager] = [Context, Context]
|
||||||
|
if InDict(Data, 'Text'):
|
||||||
TextPlain = MdToTxt(Data['Text'])
|
TextPlain = MdToTxt(Data['Text'])
|
||||||
|
TextMarkdown = CharEscape(HtmlUnescape(Data['Text']), InferMdEscape(HtmlUnescape(Data['Text']), TextPlain))
|
||||||
if isinstance(Manager, mastodon.Mastodon):
|
if isinstance(Manager, mastodon.Mastodon):
|
||||||
|
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(
|
Manager.status_post(
|
||||||
(TextPlain + '\n\n@' + Event['account']['acct']),
|
status=(TextPlain + '\n\n@' + Event['account']['acct']),
|
||||||
|
media_ids=(Media if Data['Media'] else None),
|
||||||
in_reply_to_id=Event['status']['id'],
|
in_reply_to_id=Event['status']['id'],
|
||||||
visibility=('direct' if Event['status']['visibility'] == 'direct' else 'unlisted')
|
visibility=('direct' if Event['status']['visibility'] == 'direct' else 'unlisted'),
|
||||||
)
|
)
|
||||||
elif isinstance(Manager, telegram.Update):
|
elif isinstance(Manager, telegram.Update):
|
||||||
|
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(
|
Event.message.reply_markdown_v2(
|
||||||
CharEscape(HtmlUnescape(Data['Text']), InferMdEscape(HtmlUnescape(Data['Text']), TextPlain)),
|
TextMarkdown,
|
||||||
#Event.message.reply_text(
|
reply_to_message_id=Event.message.message_id,
|
||||||
#TextPlain,
|
|
||||||
reply_to_message_id=Event.message.message_id
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def Main() -> None:
|
def Main() -> None:
|
||||||
@ -245,12 +265,9 @@ def Main() -> None:
|
|||||||
dispatcher.add_handler(CommandHandler('help', cHelp))
|
dispatcher.add_handler(CommandHandler('help', cHelp))
|
||||||
dispatcher.add_handler(CommandHandler('source', cSource))
|
dispatcher.add_handler(CommandHandler('source', cSource))
|
||||||
#dispatcher.add_handler(CommandHandler('time', cTime))
|
#dispatcher.add_handler(CommandHandler('time', cTime))
|
||||||
dispatcher.add_handler(CommandHandler('hash', cHash))
|
|
||||||
dispatcher.add_handler(CommandHandler('eval', cEval))
|
dispatcher.add_handler(CommandHandler('eval', cEval))
|
||||||
dispatcher.add_handler(CommandHandler('exec', cExec))
|
dispatcher.add_handler(CommandHandler('exec', cExec))
|
||||||
dispatcher.add_handler(CommandHandler('web', cWeb))
|
#dispatcher.add_handler(CommandHandler('web', cWeb))
|
||||||
dispatcher.add_handler(CommandHandler('unsplash', cUnsplash))
|
|
||||||
dispatcher.add_handler(CommandHandler('safebooru', cSafebooru))
|
|
||||||
|
|
||||||
for Cmd in ('hug', 'pat', 'poke', 'cuddle', 'floor', 'hands', 'sessocto'):
|
for Cmd in ('hug', 'pat', 'poke', 'cuddle', 'floor', 'hands', 'sessocto'):
|
||||||
dispatcher.add_handler(CommandHandler(Cmd, multifun))
|
dispatcher.add_handler(CommandHandler(Cmd, multifun))
|
||||||
|
Reference in New Issue
Block a user