Add months-old local changes, restructure platforms api and some modules, fix some bugs

This commit is contained in:
2024-06-15 02:08:09 +02:00
parent 2512df4f98
commit b7eb53e6a7
13 changed files with 393 additions and 332 deletions

0
ModWinDog/Codings.py Normal file
View File

15
ModWinDog/Hash.py Normal file
View File

@@ -0,0 +1,15 @@
import hashlib
# Module: Hash
# Responds with the hash-sum of a message received.
def cHash(Context, Data) -> None:
if len(Data.Tokens) >= 3 and Data.Tokens[1] in hashlib.algorithms_available:
Alg = Data.Tokens[1]
Hash = hashlib.new(Alg, Alg.join(Data.Body.split(Alg)[1:]).strip().encode()).hexdigest()
SendMsg(Context, {
"TextPlain": Hash,
"TextMarkdown": MarkdownCode(Hash, True),
})
else:
SendMsg(Context, {"Text": choice(Locale.__('hash.usage')).format(Data.Tokens[0], hashlib.algorithms_available)})

129
ModWinDog/Internet.py Normal file
View File

@@ -0,0 +1,129 @@
from urlextract import URLExtract
from urllib import parse as UrlParse
from urllib.request import urlopen, Request
def HttpGet(Url:str):
return urlopen(Request(Url, headers={"User-Agent": WebUserAgent}))
# Module: Embedded
# Rewrite a link trying to make sure we have an embed view.
def cEmbedded(Context, Data) -> None:
if len(Data.Tokens) >= 2:
# Find links in command body
Text = (Data.TextMarkdown + ' ' + Data.TextPlain)
elif Data.Quoted and Data.Quoted.Text:
# Find links in quoted message
Text = (Data.Quoted.TextMarkdown + ' ' + Data.Quoted.TextPlain)
else:
# TODO Error message
return
pass
urls = URLExtract().find_urls(Text)
if len(urls) > 0:
proto = 'https://'
url = urls[0]
urlLow = url.lower()
if urlLow.startswith('http://') or urlLow.startswith('https://'):
proto = url.split('://')[0] + '://'
url = '://'.join(url.split('://')[1:])
urlLow = '://'.join(urlLow.split('://')[1:])
urlDomain = urlLow.split('/')[0]
if urlDomain in ("facebook.com", "www.facebook.com", "m.facebook.com", "mbasic.facebook.com"):
url = "https://hlb0.octt.eu.org/cors-main.php/https://" + url
proto = ''
else:
if urlDomain == "instagram.com":
urlDomain = "ddinstagram.com"
elif urlDomain in ("twitter.com", "x.com"):
urlDomain = "fxtwitter.com"
elif urlDomain == "vm.tiktok.com":
urlDomain = "vm.vxtiktok.com"
url = urlDomain + url[len(urlDomain):]
SendMsg(Context, {"TextPlain": f"{{{proto}{url}}}"})
# else TODO error message?
# Module: Web
# Provides results of a DuckDuckGo search.
def cWeb(Context, Data) -> None:
if Data.Body:
try:
QueryUrl = UrlParse.quote(Data.Body)
Req = HttpGet(f'https://html.duckduckgo.com/html?q={QueryUrl}')
Caption = f'🦆🔎 "{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 = UrlParse.unquote(Line.split(' href="//duckduckgo.com/l/?uddg=')[1].split('&rut=')[0])
Title = Line.strip().split('</a>')[0].strip().split('</span>')[-1].strip().split('>')
if len(Title) > 1:
Title = HtmlUnescape(Title[1].strip())
Caption += f'[{Index}] {Title} : {{{Link}}}\n\n'
else:
continue
SendMsg(Context, {"TextPlain": f'{Caption}...'})
except Exception:
raise
else:
pass
# Module: Translate
# Return the received message after translating it in another language.
def cTranslate(Context, Data) -> None:
if len(Data.Tokens) < 3:
return
try:
Lang = Data.Tokens[1]
# TODO: Use many different public Lingva instances in rotation to avoid overloading a specific one
Result = json.loads(HttpGet(f'https://lingva.ml/api/v1/auto/{Lang}/{UrlParse.quote(Lang.join(Data.Body.split(Lang)[1:]))}').read())["translation"]
SendMsg(Context, {"TextPlain": Result})
except Exception:
raise
# Module: Unsplash
# Send a picture sourced from Unsplash.
def cUnsplash(Context, Data) -> None:
try:
Req = HttpGet(f'https://source.unsplash.com/random/?{UrlParse.quote(Data.Body)}')
ImgUrl = Req.geturl().split('?')[0]
SendMsg(Context, {
"TextPlain": f'{{{ImgUrl}}}',
"TextMarkdown": MarkdownCode(ImgUrl, True),
"Media": Req.read(),
})
except Exception:
raise
# Module: Safebooru
# Send a picture sourced from Safebooru.
def cSafebooru(Context, Data) -> None:
ApiUrl = 'https://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags='
try:
if Data.Body:
for i in range(7):
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(Data.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 '<img ' in Line and ' id="image" ' in Line and ' src="':
ImgUrl = Line.split(' src="')[1].split('"')[0]
ImgId = ImgUrl.split('?')[-1]
break
if ImgUrl:
SendMsg(Context, {
"TextPlain": f'[{ImgId}]\n{{{ImgUrl}}}',
"TextMarkdown": f'\\[`{ImgId}`\\]\n{MarkdownCode(ImgUrl, True)}',
"Media": HttpGet(ImgUrl).read(),
})
else:
pass
except Exception:
raise

View File

@@ -3,45 +3,36 @@
# Licensed under AGPLv3 by OctoSpacc #
# ==================================== #
from urlextract import URLExtract
# Module: Percenter
# Provides fun trough percentage-based toys.
def percenter(Context, Data=None) -> None:
if Data.Body:
Text = choice(Locale.__(f'{Data.Name}.done'))
else:
Text = choice(Locale.__(f'{Data.Name}.empty'))
SendMsg(Context, {"Text": Text.format(Cmd=Data.Tokens[0], Percent=RandPercent(), Thing=Data.Body)})
def percenter(Context, Data) -> None:
SendMsg(Context, {"Text": choice(Locale.__(f'{Data.Name}.{"done" if Data.Body else "empty"}')).format(
Cmd=Data.Tokens[0], Percent=RandPercent(), Thing=Data.Body)})
# Module: Multifun
# Provides fun trough preprogrammed-text-based toys.
def multifun(update:Update, context:CallbackContext) -> None:
Cmd = HandleCmd(update)
if not Cmd: return
Key = ParseCmd(update.message.text).Name
ReplyToMsg = update.message.message_id
if update.message.reply_to_message:
ReplyFromUID = update.message.reply_to_message.from_user.id
if ReplyFromUID == TelegramId and 'bot' in Locale.__(Key):
Text = CharEscape(choice(Locale.__(f'{Key}.bot')), 'MARKDOWN_SPEECH')
elif ReplyFromUID == update.message.from_user.id and 'self' in Locale.__(Key):
FromUName = CharEscape(update.message.from_user.first_name, 'MARKDOWN')
Text = CharEscape(choice(Locale.__(f'{Key}.self')), 'MARKDOWN_SPEECH').format(FromUName)
def multifun(Context, Data) -> None:
cmdkey = Data.Name
replyToId = None
if Data.Quoted:
replyFromUid = Data.Quoted.User["Id"]
# TODO work on all platforms for the bot id
if int(replyFromUid.split('@')[0]) == int(TelegramId) and 'bot' in Locale.__(cmdkey):
Text = choice(Locale.__(f'{cmdkey}.bot'))
elif replyFromUid == Data.User["Id"] and 'self' in Locale.__(cmdkey):
Text = choice(Locale.__(f'{cmdkey}.self')).format(Data.User["Name"])
else:
if 'others' in Locale.__(Key):
FromUName = CharEscape(update.message.from_user.first_name, 'MARKDOWN')
ToUName = CharEscape(update.message.reply_to_message.from_user.first_name, 'MARKDOWN')
Text = CharEscape(choice(Locale.__(f'{Key}.others')), 'MARKDOWN_SPEECH').format(FromUName,ToUName)
ReplyToMsg = update.message.reply_to_message.message_id
if 'others' in Locale.__(cmdkey):
Text = choice(Locale.__(f'{cmdkey}.others')).format(Data.User["Name"], Data.Quoted.User["Name"])
replyToId = Data.Quoted.messageId
else:
if 'empty' in Locale.__(Key):
Text = CharEscape(choice(Locale.__(f'{Key}.empty')), 'MARKDOWN_SPEECH')
update.message.reply_markdown_v2(Text, reply_to_message_id=ReplyToMsg)
if 'empty' in Locale.__(cmdkey):
Text = choice(Locale.__(f'{cmdkey}.empty'))
SendMsg(Context, {"Text": Text, "ReplyTo": replyToId})
# Module: Start
# Salutes the user, for now no other purpose except giving a feel that the bot is working.
def cStart(Context, Data=None) -> None:
def cStart(Context, Data) -> None:
SendMsg(Context, {"Text": choice(Locale.__('start')).format(Data.User['Name'])})
# Module: Help
@@ -55,16 +46,19 @@ def cHelp(Context, Data=None) -> None:
# Module: Source
# Provides a copy of the bot source codes and/or instructions on how to get it.
def cSource(Context, Data=None) -> None:
SendMsg(Context, {"TextPlain": "{https://gitlab.com/octospacc/WinDog}"})
SendMsg(Context, {"TextPlain": ("""\
* Original Source Code: {https://gitlab.com/octospacc/WinDog}
* Mirror: {https://github.com/octospacc/WinDog}
""" + (f"* Modified Source Code: {{{ModifiedSourceUrl}}}" if ModifiedSourceUrl else ""))})
# Module: Config
# ...
def cConfig(update:Update, context:CallbackContext) -> None:
Cmd = HandleCmd(update)
if not Cmd: return
# ... area: eu, us, ...
# ... language: en, it, ...
# ... userdata: import, export, delete
#def cConfig(update:telegram.Update, context:CallbackContext) -> None:
# Cmd = TelegramHandleCmd(update)
# if not Cmd: return
# # ... area: eu, us, ...
# # ... language: en, it, ...
# # ... userdata: import, export, delete
# Module: Ping
# Responds pong, useful for testing messaging latency.
@@ -73,7 +67,7 @@ def cPing(Context, Data=None) -> None:
# Module: Echo
# Responds back with the original text of the received message.
def cEcho(Context, Data=None) -> None:
def cEcho(Context, Data) -> None:
if Data.Body:
SendMsg(Context, {"Text": Data.Body})
else:
@@ -81,33 +75,21 @@ def cEcho(Context, Data=None) -> None:
# Module: Broadcast
# Sends an admin message over to another destination
def cBroadcast(Context, Data=None) -> None:
if len(Data.Tokens) >= 3 and Data.User['Id'] in AdminIds:
Dest = Data.Tokens[1]
Text = ' '.join(Data.Tokens[2:])
SendMsg(Context, {"TextPlain": Text}, Dest)
SendMsg(Context, {"TextPlain": "Executed."})
else:
SendMsg(Context, {"Text": choice(Locale.__('eval'))})
def cBroadcast(Context, Data) -> None:
if Data.User['Id'] not in AdminIds:
return SendMsg(Context, {"Text": choice(Locale.__('eval'))})
if len(Data.Tokens) < 3:
return SendMsg(Context, {"Text": "Bad usage."})
Dest = Data.Tokens[1]
Text = ' '.join(Data.Tokens[2:])
SendMsg(Context, {"TextPlain": Text}, Dest)
SendMsg(Context, {"TextPlain": "Executed."})
#def cTime(update:Update, context:CallbackContext) -> None:
# update.message.reply_markdown_v2(
# CharEscape(choice(Locale.__('time')).format(time.ctime().replace(' ', ' ')), 'MARKDOWN_SPEECH'),
# reply_to_message_id=update.message.message_id)
# Module: Hash
# Responds with the hash-sum of a message received.
def cHash(Context, Data=None) -> None:
if len(Data.Tokens) >= 3 and Data.Tokens[1] in hashlib.algorithms_available:
Alg = Data.Tokens[1]
Hash = hashlib.new(Alg, Alg.join(Data.Body.split(Alg)[1:]).strip().encode()).hexdigest()
SendMsg(Context, {
"TextPlain": Hash,
"TextMarkdown": MarkdownCode(Hash, True),
})
else:
SendMsg(Context, {"Text": choice(Locale.__('hash.usage')).format(Data.Tokens[0], hashlib.algorithms_available)})
# Module: Eval
# Execute a Python command (or safe literal operation) in the current context. Currently not implemented.
def cEval(Context, Data=None) -> None:
@@ -115,7 +97,7 @@ def cEval(Context, Data=None) -> None:
# Module: Exec
# Execute a system command from the allowed ones and return stdout/stderr.
def cExec(Context, Data=None) -> None:
def cExec(Context, Data) -> None:
if len(Data.Tokens) >= 2 and Data.Tokens[1].lower() in ExecAllowed:
Cmd = Data.Tokens[1].lower()
Out = subprocess.run(('sh', '-c', f'export PATH=$PATH:/usr/games; {Cmd}'), stdout=subprocess.PIPE).stdout.decode()
@@ -138,115 +120,3 @@ def cFormat(Context, Data=None) -> None:
def cFrame(Context, Data=None) -> None:
pass
# Module: Embedded
# Rewrite a link trying to make sure we have an embed view.
def cEmbedded(Context, Data=None) -> None:
if len(Data.Tokens) >= 2:
# Find links in command body
Text = Data.Body
elif Data.Quoted and Data.Quoted['Body']:
# Find links in quoted message
Text = Data.Quoted['Body']
else:
# Error
return
pass
Urls = URLExtract().find_urls(Text)
if len(Urls) > 0:
Proto = 'https://'
Url = Urls[0]
UrlLow = Url.lower()
if UrlLow.startswith('http://') or UrlLow.startswith('https://'):
Proto = Url.split('://')[0] + '://'
Url = '://'.join(Url.split('://')[1:])
UrlLow = '://'.join(UrlLow.split('://')[1:])
if UrlLow.startswith('facebook.com/') or UrlLow.startswith('www.facebook.com/') or UrlLow.startswith('m.facebook.com/') or UrlLow.startswith('mbasic.facebook.com/'):
Url = 'https://hlb0.octt.eu.org/cors-main.php/https://' + Url
Proto = ''
elif UrlLow.startswith('instagram.com/'):
Url = 'ddinstagram.com/' + Url[len('instagram.com/'):]
elif UrlLow.startswith('twitter.com/'):
Url = 'fxtwitter.com/' + Url[len('twitter.com/'):]
SendMsg(Context, {"TextPlain": Proto+Url})
# Module: Web
# Provides results of a DuckDuckGo search.
def cWeb(Context, Data=None) -> None:
if Data.Body:
try:
QueryUrl = UrlParse.quote(Data.Body)
Req = HttpGet(f'https://html.duckduckgo.com/html?q={QueryUrl}')
Caption = f'🦆🔎 "{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 = UrlParse.unquote(Line.split(' href="//duckduckgo.com/l/?uddg=')[1].split('&amp;rut=')[0])
Title = HtmlUnescape(Line.split('</a>')[0].split('</span>')[-1].split('>')[1])
Caption += f'[{Index}] {Title} : {{{Link}}}\n\n'
SendMsg(Context, {"TextPlain": f'{Caption}...'})
except Exception:
raise
else:
pass
# Module: Translate
# Return the received message after translating it in another language.
def cTranslate(Context, Data=None) -> None:
if Data.Body:
try:
Lang = Data.Tokens[1]
# TODO: Use many different public Lingva instances in rotation to avoid overloading a specific one
Result = json.loads(HttpGet(f'https://lingva.ml/api/v1/auto/{Lang}/{UrlParse.quote(Lang.join(Data.Body.split(Lang)[1:]))}').read())["translation"]
SendMsg(Context, {"TextPlain": Result})
except Exception:
raise
else:
pass
# Module: Unsplash
# Send a picture sourced from Unsplash.
def cUnsplash(Context, Data=None) -> None:
try:
Req = HttpGet(f'https://source.unsplash.com/random/?{UrlParse.quote(Data.Body)}')
ImgUrl = Req.geturl().split('?')[0]
SendMsg(Context, {
"TextPlain": f'{{{ImgUrl}}}',
"TextMarkdown": MarkdownCode(ImgUrl, True),
"Media": Req.read(),
})
except Exception:
raise
# Module: Safebooru
# Send a picture sourced from Safebooru.
def cSafebooru(Context, Data=None) -> None:
ApiUrl = 'https://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags='
try:
if Data.Body:
for i in range(7):
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(Data.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 '<img ' in Line and ' id="image" ' in Line and ' src="':
ImgUrl = Line.split(' src="')[1].split('"')[0]
ImgId = ImgUrl.split('?')[-1]
break
if ImgUrl:
SendMsg(Context, {
"TextPlain": f'[{ImgId}]\n{{{ImgUrl}}}',
"TextMarkdown": f'\\[`{ImgId}`\\]\n{MarkdownCode(ImgUrl, True)}',
"Media": HttpGet(ImgUrl).read(),
})
else:
pass
except Exception:
raise