forked from rickyphewitt/emby-skill
-
Notifications
You must be signed in to change notification settings - Fork 4
/
__init__.py
229 lines (191 loc) · 8.41 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
import hashlib
from mycroft.skills.common_play_skill import CommonPlaySkill, CPSMatchLevel
from mycroft import intent_file_handler
from mycroft.util.parse import match_one
from mycroft.skills.audioservice import AudioService
from mycroft.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["hostname"])
try:
self.jellyfin_croft = JellyfinCroft(
self.settings["hostname"] + ":" + str(self.settings["port"]),
self.settings["username"], self.settings["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'])
@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_file_handler('diagnostic.intent')
def handle_diagnostic(self, message):
self.log.info(message.data)
self.speak_dialog('diag_start')
# connec 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['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
def create_skill():
return Jellyfin()