The Metwo Syndrome
Signed-off-by: Aoi K <koizumi.aoi@chaotic.ninja> git-svn-id: file:///srv/svn/repo/chen/trunk@15 32723744-9b23-0b4a-b1da-9b2e968f9461
This commit is contained in:
parent
4526bef983
commit
e0b4ca1eb4
13
COPYING
13
COPYING
@ -1,9 +1,16 @@
|
||||
Discordian Public License (DPL)
|
||||
|
||||
All Rites Reversed Ⓚ (3188) (czar) <czar at kalli dot st>
|
||||
All Rites Reversed (ĸ) 3189 Aoi K. <koizumi dot aoi at chaotic dot ninja>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
Permission is hereby granted, to any person obtaining a copy of this
|
||||
material without restriction, including but not limited the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the material, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
YOU AGREE THAT THERE IS NO GODDESS BUT GODDESS AND SHE IS YOUR GODDESS &
|
||||
THAT THERE IS NO ERISIAN MOVEMENT BUT THE ERISIAN MOVEMENT AND IT IS THE
|
||||
ERISIAN MOVEMENT.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
HAIL ERIS!
|
||||
FIVE TONS OF FLAX
|
@ -1,7 +1,8 @@
|
||||
#+TITLE: Angel XMPP bot
|
||||
#+AUTHOR: czar
|
||||
#+TITLE: Chen XMPP bot
|
||||
#+AUTHOR: Aoi K.
|
||||
|
||||
XMPP bot to preview links and file contents
|
||||
XMPP bot to preview links and file contents. This may be identified as [[https://gt.kalli.st/czar/angel][angel]]'s
|
||||
younger sibling.
|
||||
|
||||
* Configuration
|
||||
copy the =config.ini.default= to =.config.ini= and set your variables accordly
|
||||
|
92
main.py
92
main.py
@ -1,6 +1,5 @@
|
||||
import requests
|
||||
import bs4
|
||||
import youtube_dl
|
||||
import random
|
||||
import configparser
|
||||
import re
|
||||
@ -30,18 +29,8 @@ headers = {
|
||||
"Cache-Control": "no-cache",
|
||||
}
|
||||
|
||||
youtube_links = ["www.youtube.com", "m.youtube.com"]
|
||||
|
||||
youtube_link = "youtu.be"
|
||||
|
||||
ydl = youtube_dl.YoutubeDL()
|
||||
|
||||
invidious_instances = ["invidious.snopyta.org"]
|
||||
|
||||
block_list = ("localhost", "127.0.0.1", "0.0.0.0")
|
||||
|
||||
req_list = ("http://", "https://")
|
||||
|
||||
html_files = ("text/html", "application/xhtml+xml")
|
||||
|
||||
|
||||
@ -60,26 +49,7 @@ class Lifo(list):
|
||||
self.pop()
|
||||
|
||||
|
||||
def get_youtube_title(url):
|
||||
try:
|
||||
info = ydl.extract_info(url, download=False)
|
||||
return info["title"]
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
|
||||
def get_invidious_link(yurl):
|
||||
video = yurl.split("/")[-1]
|
||||
instance = random.choice(invidious_instances)
|
||||
return f"https://{instance}/watch?v={video}"
|
||||
|
||||
|
||||
def get_yurl(path):
|
||||
yurl = f"https://youtu.be/{path}"
|
||||
return yurl
|
||||
|
||||
|
||||
class AngelBot(ClientXMPP):
|
||||
class ChenBot(ClientXMPP):
|
||||
messages = defaultdict(
|
||||
lambda: {
|
||||
"messages": Lifo(100),
|
||||
@ -93,28 +63,9 @@ class AngelBot(ClientXMPP):
|
||||
urls = [u for u in str_list if any(r in u for r in req_list)]
|
||||
return urls
|
||||
|
||||
def send_youtube_info(self, uri, sender, mtype):
|
||||
if uri.netloc == youtube_link:
|
||||
yurl = get_yurl(uri.path)
|
||||
elif "v" in (query := parse_qs(uri.query)):
|
||||
if v := query["v"]:
|
||||
yurl = get_yurl(v[0])
|
||||
else:
|
||||
return
|
||||
if output := get_youtube_title(yurl):
|
||||
if output in self.messages[sender]["previews"]:
|
||||
return
|
||||
self.messages[sender]["previews"].add(output)
|
||||
|
||||
invidious = get_invidious_link(yurl)
|
||||
self.send_message(mto=sender, mbody=f"*{output}*", mtype=mtype)
|
||||
self.send_message(mto=sender, mbody=invidious, mtype=mtype)
|
||||
|
||||
async def parse_uri(self, uri, sender, mtype):
|
||||
netloc = uri.netloc
|
||||
if netloc in (youtube_links + [youtube_link]):
|
||||
self.send_youtube_info(uri, sender, mtype)
|
||||
elif netloc.split(":")[0] in block_list:
|
||||
if netloc.split(":")[0] in block_list:
|
||||
return
|
||||
else:
|
||||
await self.process_link(uri, sender, mtype)
|
||||
@ -219,7 +170,7 @@ class AngelBot(ClientXMPP):
|
||||
except Exception:
|
||||
return
|
||||
|
||||
def __init__(self, jid, password, nick="angel", autojoin=None):
|
||||
def __init__(self, jid, password, nick="Chen", autojoin=None):
|
||||
ClientXMPP.__init__(self, jid, password)
|
||||
self.jid = jid
|
||||
self.nick = nick
|
||||
@ -251,36 +202,10 @@ class AngelBot(ClientXMPP):
|
||||
...
|
||||
|
||||
async def update_info(self):
|
||||
with open("angel.png", "rb") as avatar_file:
|
||||
avatar = avatar_file.read()
|
||||
|
||||
avatar_type = "image/png"
|
||||
avatar_id = self.plugin["xep_0084"].generate_id(avatar)
|
||||
avatar_bytes = len(avatar)
|
||||
|
||||
info = {
|
||||
"id": avatar_id,
|
||||
"type": avatar_type,
|
||||
"bytes": avatar_bytes,
|
||||
}
|
||||
|
||||
vcard = self.plugin["xep_0054"].make_vcard()
|
||||
|
||||
vcard["URL"] = "https://gt.kalli.st/czar/angel"
|
||||
|
||||
vcard["URL"] = "https://gt.kalli.st/novaburst/chen"
|
||||
asyncio.gather(self.plugin["xep_0054"].publish_vcard(vcard))
|
||||
|
||||
asyncio.gather(self.plugin["xep_0084"].publish_avatar(avatar))
|
||||
|
||||
asyncio.gather(
|
||||
self.plugin["xep_0153"].set_avatar(
|
||||
avatar=avatar,
|
||||
mtype=avatar_type,
|
||||
)
|
||||
)
|
||||
|
||||
asyncio.gather(self.plugin["xep_0084"].publish_avatar_metadata([info]))
|
||||
|
||||
async def message(self, msg):
|
||||
if msg["type"] in ("chat", "normal"):
|
||||
mtype = "chat"
|
||||
@ -323,11 +248,10 @@ class AngelBot(ClientXMPP):
|
||||
if __name__ == "__main__":
|
||||
config = configparser.ConfigParser()
|
||||
config.read("config.ini")
|
||||
jid = config["angel"]["jid"]
|
||||
password = config["angel"]["password"]
|
||||
autojoin = config["angel"]["autojoin"].split()
|
||||
|
||||
bot = AngelBot(jid, password, autojoin=autojoin)
|
||||
jid = config["chen"]["jid"]
|
||||
password = config["chen"]["password"]
|
||||
autojoin = config["chen"]["autojoin"].split()
|
||||
bot = ChenBot(jid, password, autojoin=autojoin)
|
||||
|
||||
bot.connect()
|
||||
bot.process(forever=True)
|
||||
|
@ -2,6 +2,5 @@ requests
|
||||
slixmpp
|
||||
beautifulsoup4
|
||||
git+https://github.com/GillesArcas/PythonSed#egg=PythonSed
|
||||
youtube-dl
|
||||
pantomime
|
||||
aiohttp
|
||||
|
Loading…
x
Reference in New Issue
Block a user