This repository has been archived by the owner on Dec 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
mopidy.py
213 lines (187 loc) · 6.66 KB
/
mopidy.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import requests
import json
import shutil
import tempfile
import urllib.request
import threading
import time
import pygame
import traceback
class MusicPlayer(object):
trackdata = dict()
artist = ""
album = ""
title = ""
image = None
_imageurl = ""
download_complete = False
image_cache = {}
playing = False
muted = False
volume = 100
trackdata_changed = True
old_trackimages = None
old_trackinfo = None
playlist_set = False
def __init__(self, hostname="127.0.0.1", port="6680", password="", shuffle=False):
self.url = "http://"+hostname+":"+port+"/mopidy/rpc"
if shuffle == "1":
self.shuffle = True
else:
self.shuffle = False
# print(self.checkAlarmPlaylist())
self.update_thread = threading.Thread(target=self.updateStatus)
self.update_thread.daemon = True
self.update_thread.start()
def _downloader(self):
if self._imageurl != None and self._imageurl not in self.image_cache:
with urllib.request.urlopen(self._imageurl) as response:
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
shutil.copyfileobj(response, tmp_file)
self.image_cache[self._imageurl] = tmp_file.name
def updateStatus(self):
while True:
self.updateTrackInfo()
self.getState()
self.getVolume()
time.sleep(1)
@property
def imageurl(self):
return self._imageurl
@imageurl.setter
def imageurl(self, url):
if url != self._imageurl:
self._imageurl = url
self._downloader()
self._t = threading.Thread(
target=self._downloader)
self._t.daemon = True
self._t.start()
self.image = pygame.Surface((1, 1), flags=pygame.SRCALPHA)
if self._imageurl != None:
self.image = pygame.image.load(
self.image_cache[self._imageurl])
self.trackdata_changed = True
def updateTrackInfo(self):
artist = ""
album = ""
title = ""
imagefile = ""
try:
trackinfo = self._clientRequest(
"core.playback.get_current_track")["result"]
trackimages = self._clientRequest("core.library.get_images", {
"uris": [trackinfo["uri"]]})["result"]
if self.old_trackinfo == trackinfo and self.old_trackimages == trackimages:
self.trackdata_changed = False
return
else:
self.old_trackinfo = trackinfo
self.old_trackimages = trackimages
self.trackdata_changed = True
self.artist = trackinfo["artists"][0]["name"].strip()
self.title = trackinfo["name"].strip()
try:
self.album = trackinfo["album"]["name"].strip()
except:
self.album = ""
try:
self.imageurl = trackimages[trackinfo["uri"]][0]["uri"]
except:
self.imageurl = None
except Exception as e:
print(traceback.format_exc())
self.artist = self.album = self.title = ""
self.imageurl = None
if self.artist == self.album:
self.album = ""
def togglePlay(self):
if not self.playlist_set:
self.setAlarmPlaylist()
if self.playing:
method = "core.playback.pause"
else:
method = "core.playback.play"
self._clientRequest(method)
self.getState()
def play(self):
if not self.playlist_set:
self.setAlarmPlaylist()
if self.shuffle:
method = "core.tracklist.shuffle"
self._clientRequest(method)
method = "core.playback.play"
self._clientRequest(method)
self.getState()
def skip(self):
self._clientRequest("core.playback.next")
def back(self):
self._clientRequest("core.playback.previous")
def getVolume(self):
try:
self.volume = int(self._clientRequest(
"core.mixer.get_volume")["result"])
self.muted = bool(self._clientRequest(
"core.mixer.get_mute")["result"])
except Exception as e:
print(e)
self.volume = 100
self.muted = False
def toggleMute(self):
self._clientRequest("core.mixer.set_mute", {"mute": not self.muted})
self.muted = bool(self._clientRequest(
"core.mixer.get_mute")["result"])
def volup(self):
self._clientRequest("core.mixer.set_volume", {
"volume": self.volume + 10})
self.getVolume()
def voldown(self):
self._clientRequest("core.mixer.set_volume", {
"volume": self.volume - 10})
self.getVolume()
def getState(self):
status = self._clientRequest("core.playback.get_state")["result"]
if status == "playing":
self.playing = True
else:
self.playing = False
def setAlarmPlaylist(self):
try:
self.checkAlarmPlaylist()
self._clientRequest("core.tracklist.clear")
alarm_uri = self._clientRequest("core.playlists.filter", {
"criteria": {"name": "Alarm"}})["result"][0]["uri"]
alarm_tracks = self._clientRequest(
"core.playlists.get_items", {"uri": alarm_uri})["result"]
for track in alarm_tracks:
self._clientRequest(
"core.tracklist.add", {'uri': track["uri"]})
self.playlist_set = True
except Exception as e:
print(e)
def checkAlarmPlaylist(self):
result = self._clientRequest("core.playlists.filter", {
"criteria": {"name": "Alarm"}})
if len(result["result"]) > 0:
self.playlist = result["result"][0]["uri"]
else:
self.playlist = self._clientRequest("core.playlists.create", {
"name": "Alarm"
})["result"][0]["uri"]
def _clientRequest(self, method, params={}):
headers = {'content-type': 'application/json'}
payload = {
"method": method,
"jsonrpc": "2.0",
"params": params,
"id": 1,
}
try:
return requests.post(self.url, data=json.dumps(
payload), headers=headers, timeout=1).json()
except Exception as e:
print(e)
print(payload)
return {"result": None}
if __name__ == "__main__":
print("This module cannot be called directly.")