parent
5c4e9067a0
commit
19081809f9
2 changed files with 27 additions and 16 deletions
|
|
@ -3,3 +3,5 @@ jid = angel@example.com
|
|||
password = b0TPA55W0rD
|
||||
autojoin = room1@muc.example.com room2@muc.example.com room3@muc.example.com
|
||||
nick = angel-from-underworld
|
||||
youtube_links = www.youtube.com m.youtube.com
|
||||
invidious_instances = invidious.kalli.st
|
||||
|
|
|
|||
41
main.py
41
main.py
|
|
@ -29,14 +29,10 @@ headers = {
|
|||
"Cache-Control": "no-cache",
|
||||
}
|
||||
|
||||
youtube_links = ["www.youtube.com", "m.youtube.com"]
|
||||
|
||||
youtube_link = "youtu.be"
|
||||
|
||||
ydl = youtube_dl.YoutubeDL()
|
||||
|
||||
invidious_instances = ["invidious.kalli.st"]
|
||||
|
||||
block_list = ("localhost", "127.0.0.1", "0.0.0.0")
|
||||
|
||||
req_list = ("http://", "https://")
|
||||
|
|
@ -69,13 +65,6 @@ def get_youtube_title(url):
|
|||
return None
|
||||
|
||||
|
||||
def get_invidious_link(yurl):
|
||||
"""Get an invidious link from a youtube link."""
|
||||
video = yurl.split("/")[-1]
|
||||
instance = random.choice(invidious_instances)
|
||||
return f"https://{instance}/watch?v={video}"
|
||||
|
||||
|
||||
def get_yurl(path):
|
||||
"""Get a youtube link from a path."""
|
||||
yurl = f"https://youtu.be/{path}"
|
||||
|
|
@ -99,6 +88,13 @@ class AngelBot(ClientXMPP):
|
|||
urls = [u for u in str_list if any(r in u for r in req_list)]
|
||||
return urls
|
||||
|
||||
def get_invidious_link(self, yurl):
|
||||
"""Get an invidious link from a youtube link."""
|
||||
video = yurl.split("/")[-1]
|
||||
instance = random.choice(self.invidious_instances)
|
||||
return f"https://{instance}/watch?v={video}"
|
||||
|
||||
|
||||
def send_youtube_info(self, uri, sender, mtype):
|
||||
"""Send youtube info to the sender."""
|
||||
yurl = None
|
||||
|
|
@ -110,7 +106,7 @@ class AngelBot(ClientXMPP):
|
|||
else:
|
||||
return
|
||||
|
||||
invidious = get_invidious_link(yurl)
|
||||
invidious = self.get_invidious_link(yurl)
|
||||
|
||||
if output := get_youtube_title(invidious):
|
||||
if output in self.messages[sender]["previews"]:
|
||||
|
|
@ -123,7 +119,7 @@ class AngelBot(ClientXMPP):
|
|||
async def parse_uri(self, uri, sender, mtype):
|
||||
"""Parse a uri and send the result to the sender."""
|
||||
netloc = uri.netloc
|
||||
if netloc in (youtube_links + [youtube_link]):
|
||||
if self.invidious_instances and netloc in (self.youtube_links + [youtube_link]):
|
||||
self.send_youtube_info(uri, sender, mtype)
|
||||
elif netloc.split(":")[0] in block_list:
|
||||
return
|
||||
|
|
@ -244,12 +240,17 @@ class AngelBot(ClientXMPP):
|
|||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def __init__(self, jid, password, nick="angel", autojoin=None):
|
||||
def __init__(self, jid, password, nick="angel", autojoin=None,
|
||||
youtube_links=None,
|
||||
invidious_instances=None):
|
||||
"""Initialize the bot."""
|
||||
ClientXMPP.__init__(self, jid, password)
|
||||
self.jid = jid
|
||||
self.nick = nick
|
||||
self.autojoin = autojoin or []
|
||||
self.invidious_instances = invidious_instances or []
|
||||
self.youtube_links = youtube_links or []
|
||||
|
||||
self.register_plugin("xep_0030")
|
||||
self.register_plugin("xep_0060")
|
||||
self.register_plugin("xep_0054")
|
||||
|
|
@ -361,9 +362,17 @@ if __name__ == "__main__":
|
|||
config.read("config.ini")
|
||||
jid = config["angel"]["jid"]
|
||||
password = config["angel"]["password"]
|
||||
autojoin = config["angel"]["autojoin"].split()
|
||||
autojoin = config["angel"].get("autojoin", "").split()
|
||||
nick = config["angel"]["nick"]
|
||||
bot = AngelBot(jid, password, nick=nick, autojoin=autojoin)
|
||||
youtube_links = config["angel"].get("youtube_links", "").split()
|
||||
|
||||
invidious_instances = config["angel"].get(
|
||||
"invidious_instances", ""
|
||||
).split()
|
||||
|
||||
bot = AngelBot(jid, password, nick=nick, autojoin=autojoin,
|
||||
youtube_links=youtube_links,
|
||||
invidious_instances=invidious_instances)
|
||||
|
||||
bot.connect()
|
||||
bot.process(forever=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue