summary refs log tree commit diff
diff options
context:
space:
mode:
authorPawky Laguish2024-11-27 22:41:19 +0000
committerPawky Laguish2024-11-27 22:41:19 +0000
commit82b4772f1c0bbb9772c51598625958eef3676b4f (patch)
treeb3989458b68c4dedffad7cf3e446e2ed416008ec
parentea30e6ff34f6c5356e42c23a3b0c74fbbaa17771 (diff)
should mostly work I guess
-rwxr-xr-xsoundcloud.py113
-rwxr-xr-xspotify.py61
2 files changed, 35 insertions, 139 deletions
diff --git a/soundcloud.py b/soundcloud.py
index 40c20fd..de0e930 100755
--- a/soundcloud.py
+++ b/soundcloud.py
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
-from html.parser import HTMLParser
+from urllib.parse import urlencode,urlparse
 from urllib.request import urlopen
-
+from json import loads as json_loads
 
 class SoundCloud:
     video_type = ""
@@ -24,101 +24,30 @@ class SoundCloud:
 
         return r
 
-    class parseprop(HTMLParser):
-        def __init__(self):
-            print("soundcloud parse init")
-            HTMLParser.__init__(self)
-            self.itemprops_list = ["name", "duration", "byArtist"]
-            self.h = {}
-            self.readartist=False
-
-        def handle_starttag(self, tag, attrs):
-            print("yo",tag,attrs)
-            if (tag != "meta" and tag != "link" and tag != "div") or (
-                (
-                    [i for i in attrs if "itemprop" in i] == []
-                    and ("name", "title") not in attrs
-                )
-                or (tag == "meta" and ("itemprop", "name") in attrs and self.readartist!=True)
-            ):
-                print("skip",tag,attrs)
-                return
-            # print(self,tag,attrs)
-            for k, v in attrs:
-                if k == "itemprop":
-                    if v not in self.itemprops_list:
-                        print("skipprop",tag,attrs)
-                        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
-            print({x[0]: x[1]})
-            self.h.update({x[0]: x[1]})
-            # print(x[0],"=",x[1])
-
-    def fmt_dur(dur):
-        h, m, s = 0, 0, 0
-        dur=dur.removeprefix("PT").removesuffix("S")
-        h,m = dur.split("H")
-        m,s = dur.removeprefix(f"{h}H").split("M")
-        #s = int(m[1][:-1])
-        #m = int(m[0])
-        h,m,s=int(h),int(m),int(s)
-        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):
+    def soundcloud(self, url):
         # self.util.mesg("dbg hello")
         url = url.rstrip("\x01")
-        p = self.parseprop()
-        # use premature optimization? it should be SLIGHTLY faster, but can sometimes fail
-        data = b""
-        if False: #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
-            data = urlopen(url).read().decode()
-        # print(f"\x1b[31m my data is: {data}\x1b[0m")
-        p.feed(data)
-        if p.h == {}:
+        url=urlparse(url)
+        url=url.scheme+"://"+url.netloc+url.path
+        url = f"https://soundcloud.com/oembed?{urlencode([('url',url),('format','json')])}"
+        data=urlopen(url).read().decode()
+        data=json_loads(data)
+        """ {'version': 1.0, 'type': 'rich', 'provider_name': 'SoundCloud', 'provider_url': 'https://soundcloud.com', 'height': 400, 'width': '100%', 'title': 'Doses And Mimosas - Cherub (Mocha Remix) by Mocha Music', 'description': 'One of my favorite songs ever with a dubstep twist! Hope you enjoy :)', 'thumbnail_url': 'https://i1.sndcdn.com/artworks-768QwHQ4tGr0P4wc-iH0Zww-t500x500.jpg', 'html': '<iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F1881169554&show_artwork=true"></iframe>', 'author_name': 'Mocha Music', 'author_url': 'https://soundcloud.com/mochamusic11' }"""
+        #print(data["title"].removesuffix(" by "+data["author_name"]),data["author_name"])
+        try:
+          artist=data["author_name"];title=data["title"].removesuffix(" by "+artist)
+        except KeyError:
+          title="";artist=""
+        print(title.removesuffix(" by "+artist),"|",artist)
+        if title == "":
             irc_string = "[\x0304SoundCloud\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03"
             ansi_string = "[\x1b[31mSoundCloud\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
