idk, added version command, youtube displays view count now...

This commit is contained in:
jan6 2023-07-08 13:08:25 -05:00
parent a84639237e
commit 83754f44cb
3 changed files with 48 additions and 9 deletions

View File

@ -111,6 +111,8 @@ class Command:
command = "dbg"
elif cmd == "dbg2" or cmd.startswith("dbg2 "):
command = "dbg2"
elif cmd == "version" or cmd == "ver":
command = "version"
elif cmd.startswith("\x01") or self.is_ctcp:
command = "ctcp"
else:
@ -159,7 +161,11 @@ class Command:
YouTube = self.YouTube
if cmd.startswith("yt "):
cmd = cmd[3:]
# cmd=cmd.split()[0]
try:
YouTube.premature_optimization = self.config.cmd.yt_premature_opt
except AttributeError:
pass
print(f" YT premature_optimization={YouTube.premature_optimization}")
urls = YouTube.match_urls(YouTube, cmd)
yt_failed = False
for video in urls:
@ -178,10 +184,23 @@ class Command:
"""simple echo command"""
mesg("\x7f" + cmd.split(" ", 1)[1])
@cmd
def version(self, prefix, cmd, pm, line, admin, mesg):
"""version"""
with open(self.config.self.gitdir + ".git/logs/HEAD") as f:
for l in f:
pass
mesg(
f"{self.config.self.nick} version {l.split()[1]} ({self.config.self.source})"
)
@cmd
def dbg2(self, prefix, cmd, pm, line, admin, mesg):
"""simple echo command"""
mesg("\x08" + cmd.split(" ", 1)[1])
"""version"""
with open(self.config.self.gitdir + ".git/logs/HEAD") as f:
for l in f:
pass
mesg("version " + l.split()[1])
@cmd
def me(self, prefix, cmd, pm, line, admin, mesg):

View File

@ -16,6 +16,7 @@ class config(config):
# you should probably indicate yourself to be the owner of the bot, in username, or realname, or both
realname = "jan6's bot"
source = "https://tildegit.org/jan6/bot6" # so far only used for ctcp response, perhaps we're running a fork?
gitdir = "./" # where is bot6 cloned? currently only used for version (assuming latest commit == latest version)
class server:
name = "libera"
@ -50,6 +51,9 @@ class config(config):
# commands which should only ever be used by admins, should be designated as such in code, not through here (e.g. exit)
admin_only = []
ignored_nicks = []
# try to read youtube page only up to <body> tag, maybe it's faster?
# premature optimization is the root of all evil
yt_premature_opt = True
capabilities = [ # what capabilities shall we request?
"message-tags", # needed for account-tag!

View File

@ -3,7 +3,7 @@ from urllib.request import urlopen
class YouTube:
y, z = {}, {}
y, z = {}, {} # empty on every invokation
def mesg(self, msg, t=None):
self.util.mesg(msg, t)
@ -54,12 +54,14 @@ class YouTube:
# print(self,tag,attrs)
for k, v in attrs:
if k == "itemprop":
if v not in ["name", "duration", "uploadDate"]:
if v not in ["name", "duration", "uploadDate", "interactionCount"]:
return
x = [v]
if tag == "link" and v == "name":
x = ["channelName"]
elif k == "content":
if attrs[0][1] == "interactionCount":
v = int(v)
x += [v]
z.update({x[0]: x[1]})
# print(x[0],"=",x[1])
@ -77,6 +79,8 @@ class YouTube:
return "LIVE"
elif m == 0 and s != 0:
return f"{s}s"
elif s == 0:
return f"{m}m"
else:
return f"{m}m {s}s"
@ -97,8 +101,19 @@ class YouTube:
global y, z
y, z = {}, {}
p = self.parseprop()
# url="https://www.youtube.com/watch?v=gyMpI8csWis"
data = urlopen(url).read().decode()
# use premature optimization? it should be SLIGHTLY faster
if self.premature_optimization:
url_h, data = urlopen(url), b""
# <body> appears on approximately line 21 or 22, so we read 24 lines to be safe (23-25 should be license comment)
# 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()
else:
# just read all of the html
data = urlopen(url).read().decode()
# print(f"\x1b[31m my data is: {data}\x1b[0m")
p.feed(data)
if y == z == {}:
irc_string = "[\x0304Youtube\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03"
@ -107,8 +122,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']}"
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']}"
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"
print(ansi_string)
return irc_string, False
@ -116,4 +131,5 @@ class YouTube:
if __name__ == "__main__":
import sys
YouTube.premature_optimization = False
YouTube.yt(YouTube, sys.argv[1])