-
Notifications
You must be signed in to change notification settings - Fork 24
/
__init__.py
executable file
·530 lines (456 loc) · 20.5 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# The MIT License (MIT)
#
# Copyright (c) 2016 Ethan Ward
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import subprocess
import json
import time
from os import makedirs, remove, listdir, path
from os.path import dirname, join, exists, expanduser, isfile, abspath, isdir
import shutil
from adapt.intent import IntentBuilder
from mycroft.skills.core import MycroftSkill, intent_handler
from mycroft.util.log import LOG
from fuzzywuzzy import fuzz, process as fuzz_process
from mycroft.audio import wait_while_speaking
from mycroft.messagebus.message import Message
import mycroft.client.enclosure.display_manager as DisplayManager
class PianobarSkill(MycroftSkill):
def __init__(self):
super(PianobarSkill, self).__init__(name="PianobarSkill")
self.process = None
self.piano_bar_state = None # 'playing', 'paused', 'autopause'
self.current_station = None
self._is_setup = False
self.vocabs = [] # keep a list of vocabulary words
self.pianobar_path = expanduser('~/.config/pianobar')
self._pianobar_initated = False
self.debug_mode = False
self.idle_count = 0
# Initialize settings values
self.settings["email"] = ""
self.settings["password"] = ""
self.settings["song_artist"] = ""
self.settings["song_title"] = ""
self.settings["song_album"] = ""
self.settings["station_name"] = ""
self.settings["station_count"] = 0
self.settings["stations"] = []
self.settings["last_played"] = None
self.settings['first_init'] = True # True = first run ever
subprocess.call(["killall", "-9", "pianobar"])
def initialize(self):
self._load_vocab_files()
# Check and then monitor for credential changes
self.settings.set_changed_callback(self.on_websettings_changed)
self.on_websettings_changed()
self.add_event('mycroft.stop', self.stop)
######################################################################
# 'Auto ducking' - pause playback when Mycroft wakes
def handle_listener_started(self, message):
if self.piano_bar_state == "playing":
self.handle_pause()
self.piano_bar_state = "autopause"
# Start idle check
self.idle_count = 0
self.cancel_scheduled_event('IdleCheck')
self.schedule_repeating_event(self.check_for_idle, None,
1, name='IdleCheck')
def check_for_idle(self):
if not self.piano_bar_state == "autopause":
self.cancel_scheduled_event('IdleCheck')
return
if DisplayManager.get_active() == '':
# No activity, start to fall asleep
self.idle_count += 1
if self.idle_count >= 2:
# Resume playback after 2 seconds of being idle
self.cancel_scheduled_event('IdleCheck')
self.handle_resume_song()
else:
self.idle_count = 0
######################################################################
def _register_all_intents(self):
""" Intents should only be registered once settings are inputed
to avoid conflicts with other music skills
"""
next_station_intent = IntentBuilder("PandoraNextStationIntent"). \
require("Next").require("Station").build()
self.register_intent(next_station_intent, self.handle_next_station)
list_stations_intent = IntentBuilder("PandoraListStationIntent"). \
optionally("Pandora").require("Query").require("Station").build()
self.register_intent(list_stations_intent, self.handle_list)
play_stations_intent = IntentBuilder("PandoraChangeStationIntent"). \
require("Change").require("Station").build()
self.register_intent(play_stations_intent, self.play_station)
# Messages from the skill-playback-control / common Audio service
self.add_event('mycroft.audio.service.pause', self.handle_pause)
self.add_event('mycroft.audio.service.resume', self.handle_resume_song)
self.add_event('mycroft.audio.service.next', self.handle_next_song)
def on_websettings_changed(self):
if not self._is_setup:
email = self.settings.get("email", "")
password = self.settings.get("password", "")
try:
if email and password:
self._configure_pianobar()
self._init_pianobar()
self._register_all_intents()
self._is_setup = True
except Exception as e:
LOG.error(e)
def _configure_pianobar(self):
# Initialize the Pianobar configuration file
if not exists(self.pianobar_path):
makedirs(self.pianobar_path)
config_path = join(self.pianobar_path, 'config')
with open(config_path, 'w+') as f:
# grabs the tls_key needed
tls_key = subprocess.check_output(
"openssl s_client -connect tuner.pandora.com:443 \
< /dev/null 2> /dev/null | openssl x509 -noout \
-fingerprint | tr -d ':' | cut -d'=' -f2",
shell=True)
config = 'audio_quality = medium\n' + \
'tls_fingerprint = {}\n' + \
'user = {}\n' + \
'password = {}\n' + \
'event_command = {}'
f.write(config.format(tls_key,
self.settings["email"],
self.settings["password"],
self._dir + '/event_command.py'))
# Raspbian requires adjustments to audio output to use PulseAudio
platform = self.config_core['enclosure'].get('platform')
if platform == 'picroft' or platform == 'mycroft_mark_1':
libao_path = expanduser('~/.libao')
if not isfile(libao_path):
with open(libao_path, 'w') as f:
f.write('dev=0\ndefault_driver=pulse')
self.speak_dialog("configured.please.reboot")
wait_while_speaking()
self.emitter.emit(Message('system.reboot'))
def _load_vocab_files(self):
# Keep a list of all the vocabulary words for this skill. Later
# these words will be removed from utterances as part of the station
# name.
vocab_dir = join(dirname(__file__), 'vocab', self.lang)
if path.exists(vocab_dir):
for vocab_type in listdir(vocab_dir):
if vocab_type.endswith(".voc"):
with open(join(vocab_dir, vocab_type), 'r') as voc_file:
for line in voc_file:
parts = line.strip().split("|")
vocab = parts[0]
self.vocabs.append(vocab)
else:
LOG.error('No vocab loaded, ' + vocab_dir + ' does not exist')
def start_monitor(self):
# Clear any existing event
self.stop_monitor()
# Schedule a new one every second to monitor/update display
self.schedule_repeating_event(self._poll_for_pianobar_update,
None, 1,
name='MonitorPianobar')
self.add_event('recognizer_loop:record_begin',
self.handle_listener_started)
def stop_monitor(self):
# Clear any existing event
self.cancel_scheduled_event('MonitorPianobar')
def _poll_for_pianobar_update(self, message):
# Checks once a second for feedback from Pianobar
# 'info_ready' file is created by the event_command.py
# script when Pianobar sends new track information.
info_ready_path = join(self.pianobar_path, 'info_ready')
if isfile(info_ready_path):
self._load_current_info()
try:
remove(info_ready_path)
except Exception as e:
LOG.error(e)
# Update the "Now Playing song"
LOG.info("State: "+str(self.piano_bar_state))
if self.piano_bar_state == "playing":
self.enclosure.mouth_text(self.settings["song_artist"] + ": " +
self.settings["song_title"])
def cmd(self, s):
self.process.stdin.write(s.encode())
self.process.stdin.flush()
def _init_pianobar(self):
if self.settings.get('first_init') is False:
return
# Run this exactly one time to prepare pianobar for usage
# by Mycroft.
try:
LOG.info("INIT PIANOBAR")
subprocess.call(["killall", "-9", "pianobar"])
self.process = subprocess.Popen(["pianobar"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
time.sleep(3)
self.cmd("0\n")
self.cmd("S")
time.sleep(0.5)
self.process.kill()
self.settings['first_init'] = False
self._load_current_info()
except Exception as e:
LOG.exception('Failed to connect to Pandora')
self.speak_dialog('wrong.credentials')
self.process = None
def _load_current_info(self):
# Load the 'info' file created by Pianobar when changing tracks
info_path = join(self.pianobar_path, 'info')
# this is a hack to remove the info_path
# if it's a directory. An earlier version
# of code may sometimes create info_path as
# a directory instead of a file path
# date: 02-18
if isdir(info_path):
shutil.rmtree(info_path)
if not exists(info_path):
with open(info_path, 'w+'):
pass
with open(info_path, 'r') as f:
info = json.load(f)
# Save the song info for later display
self.settings["song_artist"] = info["artist"]
self.settings["song_title"] = info["title"]
self.settings["song_album"] = info["album"]
self.settings["station_name"] = info["stationName"]
if self.debug_mode:
LOG.info("Station name: " + str(self.settings['station_name']))
self.settings["station_count"] = int(info["stationCount"])
self.settings["stations"] = []
for index in range(self.settings["station_count"]):
station = "station" + str(index)
self.settings["stations"].append(
(info[station].replace("Radio", ""), index))
if self.debug_mode:
LOG.info("Stations: "+str(self.settings["stations"]))
# self.settings.store()
def _launch_pianobar_process(self):
try:
LOG.info("Starting Pianobar process")
subprocess.call(["killall", "-9", "pianobar"])
time.sleep(1)
# start pandora
if self.debug_mode:
self.process = \
subprocess.Popen(["pianobar"], stdin=subprocess.PIPE)
else:
self.process = subprocess.Popen(["pianobar"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
self.current_station = "0"
self.cmd("0\n")
self.handle_pause()
time.sleep(2)
self._load_current_info()
LOG.info("Pianobar process initialized")
except Exception:
LOG.exception('Failed to connect to Pandora')
self.speak_dialog('wrong.credentials')
self.process = None
def _extract_station(self, utterance):
"""
parse the utterance for station names
and return station with highest probability
"""
try:
# TODO: Internationalize
common_words = [" to ", " on ", " pandora", " play"]
for vocab in self.vocabs:
utterance = utterance.replace(vocab, "")
# strip out other non important words
for words in common_words:
utterance = utterance.replace(words, "")
utterance.lstrip()
stations = [station[0] for station in self.settings["stations"]]
probabilities = fuzz_process.extractOne(
utterance, stations, scorer=fuzz.ratio)
if self.debug_mode:
LOG.info("Probabilities: " + str(probabilities))
if probabilities[1] > 70:
station = probabilities[0]
return station
else:
return None
except Exception as e:
LOG.info(e)
return None
def _play_station(self, station, dialog=None):
LOG.info("Starting: "+str(station))
self._launch_pianobar_process()
if not self.process:
return
if dialog:
self.speak_dialog(dialog, {"station": station})
else:
if station:
self.speak_dialog("playing.station", {"station": station})
self.enclosure.mouth_think()
if station:
for channel in self.settings.get("stations"):
if station == channel[0]:
self.cmd("s")
self.current_station = str(channel[1])
station_number = str(channel[1]) + "\n"
self.cmd(station_number)
self.piano_bar_state = "playing"
self.settings["last_played"] = channel
self.start_monitor()
else:
time.sleep(2) # wait for pianobar to loading
if self.debug_mode:
LOG.info(self.settings.get('stations'))
# try catch block because some systems
# may not load pianobar info in time
try:
channel = self.settings.get("stations")[0]
if self.debug_mode:
LOG.info(channel)
if channel:
self.speak_dialog(
"playing.station", {"station": channel[0]}
)
station_number = str(channel[1]) + "\n"
if self.debug_mode:
LOG.info(station_number)
self.cmd(station_number)
self.settings["last_played"] = channel
else:
raise ValueError
except Exception as e:
LOG.info(e)
self.speak_dialog("playing.station", {"station": "pandora"})
self.current_station = "0"
self.cmd("0\n")
self.handle_resume_song()
self.piano_bar_state = "playing"
self.start_monitor()
@intent_handler(IntentBuilder("").require("Play").require("Pandora"))
def play_pandora(self, message=None):
if self._is_setup:
# Examine the whole utterance to see if the user requested a
# station by name
station = self._extract_station(message.data["utterance"])
if self.debug_mode:
LOG.info("Station request:" + str(station))
dialog = None
if not station:
last_played = self.settings.get("last_played")
if last_played:
station = last_played[0]
dialog = "resuming.last.station"
else:
# default to the first station in the list
if self.settings.get("stations"):
station = self.settings["stations"][0][0]
# Play specified station
self._play_station(station, dialog)
else:
# Lead user to setup for Pandora
self.speak_dialog("please.register.pandora")
def handle_next_song(self, message=None):
if self.process and self.piano_bar_state == "playing":
self.enclosure.mouth_think()
self.cmd("n")
self.piano_bar_state = "playing"
self.start_monitor()
def handle_next_station(self, message=None):
if self.process and self.settings.get("stations"):
new_station = int(self.current_station) + 1
if new_station >= int(self.settings.get("station_count", 0)):
new_station = 0
new_station = self.settings["stations"][new_station][0]
self._play_station(new_station)
def handle_pause(self, message=None):
if self.process:
self.cmd("S")
self.process.stdin.flush()
self.piano_bar_state = "paused"
self.stop_monitor()
def handle_resume_song(self, message=None):
if self.process:
self.cmd("P")
self.piano_bar_state = "playing"
self.start_monitor()
def play_station(self, message=None):
if self._is_setup:
# Examine the whole utterance to see if the user requested a
# station by name
station = self._extract_station(message.data["utterance"])
if station is not None:
self._play_station(station)
else:
self.speak_dialog("no.matching.station")
else:
# Lead user to setup for Pandora
self.speak_dialog("please.register.pandora")
def handle_list(self, message=None):
is_playing = self.piano_bar_state == "playing"
if is_playing:
self.handle_pause()
# build the list of stations
l = []
for station in self.settings.get("stations"):
l.append(station[0]) # [0] = name
if len(l) == 0:
self.speak_dialog("no.stations")
return
# read the list
if len(l) > 1:
list = ', '.join(l[:-1]) + " " + \
self.translate("and") + " " + l[-1]
else:
list = str(l)
self.speak_dialog("subscribed.to.stations", {"stations": list})
if is_playing:
wait_while_speaking()
self.handle_resume_song()
def stop(self):
if self.piano_bar_state and not self.piano_bar_state == "paused":
self.handle_pause()
self.enclosure.mouth_reset()
return True
@intent_handler(IntentBuilder("").require("Pandora").
require("Debug").require("On"))
def debug_on_intent(self, message=None):
if not self.debug_mode:
self.debug_mode = True
self.speak_dialog("entering.debug.mode")
@intent_handler(IntentBuilder("").require("Pandora").
require("Debug").require("Off"))
def debug_off_intent(self, message=None):
if self.debug_mode:
self.debug_mode = False
self.speak_dialog("leaving.debug.mode")
def shutdown(self):
self.stop_monitor()
# Clean up before shutting down the skill
if self.piano_bar_state == "playing":
self.enclosure.mouth_reset()
if self.process:
self.cmd("q")
super(PianobarSkill, self).shutdown()
def create_skill():
return PianobarSkill()