-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path__init__.py
165 lines (129 loc) · 5.27 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
import hashlib
from mycroft import intent_file_handler
from mycroft.skills.common_play_skill import CommonPlaySkill, CPSMatchLevel
from mycroft.skills.audioservice import AudioService
from mycroft.api import DeviceApi
from .emby_croft import EmbyCroft
class Emby(CommonPlaySkill):
def __init__(self):
super().__init__()
self._setup = False
self.audio_service = None
self.emby_croft = None
self.device_id = hashlib.md5(
('Emby'+DeviceApi().identity.uuid).encode())\
.hexdigest()
def initialize(self):
pass
@intent_file_handler('emby.intent')
def handle_emby(self, message):
self.log.log(20, message.data)
# first thing is connect to emby or bail
if not self.connect_to_emby():
self.speak_dialog('configuration_fail')
return
# determine intent
intent, intent_type = EmbyCroft.determine_intent(message.data)
songs = []
try:
songs = self.emby_croft.handle_intent(intent, intent_type)
except Exception as e:
self.log.log(20, e)
self.speak_dialog('play_fail', {"media": intent})
if not songs or len(songs) < 1:
self.log.log(20, 'No songs Returned')
self.speak_dialog('play_fail', {"media": intent})
else:
# setup audio service and play
self.audio_service = AudioService(self.bus)
self.speak_playing(intent)
self.audio_service.play(songs, message.data['utterance'])
def speak_playing(self, media):
data = dict()
data['media'] = media
self.speak_dialog('emby', data)
@intent_file_handler('diagnostic.intent')
def handle_diagnostic(self, message):
self.log.log(20, message.data)
self.speak_dialog('diag_start', wait=True)
# connect to emby for diagnostics
self.connect_to_emby(diagnostic=True)
connection_success, info = self.emby_croft.diag_public_server_info()
if connection_success:
self.speak_dialog('diag_public_info_success', info, wait=True)
else:
self.speak_dialog('diag_public_info_fail', {'host': self.settings['hostname']}, wait=True)
self.speak_dialog('general_check_settings_logs', wait=True)
self.speak_dialog('diag_stop')
return
if not self.connect_to_emby():
self.speak_dialog('diag_auth_fail', wait=True)
self.speak_dialog('diag_stop')
return
else:
self.speak_dialog('diag_auth_success', wait=True)
self.speak_dialog('diagnostic')
def stop(self):
pass
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.audio_service.play(data[phrase])
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.
"""
# first thing is connect to emby or bail
if not self.connect_to_emby():
return None
self.log.log(20, phrase)
match_type, songs = self.emby_croft.parse_common_phrase(phrase)
if match_type and songs:
match_level = None
if match_type is not None:
self.log.log(20, 'Found match of type: ' + match_type)
if match_type == 'song' or match_type == 'album':
match_level = CPSMatchLevel.TITLE
elif match_type == 'artist':
match_level = CPSMatchLevel.ARTIST
self.log.log(20, 'match level' + str(match_level))
song_data = dict()
song_data[phrase] = songs
self.log.log(20, "First 3 item urls returned")
max_songs_to_log = 3
songs_logged = 0
for song in songs:
self.log.log(20, song)
songs_logged = songs_logged + 1
if songs_logged >= max_songs_to_log:
break
return phrase, match_level, song_data
else:
return None
def connect_to_emby(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
try:
self.emby_croft = EmbyCroft(
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.log(20, "failed to connect to emby, error: {0}".format(str(e)))
return auth_success
def create_skill():
return Emby()