mirror of
https://gitlab.com/octospacc/WinDog.git
synced 2025-06-05 22:09:20 +02:00
More work on Matrix, move commands to new HTML locales, fix Mastodon
This commit is contained in:
@ -10,19 +10,18 @@ MicrosoftBingSettings = {}
|
||||
""" # end windog config # """
|
||||
|
||||
from urlextract import URLExtract
|
||||
from urllib import parse as UrlParse
|
||||
from urllib.request import urlopen, Request
|
||||
|
||||
def HttpReq(url:str, method:str|None=None, *, body:bytes=None, headers:dict[str, str]={"User-Agent": WebUserAgent}):
|
||||
return urlopen(Request(url, method=method, data=body, headers=headers))
|
||||
|
||||
def cEmbedded(context:EventContext, data:InputMessageData) -> None:
|
||||
if len(data.Tokens) >= 2:
|
||||
if len(data.command.tokens) >= 2:
|
||||
# Find links in command body
|
||||
Text = (data.TextMarkdown + ' ' + data.TextPlain)
|
||||
elif data.Quoted and data.Quoted.Text:
|
||||
Text = (data.text_markdown + ' ' + data.text_plain)
|
||||
elif data.quoted and data.quoted.text_auto:
|
||||
# Find links in quoted message
|
||||
Text = (data.Quoted.TextMarkdown + ' ' + data.Quoted.TextPlain)
|
||||
Text = (data.quoted.text_markdown + ' ' + data.quoted.text_plain)
|
||||
else:
|
||||
# TODO Error message
|
||||
return
|
||||
@ -41,38 +40,37 @@ def cEmbedded(context:EventContext, data:InputMessageData) -> None:
|
||||
url = "https://hlb0.octt.eu.org/cors-main.php/https://" + url
|
||||
proto = ''
|
||||
else:
|
||||
if urlDomain == "instagram.com":
|
||||
if urlDomain in ("instagram.com", "www.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):]
|
||||
url = (urlDomain + '/' + '/'.join(url.split('/')[1:]))
|
||||
SendMessage(context, {"TextPlain": f"{{{proto}{url}}}"})
|
||||
# else TODO error message?
|
||||
|
||||
def cWeb(context:EventContext, data:InputMessageData) -> None:
|
||||
if data.Body:
|
||||
try:
|
||||
QueryUrl = UrlParse.quote(data.Body)
|
||||
Req = HttpReq(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
|
||||
SendMessage(context, {"TextPlain": f'{Caption}...'})
|
||||
except Exception:
|
||||
raise
|
||||
else:
|
||||
pass
|
||||
if not (query := data.command.body):
|
||||
return # TODO show message
|
||||
try:
|
||||
QueryUrl = urlparse.quote(query)
|
||||
Req = HttpReq(f'https://html.duckduckgo.com/html?q={QueryUrl}')
|
||||
Caption = f'🦆🔎 "{query}": 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 = html_unescape(Title[1].strip())
|
||||
Caption += f'[{Index}] {Title} : {{{Link}}}\n\n'
|
||||
else:
|
||||
continue
|
||||
SendMessage(context, {"TextPlain": f'{Caption}...'})
|
||||
except Exception:
|
||||
raise
|
||||
|
||||
def cImages(context:EventContext, data:InputMessageData) -> None:
|
||||
pass
|
||||
@ -82,57 +80,57 @@ def cNews(context:EventContext, data:InputMessageData) -> None:
|
||||
|
||||
def cTranslate(context:EventContext, data:InputMessageData) -> None:
|
||||
language_to = data.command.arguments["language_to"]
|
||||
text_input = (data.command.body or (data.Quoted and data.Quoted.Body))
|
||||
text_input = (data.command.body or (data.quoted and data.quoted.text_plain))
|
||||
if not (text_input and language_to):
|
||||
return SendMessage(context, {"TextPlain": f"Usage: /translate <to language> <text>"})
|
||||
try:
|
||||
# TODO: Use many different public Lingva instances in rotation to avoid overloading a specific one
|
||||
result = json.loads(HttpReq(f'https://lingva.ml/api/v1/auto/{language_to}/{UrlParse.quote(text_input)}').read())
|
||||
result = json.loads(HttpReq(f'https://lingva.ml/api/v1/auto/{language_to}/{urlparse.quote(text_input)}').read())
|
||||
SendMessage(context, {"TextPlain": f"[{result['info']['detectedSource']} (auto) -> {language_to}]\n\n{result['translation']}"})
|
||||
except Exception:
|
||||
raise
|
||||
|
||||
# unsplash source appears to be deprecated! <https://old.reddit.com/r/unsplash/comments/s13x4h/what_happened_to_sourceunsplashcom/l65epl8/>
|
||||
def cUnsplash(context:EventContext, data:InputMessageData) -> None:
|
||||
try:
|
||||
Req = HttpReq(f'https://source.unsplash.com/random/?{UrlParse.quote(data.Body)}')
|
||||
ImgUrl = Req.geturl().split('?')[0]
|
||||
SendMessage(context, {
|
||||
"TextPlain": f'{{{ImgUrl}}}',
|
||||
"TextMarkdown": MarkdownCode(ImgUrl, True),
|
||||
"Media": Req.read(),
|
||||
})
|
||||
except Exception:
|
||||
raise
|
||||
#def cUnsplash(context:EventContext, data:InputMessageData) -> None:
|
||||
# try:
|
||||
# Req = HttpReq(f'https://source.unsplash.com/random/?{urlparse.quote(data.command.body)}')
|
||||
# ImgUrl = Req.geturl().split('?')[0]
|
||||
# SendMessage(context, {
|
||||
# "TextPlain": f'{{{ImgUrl}}}',
|
||||
# "TextMarkdown": MarkdownCode(ImgUrl, True),
|
||||
# "Media": Req.read(),
|
||||
# })
|
||||
# except Exception:
|
||||
# raise
|
||||
|
||||
def cSafebooru(context:EventContext, data:InputMessageData) -> None:
|
||||
ApiUrl = 'https://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags='
|
||||
try:
|
||||
if data.Body:
|
||||
img_id, img_url = None, None
|
||||
if (query := data.command.body):
|
||||
for i in range(7): # retry a bunch of times if we can't find a really random result
|
||||
ImgUrls = HttpReq(f'{ApiUrl}md5:{RandHexStr(3)}%20{UrlParse.quote(data.Body)}').read().decode().split(' file_url="')[1:]
|
||||
ImgUrls = HttpReq(f'{ApiUrl}md5:{RandHexStr(3)}%20{urlparse.quote(query)}').read().decode().split(' file_url="')[1:]
|
||||
if ImgUrls:
|
||||
break
|
||||
if not ImgUrls: # literal search
|
||||
ImgUrls = HttpReq(f'{ApiUrl}{UrlParse.quote(data.Body)}').read().decode().split(' file_url="')[1:]
|
||||
ImgUrls = HttpReq(f'{ApiUrl}{urlparse.quote(query)}').read().decode().split(' file_url="')[1:]
|
||||
if not ImgUrls:
|
||||
return SendMessage(context, {"Text": "Error: Could not get any result from Safebooru."})
|
||||
ImgXml = choice(ImgUrls)
|
||||
ImgUrl = ImgXml.split('"')[0]
|
||||
ImgId = ImgXml.split(' id="')[1].split('"')[0]
|
||||
img_url = ImgXml.split('"')[0]
|
||||
img_id = ImgXml.split(' id="')[1].split('"')[0]
|
||||
else:
|
||||
HtmlReq = HttpReq(HttpReq('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]
|
||||
img_url = Line.split(' src="')[1].split('"')[0]
|
||||
img_id = img_url.split('?')[-1]
|
||||
break
|
||||
if ImgUrl:
|
||||
SendMessage(context, {
|
||||
"TextPlain": f'[{ImgId}]\n{{{ImgUrl}}}',
|
||||
"TextMarkdown": (f'\\[`{ImgId}`\\]\n' + MarkdownCode(ImgUrl, True)),
|
||||
"media": {"url": ImgUrl}, #, "bytes": HttpReq(ImgUrl).read()},
|
||||
})
|
||||
if img_url:
|
||||
SendMessage(context, OutputMessageData(
|
||||
text_plain=f"[{img_id}]\n{{{img_url}}}",
|
||||
text_html=f"[<code>{img_id}</code>]\n<pre>{img_url}</pre>",
|
||||
media={"url": img_url}))
|
||||
else:
|
||||
pass
|
||||
except Exception as error:
|
||||
|
Reference in New Issue
Block a user