-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
281 lines (240 loc) · 10.8 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
281
import hashlib
from mycroft.skills.common_play_skill import CommonPlaySkill, CPSMatchLevel
from ovos_workshop.decorators import intent_file_handler
from ovos_utils.parse import match_one
from ovos_audio.audio import AudioService
from ovos_backend_client.api import DeviceApi
from random import shuffle
from .jellyfin_croft import JellyfinCroft
class Jellyfin(CommonPlaySkill):
def __init__(self):
super().__init__()
self._setup = False
self.audio_service = None
self.jellyfin_croft = None
self.songs = []
self.device_id = hashlib.md5(
('Jellyfin'+DeviceApi().identity.uuid).encode())\
.hexdigest()
def CPS_match_query_phrase(self, phrase):
""" This method responds whether the skill can play the input phrase.
The method is invoked by the PlayBackControlSkill.
Returns: tuple (matched phrase(str),
match level(CPSMatchLevel),
optional data(dict))
or None if no match was found.
"""
# slower devices like raspberry pi's need a bit more time.
self.CPS_extend_timeout(10)
# first thing is connect to jellyfin or bail
if not self.connect_to_jellyfin():
return None
self.log.debug("CPS Phrase: " + phrase)
match_type, self.songs = self.jellyfin_croft.parse_common_phrase(phrase)
if match_type and self.songs:
match_level = None
if match_type != None:
self.log.info('Found match of type: ' + match_type)
if match_type == 'song' or match_type == 'album' or match_type == 'playlist' or match_type == 'genre':
match_level = CPSMatchLevel.TITLE
elif match_type == 'artist':
match_level = CPSMatchLevel.ARTIST
self.log.info('match level :' + str(match_level))
song_data = dict()
song_data[phrase] = self.songs
self.log.info("First 3 item urls returned")
max_songs_to_log = 3
songs_logged = 0
for song in self.songs:
self.log.debug(song)
songs_logged = songs_logged + 1
if songs_logged >= max_songs_to_log:
break
return phrase, CPSMatchLevel.TITLE, song_data
else:
return None
def CPS_start(self, phrase, data):
""" Starts playback.
Called by the playback control skill to start playback if the
skill is selected (has the best match level)
"""
# setup audio service
self.audio_service = AudioService(self.bus)
self.speak_playing(phrase)
self.audio_service.play(data[phrase])
self.CPS_send_tracklist(self.jellyfin_croft.get_track_list())
def connect_to_jellyfin(self, diagnostic=False):
"""
Attempts to connect to the server based on the config
if diagnostic is False an attempt to auth is also made
returns true/false on success/failure respectively
:return:
"""
auth_success = False
self.log.debug("Testing connection to: " + self.settings.get("hostname"))
try:
self.jellyfin_croft = JellyfinCroft(
self.settings.get("hostname") + ":" + str(self.settings.get("port")),
self.settings.get("username"), self.settings.get("password"),
self.device_id, diagnostic)
auth_success = True
except Exception as e:
self.log.info("failed to connect to jellyfin, error: {0}".format(str(e)))
return auth_success
def initialize(self):
pass
@intent_file_handler('jellyfin.intent')
def handle_jellyfin(self, message):
self.log.info(message.data)
# first thing is connect to jellyfin or bail
if not self.connect_to_jellyfin():
self.speak_dialog('configuration_fail')
return
# determine intent
intent, intent_type = JellyfinCroft.determine_intent(message.data)
self.songs = []
try:
self.songs = self.jellyfin_croft.handle_intent(intent, intent_type)
except Exception as e:
self.log.info(e)
self.speak_dialog('play_fail', {"media": intent})
if not self.songs or len(self.songs) < 1:
self.log.info('No songs Returned')
self.speak_dialog('play_fail', {"media": intent})
else:
# setup audio service and play
self.audio_service = AudioService(self.bus)
backends = self.audio_service.available_backends()
self.log.debug("BACKENDS. VLC Recommended")
for key , value in backends.items():
self.log.debug(str(key) + " : " + str(value))
self.speak_playing(intent)
self.audio_service.play(self.songs, message.data['utterance'])
# Play favorites
@intent_file_handler('isfavorite.intent')
def handle_is_favorite(self, message):
self.log.info(message.data)
if not self.connect_to_jellyfin():
self.speak_dialog('configuration_fail')
return
self.songs = self.jellyfin_croft.get_favorites()
if not self.songs or len(self.songs) < 1:
self.log.info('No songs Returned')
self.speak_dialog('play_fail', {"media": "favorites"})
else:
# setup audio service and play
self.audio_service = AudioService(self.bus)
backends = self.audio_service.available_backends()
self.log.debug("BACKENDS. VLC Recommended")
for key , value in backends.items():
self.log.debug(str(key) + " : " + str(value))
self.speak_dialog('isfavorite')
self.audio_service.play(self.songs, message.data['utterance'])
@intent_file_handler('shuffle.intent')
def handle_shuffle(self, message):
self.log.info(message.data)
# Back up meta data
track_meta = self.jellyfin_croft.get_all_meta()
# first thing is connect to jellyfin or bail
if not self.connect_to_jellyfin():
self.speak_dialog('configuration_fail')
return
if not self.songs or len(self.songs) < 1:
self.log.info('No songs Returned')
self.speak_dialog('shuffle_fail')
else:
self.log.info(track_meta)
# setup audio service and, suffle play
shuffle(self.songs)
self.audio_service = AudioService(self.bus)
self.speak_dialog('shuffle')
self.audio_service.play(self.songs, message.data['utterance'])
# Restore meta data
self.jellyfin_croft.set_meta(track_meta)
def speak_playing(self, media):
data = dict()
data['media'] = media
self.speak_dialog('jellyfin', data)
@intent_file_handler('playingsong.intent')
def handle_playing(self, message):
track = "Unknown"
artist = "Unknown"
if self.audio_service.is_playing:
# See if I can get the current track index instead
track = self.audio_service.track_info()['name']
artist = self.audio_service.track_info()['artists']
if artist != [None]:
self.speak_dialog('whatsplaying', {'track' : track, 'artist': artist})
else:
track = self.jellyfin_croft.get_meta(self.audio_service.track_info()['name'])
if track != False:
self.speak_dialog('whatsplaying', {'track' : track['Name'], 'artist': track['Artists']})
else:
self.speak_dialog('notrackinfo')
else:
self.speak_dialog('notplaying')
@intent_file_handler('playlist.intent')
def handle_playlist_add(self, message):
if self.audio_service.is_playing:
track = self.audio_service.track_info()['name']
track_name = self.jellyfin_croft.get_meta(track)
add_to = self.jellyfin_croft.add_to_playlist(track, message.data.get('playlist_name'))
if add_to == True:
self.speak_dialog('playlist', {'media' : track_name['Name'], 'playlist_name' : message.data.get('playlist_name')})
return
self.speak_dialog('playlist_fail', {'media' : track_name['Name'], 'playlist_name' : message.data.get('playlist_name')})
return
# Intent for creating a new playlist
@intent_file_handler('createplaylist.intent')
def handle_create_playlist(self, message):
if not self.connect_to_jellyfin():
return None
confirm = self.ask_yesno('playlistnameconfirm', {'playlist_name' : message.data.get('playlist_name')})
if confirm == 'yes':
create_new = self.jellyfin_croft.create_playlist(message.data.get('playlist_name'))
if create_new == True:
self.speak_dialog('createplaylist', {'playlist_name' : message.data.get('playlist_name')})
return
else:
return
self.speak_dialog('createplaylist_fail', {'playlist_name' : message.data.get('playlist_name')})
return
# Intent foor marking a song as favorite
@intent_file_handler('favorite.intent')
def handle_favorite(self, message):
if self.audio_service.is_playing:
track = self.audio_service.track_info()['name']
track_name = self.jellyfin_croft.get_meta(track)
favorite = self.jellyfin_croft.favorite(track)
if favorite == True:
self.speak_dialog('favorite', {'track_name' : track_name['Name']})
return
else:
self.speak_dialog('favorite_fail', {'track_name' : track_name['Name']})
return
@intent_file_handler('diagnostic.intent')
def handle_diagnostic(self, message):
self.log.info(message.data)
self.speak_dialog('diag_start')
# connect to jellyfin for diagnostics
self.connect_to_jellyfin(diagnostic=True)
connection_success, info = self.jellyfin_croft.diag_public_server_info()
if connection_success:
self.speak_dialog('diag_public_info_success', info)
else:
self.speak_dialog('diag_public_info_fail', {'host': self.settings.get('hostname')})
self.speak_dialog('general_check_settings_logs')
self.speak_dialog('diag_stop')
return
if not self.connect_to_jellyfin():
self.speak_dialog('diag_auth_fail')
self.speak_dialog('diag_stop')
return
else:
self.speak_dialog('diag_auth_success')
self.speak_dialog('diagnostic')
def stop(self):
pass
# TODO: Remove create_skill() function
def create_skill():
return Jellyfin()