1
0
forked from jan6/bot6

Compare commits

...

4 Commits

Author SHA1 Message Date
xfnw
666c0f1c40 youtube: get rid of globals 2024-05-20 12:54:49 -04:00
c7e5538bac Merge pull request 'commands: replace eval with getattr' (#9) from xfnw/bot6:getattr into master
Reviewed-on: jan6/bot6#9
Reviewed-by: jan6 <jan6@tilde.team>
2024-05-20 16:51:21 +00:00
44403e79c0 Merge branch 'master' into getattr 2024-05-20 16:50:54 +00:00
xfnw
97abebeedb commands: replace eval with getattr 2024-04-09 14:23:37 -04:00
2 changed files with 9 additions and 12 deletions

View File

@ -127,9 +127,9 @@ class Command:
return
if command not in self.config.cmd.disabled:
if needs_prefix == False:
eval(f"self.{command}()")
getattr(self, command)()
elif not (self.prefix == None and self.pm == False):
eval(f"self.{command}()")
getattr(self, command)()
# else:
# self.mesg("this ain't a valid commanderoonie, you twat")
@ -314,7 +314,7 @@ class Command:
mesg(f"admin commands: {admin_commands}")
else:
try:
mesg(f"{topic}: " + eval(f"self.{topic}.__doc__"))
mesg(f"{topic}: " + getattr(self, topic).__doc__)
except (TypeError, AttributeError) as e:
# mesg(str( e.__class__.__name__ ))
if topic in abs_topics:

View File

@ -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)