-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path__init__.py
280 lines (234 loc) · 9.29 KB
/
__init__.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import re
import time
from fuzzywuzzy.process import extractOne as extract_one
from mycroft.skills.common_play_skill import CommonPlaySkill, CPSMatchLevel
from .mopidypost import Mopidy
NOTHING_FOUND = (None, 0.0)
class MopidySkill(CommonPlaySkill):
def __init__(self):
super(MopidySkill, self).__init__('Mopidy Skill')
self.mopidy = None
self.volume_is_low = False
self.regexes = {}
def _connect(self):
url = 'http://localhost:6680'
if self.settings:
url = self.settings.get('mopidy_url', url)
try:
mopidy = Mopidy(url)
except Exception:
self.log.warning('Could not connect to Mopidy server')
return None
self.log.info('Connected to mopidy server')
self.cancel_scheduled_event('MopidyConnect')
self.albums = {}
self.artists = {}
self.genres = {}
self.playlists = {}
self.radios = {}
self.track_names = {}
self.log.info('Loading content')
self.albums['gmusic'] = mopidy.get_gmusic_albums()
self.artists['gmusic'] = mopidy.get_gmusic_artists()
self.genres['gmusic'] = mopidy.get_gmusic_radio()
self.playlists['gmusic'] = {}
self.track_names['gmusic'] = {}
self.albums['local'] = mopidy.get_local_albums()
self.artists['local'] = mopidy.get_local_artists()
self.genres['local'] = mopidy.get_local_genres()
self.playlists['local'] = mopidy.get_local_playlists()
self.track_names['local'] = mopidy.get_local_track_names()
self.albums['spotify'] = {}
self.artists['spotify'] = {}
self.genres['spotify'] = {}
self.playlists['spotify'] = mopidy.get_spotify_playlists()
self.track_names['spotify'] = {}
self.playlist = {}
for loc in ['local', 'gmusic', 'spotify']:
self.log.info(loc)
self.playlist.update(self.playlists[loc])
self.log.info(loc)
self.playlist.update(self.genres[loc])
self.log.info(loc)
self.playlist.update(self.artists[loc])
self.log.info(loc)
self.playlist.update(self.albums[loc])
self.log.info(loc)
self.playlist.update(self.track_names[loc])
return mopidy
def initialize(self):
self.log.info('initializing Mopidy skill')
super(MopidySkill, self).initialize()
# Setup handlers for playback control messages
self.add_event('mycroft.audio.service.next', self.handle_next)
self.add_event('mycroft.audio.service.prev', self.handle_prev)
self.add_event('mycroft.audio.service.pause', self.handle_pause)
self.add_event('mycroft.audio.service.resume', self.handle_resume)
self.mopidy = self._connect()
def play(self, tracks):
self.mopidy.clear_list()
self.mopidy.add_list(tracks)
self.mopidy.play()
def translate_regex(self, regex):
if regex not in self.regexes:
path = self.find_resource(regex + '.regex', 'dialog')
if path:
with open(path) as f:
string = f.read().strip()
self.regexes[regex] = string
return self.regexes[regex]
def CPS_match_query_phrase(self, phrase):
# If no mopidy connection can be detected return None
if self.mopidy is None:
self.mopidy = self._connect()
if not self.mopidy:
return None
self.log.info('Checking Mopidy for {}'.format(phrase))
mopidy_specified = 'mopidy' in phrase
phrase = re.sub(self.translate_regex('on_mopidy'), '', phrase)
match_level = CPSMatchLevel.MULTI_KEY
match = self.specific_query(phrase)
# If nothing was found check for a generic match
if match == NOTHING_FOUND:
match = self.generic_query(phrase)
match_level = CPSMatchLevel.GENERIC
self.log.info('Mopidy match: {}'.format(match))
if match == NOTHING_FOUND:
self.log.debug('Nothing found on mopidy')
return None
else:
return (phrase,
(CPSMatchLevel.EXACT if mopidy_specified else match_level),
{'playlist': match[0],
'playlist_type': match[2],
'library_type': match[3]
})
def query_song(self, song):
best_found = None
best_conf = 0
library_type = None
for t in self.track_names:
found, conf = (extract_one(song, self.track_names[t].keys()) or
(None, 0))
if conf > best_conf and conf > 50:
best_conf = conf
best_found = found
library_type = t
return best_found, best_conf, 'song', library_type
def query_artist(self, artist):
best_found = None
best_conf = 0.0
library_type = None
for t in self.artists:
found, conf = (extract_one(artist, self.artists[t].keys()) or
(None, 0))
if conf > best_conf and conf > 50:
best_conf = conf
best_found = found
library_type = t
return best_found, best_conf, 'artist', library_type
def query_album(self, album):
best_found = None
best_conf = 0
library_type = None
for t in self.albums:
self.log.info(self.albums[t].keys())
found, conf = (extract_one(album, self.albums[t].keys()) or
(None, 0))
if conf > best_conf and conf > 50:
best_conf = conf
best_found = found
library_type = t
self.log.info('ALBUMS')
self.log.info((best_found, best_conf))
return best_found, best_conf, 'album', library_type
def specific_query(self, phrase):
"""Check if the request is for a specific type.
This checks, albums, artists, genres and tracks.
"""
# Check if playlist
#match = re.match(self.translate_regex('playlist'), phrase)
#if match:
# return self.query_playlist(match.groupdict()['playlist'])
# Check album
match = re.match(self.translate_regex('album'), phrase)
if match:
album = match.groupdict()['album']
return self.query_album(album)
# Check artist
match = re.match(self.translate_regex('artist'), phrase)
if match:
artist = match.groupdict()['artist']
return self.query_artist(artist)
match = re.match(self.translate_regex('song'), phrase)
if match:
song = match.groupdict()['track']
return self.query_song(song)
return NOTHING_FOUND
def generic_query(self, phrase):
found, conf = extract_one(phrase, self.playlist.keys())
if conf > 50:
return found, conf, 'generic', ''
else:
return NOTHING_FOUND
def CPS_start(self, phrase, data):
p = data.get('playlist')
list_type = data.get('playlist_type', 'generic')
library_type = data.get('library_type', 'generic')
lists = {'generic': self.playlist,
'artist': self.artists,
'album': self.albums,
'song': self.track_names
}
if list_type == 'generic':
playlists = lists[list_type]
else:
playlists = lists[list_type][library_type]
self.stop()
self.speak('Playing {}'.format(p))
time.sleep(3)
if playlists[p]['type'] == 'playlist':
tracks = self.mopidy.get_items(playlists[p]['uri'])
if playlists[p]['type'] == 'track':
tracks = playlists[p]['uri']
else:
tracks = self.mopidy.get_tracks(playlists[p]['uri'])
self.play(tracks)
def stop(self, message=None):
self.log.info('Handling stop request')
if self.mopidy:
self.mopidy.clear_list()
self.mopidy.stop()
def handle_next(self, message):
self.mopidy.next()
def handle_prev(self, message):
self.mopidy.previous()
def handle_pause(self, message):
self.mopidy.pause()
def handle_resume(self, message):
"""Resume playback if paused"""
self.mopidy.resume()
def lower_volume(self, message):
self.log.info('lowering volume')
self.mopidy.lower_volume()
self.volume_is_low = True
def restore_volume(self, message):
self.log.info('maybe restoring volume')
self.volume_is_low = False
time.sleep(2)
if not self.volume_is_low:
self.log.info('restoring volume')
self.mopidy.restore_volume()
def handle_currently_playing(self, message):
current_track = self.mopidy.currently_playing()
if current_track is not None:
self.mopidy.lower_volume()
time.sleep(1)
if 'album' in current_track:
data = {'current_track': current_track['name'],
'artist': current_track['album']['artists'][0]['name']}
self.speak_dialog('currently_playing', data)
time.sleep(6)
self.mopidy.restore_volume()
def create_skill():
return MopidySkill()