summaryrefslogtreecommitdiffstats
path: root/god/streams/bin/relaxxapi.py
blob: f367caef9958fd25e55827f21396d84d7387b236 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python2
import json
from urllib import quote
class relaxx:

    def __init__(self,relaxxurl="http://lounge.mpd.shack/"):
        self.baseurl=relaxxurl
        import requests
        ret = requests.get(relaxxurl) # grab cookie
        try:
            self.r = requests.session(cookies=ret.cookies,headers={"Referer":relaxxurl})
        except:
            print ("you are missing the `requests` dependency, please do a `pip install requests`")
    def _status(self,value=0,data="json=null"):
        """
        value is some weird current playlist value, 0 seems to work
        data is url encoded kv-store
        """
        # TODO get the current playlist value
        url=self.baseurl+"include/controller-ping.php?value=%s"%value
        return self.r.post(url,data="json=null").text

    def _playlist(self,action,value="",json="null",method="get"):
        """
        This function is the interface to the controller-playlist api
            use it if you dare
        Possible actions:
            clear
            addSong url_encoded_path
            moveSong 1:2
            getPlaylists
            getPlaylistInfo 1
            listPlaylistInfo
        as everything seems to be a get request, the method is set to GET as
        default
        """
        url=self.baseurl+"include/controller-playlist.php?action=%s&value=%s&json=%s"%(action,value,json)
        if method== "get":
            return self.r.get(url).text
        elif method == "post":
            return r.post(url).text
        else:
            raise Exception("unknown method %s")

    def _playback(self,action,value="",json="null",method="get"):
        """
        play
        continue
        stop
        setCrossfade
        """
        url=self.baseurl+"include/controller-playback.php?action=%s&value=%s&json=%s"%(action,value,json)
        # probably obsolete because everything is "get"
        if method== "get":
            return self.r.get(url).text
        elif method == "post":
            return r.post(url).text
        else:
            raise Exception("unknown method %s")

    def _radio(self,playlist=""):
        """
        both, post and get the url seem to work here...
        """
        url=self.baseurl+"include/controller-netradio.php?playlist=%s"%quote(playlist)
        return self.r.get(url).text

    def add_radio(self,playlist=""):
        print playlist
        print self._radio(playlist)
        print json.loads(self._radio(playlist)) #[1:-1])["url"]
        resolved_url= json.loads(self._radio(playlist)[1:-1])["url"]
        self.add_song(resolved_url)

    def add_song(self,path):
        return self._playlist("addSong",path)

    def get_first(self):
        return json.loads(self._playlist("getPlaylistInfo","0",""))[0]

    def get_first(self):
        return json.loads(self._playlist("getPlaylistInfo","0",""))[-1]

    def clear(self):
        return self._playlist("clear")
    
    def crossfade(self,ident="0"):
        """
        default: no crossfade
        """
        return self._playback("setCrossfade",ident)

    def repeat(self,ident="1"):
        """
        default: do repeat
        """
        return self._playback("repeat",ident)

    def play(self,ident):
        return self._playback("play",ident)

    def stop(self):
        return self._playback("stop")

    def cont(self,ident):
        return self._playback("continue",ident)

    def play_first(self):
        return self.play(self.get_first()["Id"])

    def play_last(self):
        return self.play(self.get_last()["Id"])

    def state(self):
        return json.loads(self._status())

    def is_running(self):
        return self.state()["status"]["state"] == "play"

    def playing(self):
        """ returns "" if not running
        """
        state = self.state()
        if state["status"]["state"] == "play" :
            ident = state["status"]["song"]
            current = state["playlist"]["file"][int(ident)]
            return current.get("Name",current.get("Artist")) + " - " + current["Title"]
        else:
            return ""

if __name__ == "__main__":
    r = relaxx()
    print r.state()
    print r.playing()
    print r.add_radio("http://deluxetelevision.com/livestreams/radio/DELUXE_RADIO.pls")
    #print r.clear()
    #print r.add_radio("http://somafm.com/lush.pls")
    #print r.get_first()["Id"]
    #print r.play_first()
    #print r.add_radio("http://somafm.com/lush.pls")