-        y = p.h
-        print(y)
-        y.update(duration=self.fmt_dur(y["duration"]))
-        #irc_string = f"[\x0303SoundCloud\x03] \x02{y['title']}\x02 ({y['duration']}) uploaded by \x1d{y['channelName']}\x1d on {y['uploadDate']}, {y['interactionCount']:,} views"
-        #ansi_string = f"[\x1b[32mSoundCloud\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="dummy";ansi_string="dummy"
-        print(y)
+        irc_string = f"[\x0303SoundCloud\x03] \x02{title}\x02 uploaded by \x1d{artist}\x1d"
+        ansi_string = f"[\x1b[32mSoundCloud\x1b[0m] \x1b[1m{title}\x1b[0m uploaded by \x1b[03m{artist}\x1b[0m"
+        #"""
+        #irc_string="dummy";ansi_string="dummy"
         print(ansi_string)
         return irc_string, False
 
@@ -126,4 +55,4 @@ class SoundCloud:
 if __name__ == "__main__":
     import sys
 
-    SoundCloud.yt(SoundCloud, sys.argv[1])
+    SoundCloud.soundcloud(SoundCloud, sys.argv[1])
diff --git a/spotify.py b/spotify.py
index 8d062d7..f376bc1 100755
--- a/spotify.py
+++ b/spotify.py
@@ -44,53 +44,18 @@ class Spotify:
         		Spotify.ldjson=data
         		return
 
-    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 spotify(self, url):
-        # self.util.mesg("dbg hello")
         url = url.rstrip("\x01")
         p = self.parseprop()
-        # 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")
+        data = urlopen(url).read().decode()
         p.feed(data)
-        #    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
-        #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)
+        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(Spotify.ldjson))
-        #'@type': 'MusicRecording', '@id': 'https://open.spotify.com/track/7hoyhyjcZvOCC9Tv9JgRhr', 'url': 'https://open.spotify.com/track/7hoyhyjcZvOCC9Tv9JgRhr', 'name': 'YO HO (PEG THE POOPDECK)', 'description': 'Listen to YO HO (PEG THE POOPDECK) on Spotify. Song · TheDooo · 2023', 'datePublished': '2023-08-30'
-        #Listen to The Evolution of Tears on Spotify · Album · The Gentle Men · 2021 · 10 songs
-        type=data["@type"]
+        try: type=data["@type"]
+        except KeyError:
+          print(ansi_string)
+          return irc_string, True
         id=data["@id"]
         name=data["name"]
         date=data["datePublished"]
@@ -101,12 +66,14 @@ class Spotify:
         if artists.startswith("Song · "): artists=artists.removeprefix("Song · ")
         elif artists.startswith("Album · "):
           artists=artists.removeprefix("Album · ")
-          #removes the "10 songs" part
-          artists=artists[::-1].split(" · ",1)[1][::-1]
-        artists=artists.removesuffix(f" · {date[:4]}")
+          artists=artists[::-1].split(" · ",1)[1][::-1] #removes the "10 songs" part from album
+        artists=artists.removesuffix(f" · {date[:4]}")  #remove the year too
         #print(type,id,name,"|"+artists+"|",date)
-        print(("Song: " if type=="MusicRecording" else "Album: " if type=="MusicAlbum" else f"Unknown type ({type}): ")+'"'+name+'"'+" by "+'"'+artists+'"'+" released on "+date)
-        irc_string="dummy"
+        irc_string = f"[\x0303Spotify\x03] \x02{name}\x02 by \x1d{artists}\x1d published on {date}"
+        ansi_string = f"[\x1b[32mSpotify\x1b[0m] \x1b[1m{name}\x1b[0m by \x1b[03m{artists}\x1b[0m published on {date}"
+        #print(("Song: " if type=="MusicRecording" else "Album: " if type=="MusicAlbum" else f"Unknown type ({type}): ")+'"'+name+'"'+" by "+'"'+artists+'"'+" released on "+date)
+        #irc_string="dummy"
+        print(ansi_string)
         return irc_string, False