2021-10-05 13:18:25 +00:00
|
|
|
from html.parser import HTMLParser
|
|
|
|
from urllib.request import urlopen
|
|
|
|
|
|
|
|
|
|
|
|
class YouTube:
|
|
|
|
y, z = {}, {}
|
|
|
|
|
2022-05-23 17:05:10 +00:00
|
|
|
def mesg(self, msg, t=None):
|
|
|
|
self.util.mesg(msg, t)
|
|
|
|
|
2022-08-23 14:11:47 +00:00
|
|
|
def match_urls(self, str):
|
2022-05-23 17:05:10 +00:00
|
|
|
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
|
|
|
|
]
|
2022-08-23 14:11:47 +00:00
|
|
|
r = list(dict.fromkeys(r))
|
2022-05-23 17:05:10 +00:00
|
|
|
return r
|
|
|
|
|
|
|
|
def is_embed(str):
|
|
|
|
return str.startswith("https://www.youtube.com/embed/") or str.startswith(
|
|
|
|
"https://www.youtube-nocookie.com/embed/"
|
|
|
|
)
|
|
|
|
|
2021-10-05 13:18:25 +00:00
|
|
|
class parseprop(HTMLParser):
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
|
|
if tag != "meta" or [i for i in attrs if "itemprop" in i] == []:
|
|
|
|
return
|
|
|
|
global z
|
|
|
|
for k, v in attrs:
|
|
|
|
if k == "itemprop":
|
|
|
|
if v not in ["name", "duration", "uploadDate", "channelId"]:
|
|
|
|
return
|
|
|
|
x = [v]
|
|
|
|
elif k == "content":
|
|
|
|
x += [v]
|
|
|
|
z.update({x[0]: x[1]})
|
|
|
|
# print(x[0],"=",x[1])
|
|
|
|
|
|
|
|
def fmt_dur(dur):
|
|
|
|
h, m, s = 0, 0, 0
|
|
|
|
m = dur[2:].split("M")
|
|
|
|
s = int(m[1][:-1])
|
|
|
|
m = int(m[0])
|
|
|
|
if m >= 60:
|
|
|
|
h = m // 60
|
|
|
|
m = round((m / 60 - h) * 60)
|
|
|
|
return f"{h}h {m}m {s}s"
|
2022-02-07 16:28:19 +00:00
|
|
|
elif h == 0 and m == 0 and s == 0:
|
|
|
|
return "LIVE"
|
|
|
|
elif m == 0 and s != 0:
|
|
|
|
return f"{s}s"
|
2021-10-05 13:18:25 +00:00
|
|
|
else:
|
|
|
|
return f"{m}m {s}s"
|
|
|
|
|
|
|
|
def yt(self, url):
|
2022-05-23 17:05:10 +00:00
|
|
|
# self.util.mesg("hello")
|
2022-02-07 16:28:19 +00:00
|
|
|
url = url.rstrip("\x01")
|
2022-05-23 17:05:10 +00:00
|
|
|
if self.is_embed(url):
|
|
|
|
videoId = url.split("/")[4]
|
|
|
|
url = f"https://www.youtube.com/watch?v={videoId}"
|
2021-10-05 13:18:25 +00:00
|
|
|
global y, z
|
|
|
|
y, z = {}, {}
|
|
|
|
p = self.parseprop()
|
|
|
|
# url="https://www.youtube.com/watch?v=gyMpI8csWis"
|
|
|
|
data = urlopen(url).read().decode()
|
|
|
|
p.feed(data)
|
|
|
|
z.update({"duration": self.fmt_dur(z["duration"])})
|
|
|
|
url = f"https://www.youtube.com/channel/{z['channelId']}"
|
|
|
|
data = urlopen(url).read().decode()
|
|
|
|
y, z = z, {}
|
|
|
|
p.feed(data)
|
|
|
|
y.update({"channelName": z["name"]})
|
|
|
|
y.pop("channelId")
|
2021-11-24 21:44:42 +00:00
|
|
|
irc_string = f"[\x0303Youtube\x03] \x02{y['name']}\x02 ({y['duration']}) uploaded by \x1d{y['channelName']}\x1d on {y['uploadDate']}"
|
|
|
|
ansi_string = f"[\x1b[32mYoutube\x1b[0m] \x1b[1m{y['name']}\x1b[0m ({y['duration']}) uploaded by \x1b[03m{y['channelName']}\x1b[0m on {y['uploadDate']}"
|
2021-10-05 13:18:25 +00:00
|
|
|
print(ansi_string)
|
|
|
|
return irc_string
|
2022-08-23 14:11:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
|
|
|
|
|
|
|
YouTube.yt(YouTube, sys.argv[1])
|