From 954f2085beb3c4948c1ea9d31f2f2145528d2225 Mon Sep 17 00:00:00 2001 From: jan6 Date: Wed, 9 Aug 2023 20:11:11 -0500 Subject: [PATCH] fixed some edge-case where title might be wrongly set to "true" --- youtube.py | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/youtube.py b/youtube.py index 6d3c85a..bc70d47 100644 --- a/youtube.py +++ b/youtube.py @@ -4,7 +4,7 @@ from urllib.request import urlopen class YouTube: y, z = {}, {} # empty on every invokation - video_type="" + video_type = "" def mesg(self, msg, t=None): self.util.mesg(msg, t) @@ -56,14 +56,19 @@ class YouTube: def __init__(self): print("yt parse init") HTMLParser.__init__(self) - self.itemprops_list=["name", "duration", "uploadDate", "interactionCount"] - if YouTube.video_type=="clip": - self.itemprops_list+=["description"] + self.itemprops_list = ["name", "duration", "uploadDate", "interactionCount"] + if YouTube.video_type == "clip": + self.itemprops_list += ["description"] print("it is a clip!") + def handle_starttag(self, tag, attrs): - if (tag != "meta" and tag != "link") 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] == [] + and ("name", "title") not in attrs + ) + or (tag == "meta" and ("itemprop", "name") in attrs) + ): return global z # print(self,tag,attrs) @@ -78,6 +83,10 @@ class YouTube: if attrs[0][1] == "interactionCount": v = int(v) x += [v] + elif k == "name" and v == "title": + x = [v] + else: + return z.update({x[0]: x[1]}) # print(x[0],"=",x[1]) @@ -102,21 +111,27 @@ class YouTube: def yt(self, url): # self.util.mesg("dbg hello") url = url.rstrip("\x01") - self.video_type=("clip" if self.is_clip(url) else - "shorts" if self.is_ytshorts(url) else - "music" if self.is_ytmusic(url) else - "embed" if self.is_embed(url) else - "video") - video_type=self.video_type - if video_type=="embed": + self.video_type = ( + "clip" + if self.is_clip(url) + else "shorts" + if self.is_ytshorts(url) + else "music" + if self.is_ytmusic(url) + else "embed" + if self.is_embed(url) + else "video" + ) + video_type = self.video_type + if video_type == "embed": videoId = url.split("/")[4] url = f"https://www.youtube.com/watch?v={videoId}" - elif video_type=="music": + elif video_type == "music": for i in url.split("?")[1].split("&"): if i[0:2] == "v=": videoId = i[2:] url = f"https://www.youtube.com/watch?v={videoId}" - elif video_type=="shorts": + elif video_type == "shorts": videoId = url.split("?")[0].split("/")[-1] url = f"https://www.youtube.com/watch?v={videoId}" global y, z @@ -143,8 +158,8 @@ class YouTube: return irc_string, True z.update({"duration": self.fmt_dur(z["duration"])}) y, z = z, {} - irc_string = f"[\x0303Youtube\x03] \x02{y['name']}\x02 ({y['duration']}) uploaded by \x1d{y['channelName']}\x1d on {y['uploadDate']}, {y['interactionCount']:,} views" - 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']}, {y['interactionCount']:,} views" + irc_string = f"[\x0303Youtube\x03] \x02{y['title']}\x02 ({y['duration']}) uploaded by \x1d{y['channelName']}\x1d on {y['uploadDate']}, {y['interactionCount']:,} views" + ansi_string = f"[\x1b[32mYoutube\x1b[0m] \x1b[1m{y['title']}\x1b[0m ({y['duration']}) uploaded by \x1b[03m{y['channelName']}\x1b[0m on {y['uploadDate']}, {y['interactionCount']:,} views" print(ansi_string) return irc_string, False