I think this fixes youtube alternate urls, bonus: now embed links work also!

This commit is contained in:
jan6 2022-05-23 20:05:10 +03:00
parent 7c966381fe
commit d5f6f0a7bc
4 changed files with 45 additions and 28 deletions

View File

@ -1,7 +1,8 @@
from functools import wraps
from irctokens import build
from youtube import YouTube
import importlib
import sys
class Command:
@ -9,8 +10,8 @@ class Command:
self.config = config
self.commands = []
def mesg(self, msg):
self.util.mesg(msg)
def mesg(self, msg, t=None):
self.util.mesg(msg, t)
def notice(self, msg):
self.util.notice(msg)
@ -94,6 +95,7 @@ class Command:
def preq_cmd(self): # command prequisites / triggers
cmd = self.line
#self.mesg(f"attempting command: {cmd}")
if cmd == "help" or cmd.startswith("help "):
command = "help"
elif cmd.startswith("echo "):
@ -106,11 +108,7 @@ class Command:
command = "dbg"
elif cmd == "dbg2" or cmd.startswith("dbg2 "):
command = "dbg2"
elif (
"https://www.youtube.com/watch?v="
or "https://m.youtube.com/watch?v="
or "https://youtu.be/"
) in cmd or cmd.startswith("yt "):
elif cmd.startswith("yt ") or self.YouTube.match_urls(self.YouTube,cmd)!=[]:
command = "yt"
elif cmd.startswith("\x01") or self.is_ctcp:
command = "ctcp"
@ -151,19 +149,11 @@ class Command:
@cmd
def yt(self, prefix, cmd, pm, line, admin, mesg):
"""youtube"""
YouTube = self.YouTube
if cmd.startswith("yt "):
cmd = cmd[3:]
# cmd=cmd.split()[0]
urls = [
i
for i in cmd.split()
if i.startswith(
"https://www.youtube.com/watch?v="
or "https://m.youtube.com/watch?v="
or "https://youtu.be/"
)
]
# mesg(urls)
urls = YouTube.match_urls(YouTube,cmd)
for video in urls:
try:
a = YouTube.yt(YouTube, video)

View File

@ -14,7 +14,7 @@ class config:
# get password from secret file
nickserv_pass = open("pass.txt", "r").read().strip()
channel = "##jan6"
autojoin = ["##share", "#1337331", "#opsec"]
autojoin = ["##share", "#1337331"]
class admin:
accounts = ["jan6"]

View File

@ -2,6 +2,7 @@ import irctokens
from config import config as Config
from util import Util
from commands import Command
from youtube import YouTube
import sys, importlib, time
@ -28,7 +29,9 @@ def stuff(bot, sock):
config = importlib.reload(sys.modules["config"]).config
util = Util(config, sock)
command = importlib.reload(sys.modules["commands"]).Command(config)
command.YouTube = importlib.reload(sys.modules["youtube"]).YouTube
command.util = util
command.YouTube.util = util
prefixes = config.cmd.prefixes
admin_accounts = config.admin.accounts
admin_users = config.admin.hostmasks
@ -86,9 +89,11 @@ def stuff(bot, sock):
f"IDENTIFY {config.self.nick} {config.server.nickserv_pass}",
line.source.split("!")[0],
)
if line.command == "433": #433 is ERR_NICKNAMEINUSE
if line.command == "433": # 433 is ERR_NICKNAMEINUSE
util.nick(config.self.nick + "_")
if line.command == "376" or line.command == "422": #376 is RPL_ENDOFMOTD and 422 is ERR_NOMOTD
if (
line.command == "376" or line.command == "422"
): # 376 is RPL_ENDOFMOTD and 422 is ERR_NOMOTD
send(irctokens.build("CAP", ["LS", "302"]).format())
elif line.command == "CAP" and line.params[1] == "LS":
if server_caps == []:
@ -134,13 +139,9 @@ def stuff(bot, sock):
is_pm
or cmd.startswith(self_nick)
or cmd[0] in prefixes
or cmd.startswith("https://")
or (
"https://www.youtube.com/watch?v="
or "https://m.youtube.com/watch?v="
or "https://youtu.be/"
)
in cmd
or "https://" in cmd
or "http://" in cmd
# or cmd.startswith("https://")
):
try:
# if message in a channel, remove prefixes

View File

@ -5,6 +5,28 @@ from urllib.request import urlopen
class YouTube:
y, z = {}, {}
def mesg(self, msg, t=None):
self.util.mesg(msg, t)
def match_urls(self,str):
r = [
i
for i in str.split()
if "https://youtu.be/" in i
or "https://www.youtube.com/watch?v=" in i
or "https://m.youtube.com/watch?v=" in i
or "https://youtube.com/watch?v=" in i
or "https://www.youtube.com/embed/" in i
or "https://www.youtube-nocookie.com/embed/" in i
]
r=list(dict.fromkeys(r))
return r
def is_embed(str):
return str.startswith("https://www.youtube.com/embed/") or str.startswith(
"https://www.youtube-nocookie.com/embed/"
)
class parseprop(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag != "meta" or [i for i in attrs if "itemprop" in i] == []:
@ -37,7 +59,11 @@ class YouTube:
return f"{m}m {s}s"
def yt(self, url):
# self.util.mesg("hello")
url = url.rstrip("\x01")
if self.is_embed(url):
videoId = url.split("/")[4]
url = f"https://www.youtube.com/watch?v={videoId}"
global y, z
y, z = {}, {}
p = self.parseprop()