summary refs log tree commit diff
path: root/soundcloud.py
blob: de0e9300020cfc8645d0f229be3e10d24e79631d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
from urllib.parse import urlencode,urlparse
from urllib.request import urlopen
from json import loads as json_loads

class SoundCloud:
    video_type = ""

    def mesg(self, msg, t=None):
        self.util.mesg(msg, t)

    def match_urls(self, str):
        r = [
            i
            for i in str.split()
            if "https://soundcloud.com" in i
        ]
        r = list(dict.fromkeys(r))
        n = 0
        for i in r:
            if not i.startswith("http"):
                r.pop(n)
            n += 1

        return r

    def soundcloud(self, url):
        # self.util.mesg("dbg hello")
        url = url.rstrip("\x01")
        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
        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


if __name__ == "__main__":
    import sys

    SoundCloud.soundcloud(SoundCloud, sys.argv[1])