forked from dingdang-robot/dingdang-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetEaseMusic.py
606 lines (541 loc) · 21.4 KB
/
NetEaseMusic.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# -*- coding: utf-8-*-
# 网易云音乐播放插件
import logging
import threading
import hashlib
import time
import subprocess
import sys
import os
import re
import random
from MusicBoxApi import api as NetEaseApi
import eyed3
from random import shuffle
reload(sys)
sys.setdefaultencoding('utf8')
#import cProfile, pstats, StringIO
# Standard module stuff
WORDS = ["YINYUE"]
SLUG = "netease_music"
def handle(text, mic, profile, wxbot=None):
"""
Responds to user-input, typically speech text
Arguments:
text -- user-input, typically transcribed speech
mic -- used to interact with the user (for both input and output)
profile -- contains information related to the user (e.g., phone
number)
wxbot -- wechat bot instance
"""
logger = logging.getLogger(__name__)
kwargs = {}
kwargs['mic'] = mic
kwargs['profile'] = profile
logger.debug("Preparing to start netease music module")
try:
netease_wrapper = NetEaseWrapper(**kwargs)
except Exception, e:
logger.debug(e)
logger.error("Couldn't connect to NetEase server", exc_info=True)
mic.say(u"访问网易云音乐失败了,请稍后再试", cache=True)
return
persona = 'DINGDANG'
if 'robot_name' in profile:
persona = profile['robot_name']
robot_name_cn = u'叮当'
if 'robot_name_cn' in profile:
robot_name_cn = profile['robot_name_cn']
logger.debug("Starting music mode")
music_mode = MusicMode(persona, robot_name_cn, mic, netease_wrapper, wxbot)
music_mode.stop = False
# 登录网易云音乐
account = ''
password = ''
report = False
local_default = False
if SLUG in profile:
if 'account' in profile[SLUG]:
account = profile[SLUG]['account']
if 'password' in profile[SLUG]:
password = profile[SLUG]['password']
if 'report' in profile[SLUG]:
report = profile[SLUG]['report']
if 'local_default' in profile[SLUG]:
local_default = profile[SLUG]['local_default']
if account == '' or password == '':
mic.say("请先配置好账户信息再找我播放音乐", cache=True)
return
has_login = False
home_dir = os.path.expandvars('$HOME')
user_info = os.path.join(home_dir, 'userInfo')
if not (os.path.exists(user_info)):
mic.say("稍等,正在为您登录网易云音乐", cache=True)
res = music_mode.login(account, password, report)
if res:
mic.say("登录成功", cache=True)
has_login = True
else:
music_mode.read_login_info(user_info, report)
has_login = True
if not has_login:
mic.say("登录失败, 退出播放. 请检查配置, 稍后再试", cache=True)
return
if wxbot is not None:
wxbot.music_mode = music_mode
pattern = re.compile(ur'(我想听|来一首)([,]?)([\u4e00-\u9fa5]*)')
text_utf8 = text.decode('utf-8')
if pattern.match(text_utf8) and text != u'播放音乐':
m = pattern.search(text_utf8)
song_name = m.group(3)
if song_name != '':
music_mode.handleForever(
play_type=2, song_name=song_name) # 2: 播放指定歌曲
elif any(word in text for word in [u"歌单", u"我的"]):
music_mode.handleForever(play_type=1) # 1: 用户歌单
else:
# 默认播放本地音乐
if local_default:
music_mode.handleForever(play_type=3) # 3: 本地音乐
else:
music_mode.handleForever(play_type=0) # 0: 推荐榜单
logger.debug("Exiting music mode")
return
def isValid(text):
"""
Returns True if the input is related to music.
Arguments:
text -- user-input, typically transcribed speech
"""
return any(word in text for word in [u"听歌", u"音乐", u"播放",
u"我想听", u"唱歌", u"唱首歌",
u"歌单", u"榜单"])
# The interesting part
class MusicMode(object):
def __init__(self, PERSONA, robot_name_cn, mic,
netease_wrapper, wxbot=None):
self._logger = logging.getLogger(__name__)
self.persona = PERSONA
self.robot_name_cn = robot_name_cn
self.music = netease_wrapper
self.mic = mic
self.wxbot = wxbot
self.search_mode = False
self.to_listen = True
self.to_report = False
self.delegating = False
if self.wxbot is not None:
self.msg_thread = threading.Thread(target=self.wxbot.proc_msg)
def read_login_info(self, user_info, report=False):
self.to_report = report
self.music.read_login_info(user_info)
def login(self, account, password, report=False):
self.to_report = report
return self.music.login(account, password)
def delegateInput(self, input, call_by_wechat=False):
command = input.upper()
if command.startswith(self.robot_name_cn + ": "):
return
if call_by_wechat:
self._logger.debug('called by wechat')
self.music.stop()
time.sleep(.1)
# check if input is meant to start the music module
if u"榜单" in command:
self.mic.say(u"播放榜单音乐", cache=True)
self.music.update_playlist_by_type(0)
self.music.play(self.to_report)
return
elif u"歌单" in command:
self.music.update_playlist_by_type(1)
self.music.play(self.to_report)
return
elif any(ext in command for ext in [u"本地", u"本机"]):
self.music.update_playlist_by_type(3)
self.music.play(self.to_report)
return
elif any(ext in command for ext in [u"停止聆听", u"关闭聆听", u"别听我的"]):
if self.wxbot is None or not self.wxbot.is_login:
self.mic.say(u"您还未登录微信,不能关闭语音交互功能", cache=True)
return
self.mic.say(u"关闭语音交互功能", cache=True)
self.to_listen = False
self.music.play(False)
return
elif any(ext in command for ext in [
u"恢复聆听", u"开始聆听", u"开启聆听", u"听我的"]):
self.mic.say(u"开启语音交互功能", cache=True)
self.to_listen = True
self.music.play(False)
return
elif u"暂停" in command:
self.mic.say(u"暂停播放", cache=True)
self.music.pause()
return
elif any(ext in command for ext in [u"结束", u"退出", u"停止"]):
self.music.exit()
self.mic.say(u"结束播放", cache=True)
if self.wxbot is not None:
self.wxbot.music_mode = None
return
elif any(ext in command for ext in [u"大声", u"大声点", u"大点声"]):
self.mic.say(u"大点声", cache=True)
self.music.increase_volume()
return
elif any(ext in command for ext in [u"小声", u"小点声", u"小声点"]):
self.mic.say(u"小点声", cache=True)
self.music.decrease_volume()
return
elif any(
ext in command for ext in [
u'下一首', u"下首歌", u"切歌",
u"下一首歌", u"换首歌", u"切割",
u"那首歌"]):
self.mic.say(u"下一首歌", cache=True)
self.music.next()
return
elif any(ext in command for ext in [u'上一首', u'上一首歌', u'上首歌']):
self.mic.say(u"上一首歌", cache=True)
self.music.previous()
return
elif any(ext in command for ext in [u'搜索', u'查找']):
if call_by_wechat:
self.search_mode = True
self.mic.say(u"请直接回复要搜索的关键词", cache=True)
return
else:
self.mic.say(u"请在滴一声后告诉我您要搜索的关键词", cache=True)
input = self.mic.activeListen(MUSIC=True)
if input is None or input.strip() == '':
self.mic.say("没有听到关键词呢,请重新叫我查找吧", cache=True)
self.music.play(False)
return
self.mic.say(u'正在为您搜索%s' % input)
self.music.update_playlist_by_type(2, input)
self.music.play(self.to_report)
return
elif u'什么歌' in command:
self.mic.say(u"正在播放的是%s的%s" % (
self.music.song['artist'],
self.music.song['song_name']))
self.music.play(False)
return
elif u'随机' in command:
self.mic.say(u"随机播放", cache=True)
self.music.randomize()
return
elif u'顺序' in command:
self.mic.say(u"顺序播放", cache=True)
self.music.serialize()
return
elif any(ext in command for ext in [u"播放", u"继续", u"我想听", u"来一首"]):
pattern = re.compile(ur'(播放|我想听|来一首)([,]?)([\u4e00-\u9fa5]+)')
text_utf8 = command.decode('utf-8')
song_name = ''
if pattern.match(text_utf8):
m = pattern.search(text_utf8)
song_name = m.group(3)
if song_name != '':
self.music.update_playlist_by_type(2, song_name)
elif u'即将播放' not in command:
self.music.play(self.to_report)
return
elif self.search_mode:
self.search_mode = False
input = command
if input is None or input.strip() == '':
self.mic.say("没有听到关键词呢,请重新叫我查找吧", cache=True)
self.music.play(False)
return
self.mic.say(u'正在为您搜索%s' % input)
self.music.update_playlist_by_type(2, input)
self.music.play(self.to_report)
else:
self.mic.say(u"没有听懂呢。要退出播放,请说退出播放", cache=True)
self.music.play(False)
return
return
def handleForever(self, play_type=0, song_name=''):
"""
进入音乐播放
play_type - 0:播放推荐榜单;1:播放用户歌单
"""
if song_name != '':
self.music.update_playlist_by_type(2, song_name)
else:
self.music.update_playlist_by_type(play_type)
self.music.start()
if self.wxbot is not None:
self.msg_thread.start()
while True:
if self.music.is_stop:
self._logger.info('Stop Netease music mode')
return
if not self.to_listen or self.delegating:
self._logger.info("Listening mode is disabled.")
continue
try:
self._logger.info('离线唤醒监听中')
threshold, transcribed = self.mic.passiveListen(self.persona)
except Exception, e:
self._logger.debug(e)
threshold, transcribed = (None, None)
if not transcribed or not threshold:
self._logger.info("Nothing has been said or transcribed.")
continue
# 当听到呼叫机器人名字时,停止播放
self.music.stop()
time.sleep(.1)
# 听用户说话
input = self.mic.activeListen(MUSIC=True)
if input:
if any(ext in input for ext in [u"结束", u"退出", u"停止"]):
self.mic.say(u"结束播放", cache=True)
self.music.stop()
self.music.exit()
return
if not self.delegating:
self.delegating = True
self.delegateInput(input)
self.delegating = False
else:
self.mic.say(u"什么?", cache=True)
if not self.music.is_pause:
self.music.play(False)
class NetEaseWrapper(threading.Thread):
def __init__(self, mic, profile):
super(NetEaseWrapper, self).__init__()
self.logger = logging.getLogger(__name__)
self.cond = threading.Condition()
self.netease = NetEaseApi.NetEase()
self.mic = mic
self.profile = profile
self.userId = ""
self.volume = 0.7
self.song = None # 正在播放的曲目信息
self.idx = -1 # 正在播放的曲目序号
self.random = False
self.playlist = []
self.is_pause = False
self.is_stop = False
def set_cond(self, cond):
self.cond = cond
def update_playlist_by_type(self, play_type, keyword=''):
if play_type == 0:
# 播放热门榜单音乐
self.playlist = self.get_top_songlist()
elif play_type == 1:
# 播放用户歌单
user_playlist = self.get_user_playlist()
if user_playlist > 0:
self.playlist = self.get_song_list_by_playlist_id(
user_playlist[0]['id'])
if len(self.playlist) == 0:
self.mic.say("用户歌单没有歌曲,改为播放推荐榜单", cache=True)
self.playlist = self.get_top_songlist()
else:
self.mic.say("当前用户没有歌单,改为播放推荐榜单", cache=True)
self.playlist = self.get_top_songlist()
elif play_type == 2:
# 搜索歌曲
self.playlist = self.search_by_name(keyword)
elif play_type == 3:
self.playlist = self.get_local_songlist()
def get_local_songlist(self): # 本地音乐
local_path = ''
if 'local_path' in self.profile[SLUG]:
local_path = self.profile[SLUG]['local_path']
playlist = []
for (dirpath, dirnames, filenames) in os.walk(local_path):
# f.extend(filenames)
for filename in filenames:
try:
# only mp3 accept
if os.path.splitext(filename)[1] != ".mp3":
continue
# read mp3 properties and add to the playlist
mp3_path = dirpath + filename
audiofile = eyed3.load(mp3_path)
music_info = {}
if audiofile.tag:
if audiofile.tag.title:
music_info.setdefault(
"song_name", audiofile.tag.title)
music_info.setdefault("artist", audiofile.tag.artist)
else:
music_info.setdefault(
"song_name", os.path.splitext(filename)[0])
music_info.setdefault("artist", "")
music_info.setdefault("song_id", "0")
music_info.setdefault("album_name", "")
music_info.setdefault("mp3_url", "'{}'".format(mp3_path))
music_info.setdefault("playTime", int(
audiofile.info.time_secs) * 1000)
music_info.setdefault("quality", "")
playlist.append(music_info)
except Exception, e:
self.logger.error(e)
pass
break
# 随机本地音乐列表顺序
if 'local_shuffle' in self.profile[SLUG]:
if self.profile[SLUG]['local_shuffle']:
shuffle(playlist)
return playlist
def get_top_songlist(self): # 热门单曲
music_list = self.netease.top_songlist()
datalist = self.netease.dig_info(music_list, 'songs')
playlist = []
for data in datalist:
music_info = {}
music_info.setdefault("song_id", data.get("song_id"))
music_info.setdefault("song_name", data.get("song_name"))
music_info.setdefault("artist", data.get("artist"))
music_info.setdefault("album_name", data.get("album_name"))
music_info.setdefault("mp3_url", data.get("mp3_url"))
music_info.setdefault("playTime", data.get("playTime"))
music_info.setdefault("quality", data.get("quality"))
playlist.append(music_info)
return playlist
def read_login_info(self, user_info):
assert(os.path.exists(user_info))
with open(user_info) as f:
self.userId = f.readline()
def login(self, username, password): # 用户登陆
password = hashlib.md5(password).hexdigest()
login_info = self.netease.login(username, password)
if login_info['code'] == 200:
res = True
userId = login_info.get('profile').get('userId')
self.userId = userId
home_dir = os.path.expandvars('$HOME')
user_info = os.path.join(home_dir, 'userInfo')
file = open(user_info, 'w')
file.write(str(userId))
file.close()
else:
res = False
return res
def get_user_playlist(self): # 获取用户歌单
play_list = self.netease.user_playlist(self.userId) # 用户歌单
return play_list
def get_song_list_by_playlist_id(self, playlist_id):
songs = self.netease.playlist_detail(playlist_id)
song_list = self.netease.dig_info(songs, 'songs')
return song_list
def search_by_name(self, song_name):
data = self.netease.search(song_name)
song_ids = []
if 'songs' in data['result']:
if 'mp3Url' in data['result']['songs']:
songs = data['result']['songs']
else:
for i in range(0, len(data['result']['songs'])):
song_ids.append(data['result']['songs'][i]['id'])
songs = self.netease.songs_detail(song_ids)
song_list = self.netease.dig_info(songs, 'songs')
return song_list
def current_song(self):
if self.song is not None:
return self.song['song_name']
else:
return ''
def run(self):
while True:
if self.cond.acquire():
self.play()
self.pick_next()
def play(self, report=False):
if self.is_pause:
self.is_pause = False
if self.idx < len(self.playlist):
if self.idx == -1:
self.idx = 0
if not self.random:
song = self.playlist[self.idx]
else:
song = random.choice(self.playlist)
self.song = song
subprocess.Popen("pkill play", shell=True)
# pr = cProfile.Profile()
# pr.enable()
# if song['mp3_url'] contains 'http://',
# means music from internet, not local
if "http://" in song['mp3_url']:
song['mp3_url'] = self.netease.songs_detail_new_api(
[song['song_id']])[0]['url']
# pr.disable()
# s = StringIO.StringIO()
# sortby = 'cumulative'
# ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
# ps.print_stats()
# print s.getvalue()
mp3_url = song['mp3_url']
if mp3_url is None:
self.next()
self.cond.wait()
try:
if report:
self.mic.say(u"即将播放:%s,%s" % (
song['artist'], song['song_name']))
time.sleep(.1)
subprocess.Popen("play -v %f %s" % (
self.volume, mp3_url), shell=True, stdout=subprocess.PIPE)
self.cond.notify()
self.cond.wait(int(song.get('playTime')) / 1000)
except Exception:
pass
else:
try:
subprocess.Popen("pkill play", shell=True)
self.cond.notify()
self.cond.wait()
except Exception:
pass
def notify(self):
if self.cond.acquire():
self.cond.notifyAll()
self.cond.release()
def previous(self):
self.idx -= 2
if self.idx < 0:
self.idx = len(self.playlist) - 1
self.notify()
def pick_next(self):
self.idx += 1
if self.idx > len(self.playlist) - 1:
self.idx = 0
def next(self):
self.notify()
def randomize(self):
self.random = True
self.next()
def serialize(self):
self.random = False
self.notify()
def increase_volume(self):
self.volume += .1
if self.volume > 1:
self.volume = 1
self.notify()
def decrease_volume(self):
self.volume -= .1
if self.volume < 0:
self.volume = 0
self.notify()
def stop(self):
try:
subprocess.Popen("pkill play", shell=True)
self.cond.notifyAll()
self.cond.release()
self.cond.wait()
except Exception:
pass
def pause(self):
self.is_pause = True
# 暂不支持断点续播,因此暂停和停止相同处理
self.stop()
def exit(self):
self.is_stop = True
self.playlist = []
self.notify()