From 666c0f1c400a8159da58cf9a06195fcbefdce9d6 Mon Sep 17 00:00:00 2001 From: xfnw Date: Mon, 20 May 2024 12:19:17 -0400 Subject: [PATCH] youtube: get rid of globals --- youtube.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/youtube.py b/youtube.py index ff35f30..66b9871 100644 --- a/youtube.py +++ b/youtube.py @@ -3,7 +3,6 @@ from urllib.request import urlopen class YouTube: - y, z = {}, {} # empty on every invokation video_type = "" def mesg(self, msg, t=None): @@ -57,6 +56,7 @@ class YouTube: print("yt parse init") HTMLParser.__init__(self) self.itemprops_list = ["name", "duration", "uploadDate", "interactionCount"] + self.h = {} if YouTube.video_type == "clip": self.itemprops_list += ["description"] print("it is a clip!") @@ -70,7 +70,6 @@ class YouTube: or (tag == "meta" and ("itemprop", "name") in attrs) ): return - global z # print(self,tag,attrs) for k, v in attrs: if k == "itemprop": @@ -87,7 +86,7 @@ class YouTube: x = [v] else: return - z.update({x[0]: x[1]}) + self.h.update({x[0]: x[1]}) # print(x[0],"=",x[1]) def fmt_dur(dur): @@ -134,8 +133,6 @@ class YouTube: elif video_type == "shorts": videoId = url.split("?")[0].split("/")[-1] url = f"https://www.youtube.com/watch?v={videoId}" - global y, z - y, z = {}, {} p = self.parseprop() # use premature optimization? it should be SLIGHTLY faster, but can sometimes fail data = b"" @@ -145,8 +142,8 @@ class YouTube: # I tried to read byte amounts but it's hard to make sure no invalid utf8 bytes happen due to partial reads for i in range(24): data += url_h.readline() - data = data.decode() # bytes to utf-8 url_h.close() + data = data.decode() # bytes to utf-8 if ( data.find('meta itemprop="duration"') == -1 or data.find('meta itemprop="name"') == -1 @@ -155,13 +152,13 @@ class YouTube: data = urlopen(url).read().decode() # print(f"\x1b[31m my data is: {data}\x1b[0m") p.feed(data) - if y == z == {}: + if p.h == {}: irc_string = "[\x0304Youtube\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03" ansi_string = "[\x1b[31mYoutube\x1b[0m] \x1b[33;2mERROR:\x1b[33;1m got no data from server! \x1b[37;2m(check your URL for typos!)\x1b[0m" print(ansi_string) return irc_string, True - z.update({"duration": self.fmt_dur(z["duration"])}) - y, z = z, {} + y = p.h + y.update(duration=self.fmt_dur(y["duration"])) 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)