-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathif_music.py
446 lines (408 loc) · 15.4 KB
/
if_music.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
#coding=utf-8
import requests
import json
import arcade
import time
from pydub import AudioSegment
import tts
# import dealCookie
from play import play
from config import config
from const_config import qqid
from loguru import logger
header={"Connection":"close"}
musicsound=None
musicplayer=None
interrupted_music=False
search=False
cookiewrong=False
startagain=False
words=''
music=requests.session()
advice = []
order=0
def musicdetect(text):
global interrupted_music,words,search,startagain
if text=='播放音乐。' or text=='播放歌曲。' or text=='放一首音乐。' or text=='来一首音乐。'or text=='来首音乐。' or text.find('来一首歌')!=-1 or text.find('放首歌')!=-1 or text.find('来首歌')!=-1 or text=='播放推荐音乐。' or text=='播放日推。'or text.find('继续播放')!=-1 :
play('Sound/musicprepare.wav')
music_en()
logger.info('up the music')
startagain=True
return True
elif text.find("播放音乐")==0 or text.find('搜索音乐')!=-1 or text.find('搜索歌曲')!=-1 or text.find('播放歌曲')!=-1:
try:
words=text[4:]
if words[-1]=='。':
words=words[:-1]
if words[0]==',':
words=words[1:]
if words.find('歌手')!=-1:
words = " ".join(words.split('的',1))
# if words.find('的')!=-1:
# words=" ".join(words.split('的'))
except:
logger.warning('words split wrong')
return True
logger.info(f'搜索:{words}')
interrupted_music=True
search=True
startagain=True
music_en()
return True
elif text=='下一首。' or text =='下一首音乐。'or ((text.find('播放')!=-1 or text.find('切换')!=-1) and (text.find('下首')!=-1 or text.find('下一首')!=-1)):
interrupted_music=True
logger.info('Next Music')
startagain=True
return True
elif (text.find('停止')!=-1 or text.find('暂停')!=-1 or text.find('关闭')!=-1) and (text.find('音乐')!=-1 or text.find('播放')!=-1 or text.find('歌曲')!=-1)or text=='静音。'or text.find('音乐关了')!=-1:
music_off()
logger.info('off the music')
return True
elif text.find('调整')!=-1 and (text.find('声音')!=-1 or text.find('音量')!=-1):
try:
if text[-1] == '。':
config.set(music_volume=float(text[-4:-2])/100)
logger.info(f'music_volume:{config.get("music_volume")}')
except:
logger.warning('words split wrong in set volume')
return True
return True
elif text.find('声音')!=-1 or text.find('音量')!=-1 :
if text.find('大一点')!=-1 or text.find('调大')!=-1 or text.find('增加')!=-1 or text.find('提高')!=-1 :
config.set(music_volume=config.get("music_volume")+0.1)
logger.info(f'Volume:{config.get("music_volume")}')
return True
elif text.find('小一点')!=-1 or text.find('调小')!=-1 or text.find('减小')!=-1 or text.find('降低')!=-1 :
config.set(music_volume=config.get("music_volume")-0.1)
logger.info(f'Volume:{config.get("music_volume")}')
return True
return False
def music_get(url, headers):
for _ in range(2):
try:
r = music.get(url, headers=headers, timeout=10)
return r
except Exception as e:
logger.warning(e)
time.sleep(5)
play('Sound/urlwrong.wav')
return False
# def get_source_cookie():
# # print('stop in get source cookie')
# # return
# print('start get source cookie')
# dealCookie.get_cookie()
# print('complete')
# def set_cookie():
# get_source_cookie()
# with open('cookie.txt', 'r') as f:
# cookie = f.read()
# f.close()
# cookiejson = {'data': cookie}
# #print('cookie is', cookie)
# print('start set new cookie')
# headers = {"content-type": 'application/json', 'Connection': 'close'}
# r = requests.post(url='http://127.0.0.1:3300/user/setCookie', json=cookiejson, headers=headers)
# r.close()
def get_cookie():
global cookiewrong,startagain
with open('cookie.txt', 'r') as f:
cookie = f.read()
f.close()
cookiejson = {'data': cookie}
logger.info('start set cookie')
headers = {"content-type": 'application/json', 'Connection': 'close'}
r = requests.post(url='http://127.0.0.1:3300/user/setCookie', json=cookiejson, headers=headers)
r.close()
if cookie_check():
cookiewrong = False
return True
else:
# play('Sound/dealcookie.wav')
# set_cookie()
# play('Sound/dealcookieok.wav')
# if not cookie_check():
# print('source cookie error')# 获取资源出现错误,请检查cookie
# if cookiewrong is False:
# cookiewrong=True
# startagain=False
# return False
# else:
# return True
return False
def cookie_check():
global cookiewrong,startagain
r=music_get(f'http://127.0.0.1:3300/user/getCookie?id={qqid}',headers=header)
if r is False:
logger.error('cookie_get wrong ,check if service on ,return')
cookiewrong=True
startagain=False
return False
for cookie in r.cookies:
if cookie.name == 'qm_keyst':
if cookie.expires < int(time.time()):
logger.info('need update cookie')
return False
else:
logger.info('cookie is normal')
return True
return False
def get_advice_list():
if not cookie_check():
if not get_cookie():
logger.error('cookie wrong , stop from get_adv_list')
return
r=music_get('http://127.0.0.1:3300/recommend/daily',headers=header)
if r is False:
logger.error('cannot get (request) ,return')
return
data=json.loads(r.text)
for i in range(len(data['data']['songlist'])):
if data['data']['songlist'][i]['pay']['payplay']==0:
advice.append({'songname':data['data']['songlist'][i]['songname'],'singer':data['data']['songlist'][i]['singer'][0]['name'],'songmid':data['data']['songlist'][i]['songmid']})
r.close()
logger.info(f'advice_list: {advice}')
def get_radio_list():
if not cookie_check():
if not get_cookie():
logger.error('cookie wrong , stop from get_adv_list')
return
r=music_get('http://127.0.0.1:3300/radio?id=101',headers=header)
if r is False:
logger.error('cannot get (request) ,return')
return
data=json.loads(r.text)
try:
for i in range(len(data['data']['tracks'])):
if data['data']['tracks'][i]['pay']['pay_play']==0:
item={'songname':data['data']['tracks'][i]['name'],'singer':data['data']['tracks'][i]['singer'][0]['name'],'songmid':data['data']['tracks'][i]['mid']}
advice.append(item)
logger.info(item)
except Exception as e:
logger.warning(e)
return
#print('advice_list:',advice)
# r.close()
# music.close()
def get_search_song(words):
global interrupted_music, search
interrupted_music = True
search = True
r = music_get(f'http://127.0.0.1:3300/search/quick?key={words}',headers=header)
if r is False:
logger.error('cannot get (request) ,return')
return
data = json.loads(r.text)
for i in range(len(data['data']['song']['itemlist'])):
logger.info(i)
r=music_get(f"http://127.0.0.1:3300/song/urls?id={data['data']['song']['itemlist'][i]['mid']}",headers=header)
r.close()
if r is False:
logger.error('cannot get (request) ,return')
return
if json.loads(r.text)['data']!={}:
return [data['data']['song']['itemlist'][i]['mid'],data['data']['song']['itemlist'][i]['name'],data['data']['song']['itemlist'][i]['singer']]
else:
logger.info('find next')
#interrupted_music=False
#search=False
#play(Musicnotfound.wav')
#return False
music.close()
return get_search_song_deep(words)
def get_search_song_deep(words):
global interrupted_music,search
logger.info('start deep search')
r = music_get(f'http://127.0.0.1:3300/search?key={words}',headers=header)
r.close()
if r is False:
logger.error('cannot get (request) ,return')
return
data = json.loads(r.text)
logger.info(data)
for i in range(len(data['data']['list'])):
if data['data']['list'][i]['pay']['pay_play']==0:
return [data['data']['list'][i]['songmid'],data['data']['list'][i]['songname'],data['data']['list'][i]['singer'][0]['name']]
interrupted_music=False
search=False
play('Sound/Musicnotfound.wav')
# music.close()
return False
def converter(a,b):
global order
# convert wav to mp3
try:
audSeg = AudioSegment.from_file(a)
audSeg.export(b, format="wav")
return True
except Exception as e:
logger.warning(e)
play('Sound/convertwrong.wav')
order=order+1
return False
def play_search_song(words):
global musicsound,musicplayer,interrupted_music,search
back=get_search_song(words)
if back==False:
logger.error('wrong in play_search_song ,return ')
search=False
return
r=music_get(f'http://127.0.0.1:3300/song/url?id={back[0]}',headers=header)
if r is False :
logger.error('cannot get (request) ,return')
search=False
return
# r.close()
r=json.loads(r.text)
if r['result']==100:
#for i in r['data']:
#r=requests.get(url=r['data'][i])
r=music_get(url=r['data'],headers=header)
if r is False:
logger.error('cannot get (request) ,return')
search=False
return
with open('Sound/music_search.mp3','wb') as file:
file.write(r.content)
file.close()
r.close()
else:
logger.error('request not 100,return from play_search_song')
search=False
return
converter('Sound/music_search.mp3','Sound/music_search.wav')
logger.info(f'来自{back[2]}的{back[1]}')
tts.ssml_save(f'来自{back[2]}的,{back[1]}','Sound/musicnotify.raw')
play('Sound/ding.wav')
play('Sound/musicnotify.raw')
time.sleep(2.5)
musicsound=arcade.Sound('Sound/music_search.wav',streaming=True)
musicplayer=musicsound.play(volume=config.get("music_volume"))
interrupted_music=False
search=False
logger.info('start play search song')
music.close()
def play_advice_music(order):
global musicsound,musicplayer,interrupted_music
if len(advice)==0:
logger.error('advice is empty , return from play_adv_music')
return
r=music_get(f'http://127.0.0.1:3300/song/urls?id={advice[order]["songmid"]}',headers=header)
if r is False :
logger.error('cannot get (request in play_adv_music) ,return')
return
# r.close()
r=json.loads(r.text)
if r['result']==100:
try:
for i in r['data']:
r=music_get(url=r['data'][i],headers=header)
#r=music_get(url=r['data'],headers=header)
if r is False or r.status_code>=400:
interrupted_music = True
logger.error('cannot get (request in play_adv_music_url) ,return')
return
except Exception as e:
logger.error(f'{e},exit')
return
with open('Sound/music_adv.m4a','wb') as file:
file.write(r.content)
file.close()
r.close()
else:
logger.error('respones is not 100 , return from play_adv_music')
return
if not converter('Sound/music_adv.m4a','Sound/music_adv.wav'):
return
logger.info(f"来自{advice[order]['singer']}的{advice[order]['songname']}")
tts.ssml_save(f"来自{advice[order]['singer']}的,{advice[order]['songname']}",'Sound/musicnotify.raw')
play('Sound/ding.wav')
play('Sound/musicnotify.raw')
time.sleep(2.5)
musicsound=arcade.Sound('Sound/music_adv.wav',streaming=True)
musicplayer=musicsound.play(volume=config.get("music_volume"))
logger.info('start play advice song')
music.close()
def stop_music():
global musicsound,musicplayer
if musicsound and musicplayer and musicsound.is_playing(musicplayer):
try:
logger.info('music stopping')
musicsound.stop(musicplayer)
except:
logger.warning('stop sound wrong in if_musci stop func')
def admin_music():
#if len(advice)==0:
# print('advice is empty ,start get list')
# get_advice_list()
while order>=len(advice):
logger.info('adivce is equal to order ,start get list')
get_radio_list()
logger.info(order)
play_advice_music(order)
return None
def music_en():
config.set(MusicPlay=True)
def music_off():
config.set(MusicPlay=False)
def watch():
lastime = None
times=0
global musicsound,musicplayer,order,interrupted_music,advice,search,startagain,cookiewrong
while(1):
if cookiewrong :
if not startagain:
continue
if config.get("MusicPlay"):
if interrupted_music == True:
stop_music()
if search==True:
play_search_song(words)
times=0
else:
order = order + 1
interrupted_music=False
# play('Sound/ding.wav')
# play(preparefornextmusic.wav')
times=0
logger.info('Prepare for next music')
# 正在为您准备下一首音乐
admin_music()
elif musicsound and musicplayer :
if musicsound.is_complete(musicplayer):
stop_music()
order=order+1
times=0
logger.info('Music service : next music')
admin_music()
logger.info('music is playing')
elif musicsound.is_playing(musicplayer)==False:
musicplayer=musicsound.play(volume=config.get("music_volume"))
times=times+1
if times>800:
interrupted_music=True
logger.warning('music stop by time in if_musci')
times=0
elif musicplayer == None:
admin_music()
order=order+1
logger.info('Music service : start play')
else:
stop_music()
if musicsound and musicplayer and musicsound.is_playing(musicplayer):
if (config.get("chat_enable") or config.get("notify_enable") or config.get("rec_enable")):
if musicsound.get_volume(musicplayer)!=0.05:
musicsound.set_volume(0.05, musicplayer)
logger.info('Music service : turn down the volume')
elif musicsound.get_volume(musicplayer)!=config.get("music_volume"):
musicsound.set_volume(config.get("music_volume"), musicplayer)
logger.info('Music service : change the volume')
if time.localtime()[2]!=lastime:
lastime=time.localtime()[2]
advice=[]
order=0
time.sleep(0.5)
if __name__=="__main__":
order=0
admin_music()
time.sleep(200)