fix youtube titling failing sometimes due to youtube being a f*in annoying, also should be faster by eliminating unnecessary request

This commit is contained in:
jan6 2023-05-08 09:15:23 -05:00
parent ce4ad587c3
commit a84639237e
3 changed files with 9 additions and 8 deletions

1
bot.py
View File

@ -1,3 +1,4 @@
#!/home/jan6/src/bot6/venv/bin/python3
#!/usr/bin/env python3
import ircstates, socket, ssl

View File

@ -161,7 +161,7 @@ class Command:
cmd = cmd[3:]
# cmd=cmd.split()[0]
urls = YouTube.match_urls(YouTube, cmd)
yt_failed=False
yt_failed = False
for video in urls:
if yt_failed == True:
yt_failed = False

View File

@ -46,14 +46,19 @@ class YouTube:
class parseprop(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag != "meta" or [i for i in attrs if "itemprop" in i] == []:
if (tag != "meta" and tag != "link") or [
i for i in attrs if "itemprop" in i
] == []:
return
global z
# print(self,tag,attrs)
for k, v in attrs:
if k == "itemprop":
if v not in ["name", "duration", "uploadDate", "channelId"]:
if v not in ["name", "duration", "uploadDate"]:
return
x = [v]
if tag == "link" and v == "name":
x = ["channelName"]
elif k == "content":
x += [v]
z.update({x[0]: x[1]})
@ -101,12 +106,7 @@ class YouTube:
print(ansi_string)
return irc_string, True
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")
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']}"
print(ansi_string)