summary refs log tree commit diff
path: root/youtube.py
diff options
context:
space:
mode:
Diffstat (limited to 'youtube.py')
-rwxr-xr-xyoutube.py132
1 files changed, 28 insertions, 104 deletions
diff --git a/youtube.py b/youtube.py
index 41dd18b..a75f52b 100755
--- a/youtube.py
+++ b/youtube.py
@@ -1,11 +1,10 @@
 #!/usr/bin/env python3
-from html.parser import HTMLParser
 from urllib.request import urlopen
-
+from urllib.error import HTTPError
+from urllib.parse import urlencode, urlparse, parse_qs
+from json import loads as json_loads
 
 class YouTube:
-    video_type = ""
-
     def mesg(self, msg, t=None):
         self.util.mesg(msg, t)
 
@@ -52,119 +51,44 @@ class YouTube:
             "https://www.youtube.com/clip/"
         )
 
-    class parseprop(HTMLParser):
-        def __init__(self):
-            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!")
-
-        def handle_starttag(self, tag, attrs):
-            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
-            # print(self,tag,attrs)
-            for k, v in attrs:
-                if k == "itemprop":
-                    if v not in self.itemprops_list:
-                        return
-                    x = [v]
-                    if tag == "link" and v == "name":
-                        x = ["channelName"]
-                elif k == "content":
-                    if attrs[0][1] == "interactionCount":
-                        v = int(v)
-                    x += [v]
-                elif k == "name" and v == "title":
-                    x = [v]
-                else:
-                    return
-            self.h.update({x[0]: x[1]})
-            # print(x[0],"=",x[1])
-
-    def fmt_dur(dur):
-        h, m, s = 0, 0, 0
-        m = dur[2:].split("M")
-        s = int(m[1][:-1])
-        m = int(m[0])
-        if m >= 60:
-            h = m // 60
-            m = round((m / 60 - h) * 60)
-            return f"{h}h {m}m {s}s"
-        elif h == 0 and m == 0 and s == 0:
-            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"
-
     def yt(self, url):
+        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"
         # 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":
+        if self.is_embed(url):
             videoId = url.split("/")[4]
             url = f"https://www.youtube.com/watch?v={videoId}"
-        elif video_type == "music":
+        elif self.is_ytmusic(url):
             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 self.is_ytshorts(url):
             videoId = url.split("?")[0].split("/")[-1]
             url = f"https://www.youtube.com/watch?v={videoId}"
-        p = self.parseprop()
-        # use premature optimization? it should be SLIGHTLY faster, but can sometimes fail
-        data = b""
-        if self.premature_optimization:
-            url_h = urlopen(url)
-            # <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()
-            url_h.close()
-        data = data.decode()  # bytes to utf-8
-        if (
-            data.find('meta itemprop="duration"') == -1
-            or data.find('meta itemprop="name"') == -1
-        ):  # acts as both fallback for optimization, and in case optimization's turned off
-            # just read all of the html
+        url = urlparse(url)
+        qs=parse_qs(url.query);video_id=qs['v'][0]
+        try: playlist_id=qs['list'][0]
+        except KeyError: playlist_id=None
+        if self.prefer_playlist and playlist_id:
+          url = url.scheme + "://" + url.netloc + "/playlist?list=" + playlist_id
+        else:
+          url = url.scheme + "://" + url.netloc + url.path + "?v=" + video_id
+        url = f"https://www.youtube.com/oembed?{urlencode([('url',url),('format','json')])}"
+        try:
+            print(url," and ",playlist_id)
             data = urlopen(url).read().decode()
-        # print(f"\x1b[31m my data is: {data}\x1b[0m")
-        p.feed(data)
-        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"
+            data = json_loads(data)
+            title=data['title']
+            channelName=data['author_name']
+        except HTTPError as e:
+            irc_string = f"[\x0304Youtube\x03] \x0307ERROR:\x0308 {e} \x0315\x03"
+            ansi_string = f"[\x1b[31mYoutube\x1b[0m] \x1b[33;2mERROR:\x1b[33;1m {e} \x1b[37;2m\x1b[0m"
             print(ansi_string)
             return irc_string, True
-        y = p.h
-        print(y)
-        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"
+        irc_string = f"[\x0303Youtube\x03] \x02{title}\x02 uploaded by \x1d{channelName}\x1d"
+        ansi_string = f"[\x1b[32mYoutube\x1b[0m] \x1b[1m{title}\x1b[0m uploaded by \x1b[03m{channelName}\x1b[0m"
         print(ansi_string)
         return irc_string, False
 
@@ -172,5 +96,5 @@ class YouTube:
 if __name__ == "__main__":
     import sys
 
-    YouTube.premature_optimization = False
+    YouTube.prefer_playlist=False
     YouTube.yt(YouTube, sys.argv[1])