forked from Gunale0926/KO-ON-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
819 lines (768 loc) · 38.1 KB
/
bot.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
import json
from khl import Message, Bot
from khl.card import CardMessage, Card, Module
import subprocess
import requests
import psutil
import re
import time
with open("config.json", "r", encoding="utf-8") as f:
configstr = f.read().replace('\\', '!')
configtmp = json.loads(configstr)
config = {k: v.replace('!', '\\') for k, v in configtmp.items()}
firstloginlock=True
LOCK = False
playtime = 0
duration = 0
netease_phone = config["n_phone"]
netease_passwd = config["n_passwd"]
netease_cookie = config["n_cookie"]
qq_cookie = config["q_cookie"]
qq_id = config["q_id"]
qq_enable = config["qq_enable"]
bot = Bot(token=config["token"])
playlist = []
p=subprocess.Popen('ffmpeg')
deltatime=7
def kill():
global p
try:
process = psutil.Process(p.pid)
for proc in process.children(recursive=True):
proc.kill()
process.kill()
except Exception as e:
print(e)
helpcm=[
{
"type": "card",
"theme": "secondary",
"size": "lg",
"modules": [
{
"type": "header",
"text": {
"type": "plain-text",
"content": "点歌机操作指南"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "**1. 点歌 + (平台) + 歌名**"
}
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "功能: 将歌曲加到播放队列中\ntips:\n歌名中如果有英文引号等特殊字符,需要将歌名用英文引号括起来\n例如 **点歌 \"Rrhar'il\"**\n如果需要指定歌曲版本播放,可以在歌名后添加歌手\n例如 **点歌 勇敢勇敢-黄勇**\n现支持QQ音乐、网易云音乐、网易云音乐电台与B站,若不写平台则默认从网易云获取数据(QQ音乐需要单独安装api并在config.json中启用平台、网易云电台仅支持从节目id点播)\n例如 **点歌 qq heavensdoor**\n例如 **点歌 网易 勇敢勇敢-黄勇**\n例如 **点歌 b站 BV1qa411e7Fi**\n例如 **点歌 你看到的我**\n例如 **点歌 网易电台 2499131107**"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "**2. 下一首**"
}
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "功能: 跳过当前正播放的歌曲,仅限**有特定角色的用户**使用"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "**3. 歌单**"
}
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "功能: 展示播放队列内剩余的歌曲"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "**4. 导入歌单 + 网易云歌单id**"
}
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "功能: 将网易云歌单中的歌曲导入到播放队列\n例如 **导入歌单 977171340**"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "**5. 导入电台 + 网易云电台id**"
}
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "功能: 将网易电台中的歌曲导入到播放队列\n例如 **导入电台 972583481**"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "**6. 清空歌单**"
}
},
{
"type": "section",
"text": {
"type": "kmarkdown",
"content": "功能: 清空播放队列"
}
},
{
"type": "divider"
},
{
"type": "context",
"elements": [
{
"type": "plain-text",
"content": "如有其他问题、bug或反馈建议,请私信开发人员:\nnick-haoran#0722 Gunale#2333\n特别鸣谢: k1nbo#0001"
}
]
}
]
}
]
def getCidAndTitle(bvid,p=1):
global duration
global deltatime
url='https://api.bilibili.com/x/web-interface/view?bvid='+bvid
data=requests.get(url).json()['data']
title=data['title']
cid=data['pages'][p-1]['cid']
duration=data['pages'][p-1]['duration']+deltatime
mid=str(data['owner']['mid'])
name=data['owner']['name']
pic=data['pic']
print(cid,title,mid)
return str(cid),title,mid,name,pic
def getInformation(bvid):
bvid=bvid.replace("?p=", " ")
item=[]
if len(bvid) == 12:
cid,title,mid,name,pic=getCidAndTitle(bvid[:12])
item.append(bvid)
else:
cid,title,mid,name,pic=getCidAndTitle(bvid[:12],int(bvid[13:]))
item.append(bvid[:12])
item.append(cid)
item.append(title)
item.append(mid)
item.append(name)
item.append(pic)
return item
def getAudio(item):
baseUrl='http://api.bilibili.com/x/player/playurl?fnval=16&'
bvid,cid,title,mid,name,pic=item[0],item[1],item[2],item[3],item[4],item[5]
url=baseUrl+'bvid='+bvid+'&cid='+cid
audioUrl=requests.get(url).json()['data']['dash']['audio'][0]['baseUrl']
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0) Gecko/20100101 Firefox/56.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Range': 'bytes=0-',
'Referer': 'https://api.bilibili.com/x/web-interface/view?bvid='+bvid,
'Origin': 'https://www.bilibili.com',
'Connection': 'keep-alive'
}
response=requests.get(url=audioUrl, headers=headers)
open("tmp.mp3", "wb").write(response.content)
return bvid,cid,title,mid,name,pic
@bot.command(name='导入歌单')
async def listen(msg: Message, linkid : str):
global playlist
url = "http://127.0.0.1:3000/playlist/track/all?id="+linkid
songs = requests.get(url=url).json()["songs"]
for song in songs:
playlist.append({'name':song['name']+"-"+song['ar'][0]['name']+' '+str(song['id']),'userid':msg.author.id,'type':'网易','time':int(round(time.time() * 1000))+1000000000000})
await msg.ctx.channel.send("导入完成")
@bot.command(name='导入电台')
async def listen(msg: Message, linkid : str):
global playlist
url = "http://127.0.0.1:3000/dj/program?rid="+linkid+"&limit=10000"
programs = requests.get(url=url).json()["programs"]
for program in programs:
playlist.append({'name':program['mainSong']['name']+'-'+str(program['id']),'userid':msg.author.id,'type':'网易电台','time':int(round(time.time() * 1000))+1000000000000})
await msg.ctx.channel.send("导入完成")
@bot.command(name='清空歌单')
async def listen(msg: Message):
global playlist
if len(playlist)>0:
now=playlist[0]
playlist=[]
playlist.append(now)
await msg.ctx.channel.send("清空完成")
@bot.command(name="下一首")
async def nextmusic(msg: Message):
global playlist
global playtime
global LOCK
global duration
flag=True
for role in msg.author.roles:
print(role)
if str(role) == config["skipper"]:
flag=False
if config["skipper"]=='0':
flag=False
if flag:
await msg.ctx.channel.send("无权限")
return None
kill()
if len(playlist)==0:
await msg.ctx.channel.send("无下一首")
LOCK=False
return None
playlist.pop(0)
LOCK=False
playtime=0
duration=0
await msg.ctx.channel.send("切换成功")
@bot.command(name="点歌")
async def addmusic(msg: Message,*args):
global helpcm
global qq_enable
try:
args=list(args)
if msg.ctx.channel.id != config["channel"]:
await msg.ctx.channel.send('请在指定频道中点歌')
return
global playlist
typ='网易'
song_name=''
if args[0]=='qq' or args[0]=='网易' or args[0]=='b站'or args[0]=='网易电台':
typ=args[0]
args.pop(0)
if typ=='qq' and qq_enable == '0':
await msg.ctx.channel.send('未启用qq点歌')
return None
for st in args:
song_name = song_name + st + " "
playlist.append({'name':song_name,'userid':msg.author.id,'type':typ,'time':int(round(time.time() * 1000))})
await msg.ctx.channel.send("已添加")
except:
await msg.ctx.channel.send(helpcm)
@bot.command(name="歌单")
async def prtlist(msg: Message):
global playlist
cm = CardMessage()
c = Card()
c.append(Module.Header('正在播放:'))
if len(playlist) == 0:
c.append(Module.Section('无'))
i=0
for item in playlist:
if i==10:
break
c.append(Module.Section(item['name']))
i+=1
c.append(Module.Header('共有'+str(len(playlist))+'首歌'))
cm.append(c)
await msg.ctx.channel.send(cm)
@bot.command(name="帮助")
async def help(msg: Message):
global helpcm
await msg.ctx.channel.send(helpcm)
@bot.task.add_interval(seconds=deltatime)
async def update_played_time_and_change_music():
global deltatime
global playtime
global playlist
global LOCK
global duration
global p
global netease_phone
global netease_passwd
global firstloginlock
global neteasecookie
if firstloginlock==True:
print('login check')
url='http://127.0.0.1:3000/login/status?timestamp='
print(url)
response=requests.get(url=url).json()
print(response)
try:
response=response['data']['account']['status']
if response==-10:
url='http://127.0.0.1:3000/login/cellphone?phone='+netease_phone+'&password='+netease_passwd
print(requests.get(url=url).json())
print('网易云登陆成功')
except:
url='http://127.0.0.1:3000/login/cellphone?phone='+netease_phone+'&password='+netease_passwd
print(requests.get(url=url).json())
print('网易云登陆成功')
print('网易已登录')
url='http://127.0.0.1:3000/login/refresh'
requests.get(url=url)
print('刷新登录cookie')
if qq_enable=="1":
url='http://127.0.0.1:3300/user/setCookie'
data={
"data":qq_cookie
}
response=requests.post(url=url,json=data)
print(response.text)
url='http://127.0.0.1:3300/user/getCookie?id='+qq_id
response=requests.get(url=url)
print(response.text)
print('QQ已登录')
firstloginlock=False
if LOCK:
return
else:
LOCK = True
if len(playlist) == 0:
LOCK = False
return None
else:
if playtime == 0:
playlist.sort(key=lambda x: list(x.values())[3])
song_name = playlist[0]['name']
if song_name == "":
LOCK = False
return
if playlist[0]['type']=='网易':
try:
headers={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Cookie': netease_cookie,
'Host': '127.0.0.1:3000',
'Referer': 'https://music.163.com',
'If-None-Match': 'W/"722-3Oy0PoR7kMdKeuZyLO+S/tZ4B6I"',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Microsoft Edge";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': "Windows",
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33'
}
url="http://127.0.0.1:3000/cloudsearch?keywords="+song_name+"&limit=1"
response=requests.get(url=url,headers=headers).json()
musicid=str(response['result']['songs'][0]['id'])
url='http://127.0.0.1:3000/song/detail?ids='+musicid
response=requests.get(url=url,headers=headers).json()['songs'][0]
duration=int(response['dt']/1000)+5
song_name=response['name']
ban=re.compile('(惊雷)|(Lost Rivers)')
resu=ban.findall(song_name)
print(resu)
if len(resu)>0:
LOCK=False
playlist.pop(0)
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'吃了吗,没吃吃我一拳',
)
duration=0
playtime=0
return None
song_url='https://music.163.com/#/song?id='+str(response['id'])
album_name=response['al']['name']
if album_name=='':
album_name='无专辑'
album_url='https://music.163.com/#/album?id='+str(response['al']['id'])
singer_name=response['ar'][0]['name']
singer_url='https://music.163.com/#/artist?id='+str(response['ar'][0]['id'])
pic_url=response['al']['picUrl']
getfile_url='http://127.0.0.1:3000/song/url?id='+str(response['id'])+'×tamp='+str(int(round(time.time() * 1000)))
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data'][0]['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and (not urlresponse.endswith(".flac")):
getfile_url='http://127.0.0.1:3000/song/download/url?id='+str(response['id'])+'×tamp='+str(int(round(time.time() * 1000)))
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data']['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and (not urlresponse.endswith(".flac")):
getfile_url='http://127.0.0.1:3000/song/url?id='+str(response['id'])+'&br=320000×tamp='+str(int(round(time.time() * 1000)))
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data'][0]['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and (not urlresponse.endswith(".flac")):
getfile_url='http://127.0.0.1:3000/song/download/url?id='+str(response['id'])+'&br=320000×tamp='+str(int(round(time.time() * 1000)))
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data']['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and (not urlresponse.endswith(".flac")):
getfile_url='http://127.0.0.1:3000/song/url?id='+str(response['id'])
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data'][0]['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and (not urlresponse.endswith(".flac")):
getfile_url='http://127.0.0.1:3000/song/download/url?id='+str(response['id'])
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data']['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and (not urlresponse.endswith(".flac")):
getfile_url='http://127.0.0.1:3000/song/url?id='+str(response['id'])+'&br=320000'
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data'][0]['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and (not urlresponse.endswith(".flac")):
getfile_url='http://127.0.0.1:3000/song/download/url?id='+str(response['id'])+'&br=320000'
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data']['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
musicfile = requests.get(urlresponse)
if urlresponse.endswith("flac"):
open("tmp.flac", "wb").write(musicfile.content)
kill()
p = subprocess.Popen(
'ffmpeg -re -nostats -i "tmp.flac" -acodec libopus -ab 128k -f mpegts zmq:tcp://127.0.0.1:'+config["port"],
shell=True
)
else:
open("tmp.mp3", "wb").write(musicfile.content)
kill()
p = subprocess.Popen(
'ffmpeg -re -nostats -i "tmp.mp3" -acodec libopus -ab 128k -f mpegts zmq:tcp://127.0.0.1:'+config["port"],
shell=True
)
starttime=int(round(time.time() * 1000))
endtime=starttime+int(duration*1000)
playtime = 0
cm = [ { "type": "card", "theme": "secondary", "color": "#DD001B", "size": "lg", "modules": [ { "type": "header", "text": { "type": "plain-text", "content": "正在播放:" + song_name, }, }, { "type": "section", "text": { "type": "kmarkdown", "content":"(met)" +playlist[0]['userid']+"(met)", }, }, { "type": "context", "elements": [ { "type": "kmarkdown", "content": "歌手: [" + singer_name + "](" + singer_url + ") —出自专辑 [" + album_name + "](" + album_url + ")", } ], }, { "type": "audio", "title": song_name, "src": urlresponse, "cover": pic_url, }, {"type": "countdown","mode": "second","startTime": starttime,"endTime": endtime},{"type": "divider"}, { "type": "context", "elements": [ { "type": "image", "src": "https://img.kaiheila.cn/assets/2022-05/UmCnhm4mlt016016.png", }, { "type": "kmarkdown", "content": "网易云音乐 [在网页查看](" + song_url + ")", }, ], }, ], } ]
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
cm,
)
except Exception as e:
print(str(e))
if str(e)=="'songs'":
playlist.pop(0)
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'未检索到此歌曲',
)
else:
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'发生错误,正在重试',
)
LOCK=False
duration=0
playtime=0
return None
elif playlist[0]['type']=='b站':
try:
song_name=song_name.replace(" ", "")
bvid,cid,title,mid,name,pic=getAudio(getInformation(song_name))
starttime=int(round(time.time() * 1000))
endtime=starttime+int(duration*1000)
cm=[{"type": "card","theme": "secondary", "color": "#DD001B", "size": "lg","modules": [{"type": "section","text": {"type": "kmarkdown","content": "**标题: ["+title+"](https://www.bilibili.com/video/"+song_name+"/)**"}},{"type": "section","text": {"type": "kmarkdown","content": "UP: ["+name+"](https://space.bilibili.com/"+mid+"/)"}},{"type": "container","elements": [{"type": "image","src": pic}]},{"type": "countdown","mode": "second","startTime": starttime,"endTime": endtime}]}]
print(duration)
ban=re.compile('(惊雷)|(Lost Rivers)')
resu=ban.findall(title)
if len(resu)>0:
LOCK=False
playlist.pop(0)
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'吃了吗,没吃吃我一拳',
)
duration=0
playtime=0
return None
playtime = 0
kill()
p = subprocess.Popen(
'ffmpeg -re -nostats -i "tmp.mp3" -acodec libopus -ab 128k -f mpegts zmq:tcp://127.0.0.1:'+config["port"],
shell=True
)
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
cm,
)
except:
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'发生错误,正在重试',
)
LOCK=False
duration=0
playtime=0
return None
elif playlist[0]['type']=='网易电台':
try:
headers={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Cookie': netease_cookie,
'Host': '127.0.0.1:3000',
'Referer': 'https://music.163.com',
'If-None-Match': 'W/"722-3Oy0PoR7kMdKeuZyLO+S/tZ4B6I"',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Microsoft Edge";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': "Windows",
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33'
}
song_name=song_name.replace(" ", "")
song_name=song_name.split('-')[-1]
print(song_name)
url='http://127.0.0.1:3000/dj/program/detail?id='+song_name
response=requests.get(url=url,headers=headers).json()['program']
duration=int(response['duration']/1000)+5
song_url='https://music.163.com/#/program?id='+song_name
song_name=response['mainSong']['name']
ban=re.compile('(惊雷)|(Lost Rivers)')
resu=ban.findall(song_name)
print(resu)
if len(resu)>0:
LOCK=False
playlist.pop(0)
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'吃了吗,没吃吃我一拳',
)
duration=0
playtime=0
return None
album_name=response['radio']['name']
if album_name=='':
album_name='无专辑'
album_url='https://music.163.com/#/djradio?id='+str(response['radio']['id'])
singer_name=response['dj']['nickname']
singer_url='https://music.163.com/#/user/home?id='+str(response['dj']['userId'])
pic_url=response['radio']['picUrl']
getfile_url='http://127.0.0.1:3000/song/url?id='+str(response['mainSong']['id'])
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data'][0]['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and not urlresponse.endswith(".flac"):
getfile_url='http://127.0.0.1:3000/song/download/url?id='+str(response['mainSong']['id'])
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data']['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and not urlresponse.endswith(".flac"):
getfile_url='http://127.0.0.1:3000/song/url?id='+str(response['mainSong']['id'])+'&br=2000000'
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data'][0]['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and not urlresponse.endswith(".flac"):
getfile_url='http://127.0.0.1:3000/song/download/url?id='+str(response['mainSong']['id'])+'&br=2000000'
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data']['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
if (urlresponse.startswith("http://m702") or urlresponse.startswith("http://m802") or len(urlresponse)==0)and not urlresponse.endswith(".flac"):
getfile_url='http://127.0.0.1:3000/song/url?id='+str(response['mainSong']['id'])+'?timestamp='
urlresponse=requests.get(url=getfile_url,headers=headers).json()['data'][0]['url']
print(urlresponse)
if urlresponse==None:
urlresponse=''
musicfile = requests.get(urlresponse)
if urlresponse.endswith("flac"):
open("tmp.flac", "wb").write(musicfile.content)
kill()
p = subprocess.Popen(
'ffmpeg -re -nostats -i "tmp.flac" -acodec libopus -ab 128k -f mpegts zmq:tcp://127.0.0.1:'+config["port"],
shell=True
)
else:
open("tmp.mp3", "wb").write(musicfile.content)
kill()
p = subprocess.Popen(
'ffmpeg -re -nostats -i "tmp.mp3" -acodec libopus -ab 128k -f mpegts zmq:tcp://127.0.0.1:'+config["port"],
shell=True
)
playtime = 0
starttime=int(round(time.time() * 1000))
endtime=starttime+int(duration*1000)
cm = [ { "type": "card", "theme": "secondary", "color": "#DD001B", "size": "lg", "modules": [ { "type": "header", "text": { "type": "plain-text", "content": "正在播放:" + song_name, }, }, { "type": "section", "text": { "type": "kmarkdown", "content":"(met)" +playlist[0]['userid']+"(met)", }, }, { "type": "context", "elements": [ { "type": "kmarkdown", "content": "歌手: [" + singer_name + "](" + singer_url + ") —出自专辑 [" + album_name + "](" + album_url + ")", } ], }, { "type": "audio", "title": song_name, "src": urlresponse, "cover": pic_url, }, {"type": "countdown","mode": "second","startTime": starttime,"endTime": endtime},{"type": "divider"}, { "type": "context", "elements": [ { "type": "image", "src": "https://img.kaiheila.cn/assets/2022-05/UmCnhm4mlt016016.png", }, { "type": "kmarkdown", "content": "网易云音乐 [在网页查看](" + song_url + ")", }, ], }, ], } ]
print(duration)
kill()
p = subprocess.Popen(
'ffmpeg -re -nostats -i "tmp.mp3" -acodec libopus -ab 128k -f mpegts zmq:tcp://127.0.0.1:'+config["port"],
shell=True
)
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
cm,
)
except:
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'发生错误,正在重试',
)
LOCK=False
duration=0
playtime=0
return None
else:
try:
url="http://127.0.0.1:3300/search?key="+song_name+"&pageSize=1"
response=requests.get(url=url).json()['data']['list'][0]
song_name=response['songname']
ban=re.compile('(惊雷)|(Lost Rivers)')
resu=ban.findall(song_name)
if len(resu)>0:
LOCK=False
playlist.pop(0)
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'吃了吗,没吃吃我一拳',
)
duration=0
playtime=0
return None
duration=response['interval']+5
song_url='https://y.qq.com/n/ryqq/songDetail/'+response['songmid']
album_name=response['albumname']
if album_name=='':
album_name='无专辑'
album_url='https://y.qq.com/n/ryqq/albumDetail/'+response['albummid']
singer_name=response['singer'][0]['name']
singer_url='https://y.qq.com/n/ryqq/singer/'+response['singer'][0]['mid']
pic_url='https://y.gtimg.cn/music/photo_new/T002R300x300M000'+response['albummid']+'.jpg'
getfile_url='http://127.0.0.1:3300/song/url?id='+response['songmid']+'&mediaId='+response['strMediaMid']+'&ownCookie=1'
try:
response=requests.get(url=getfile_url).json()['data']
except:
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'api cookie失效',
)
playlist.pop(0)
LOCK=False
duration=0
playtime=0
return None
musicfile = requests.get(response)
open("tmp.mp3", "wb").write(musicfile.content)
starttime=int(round(time.time() * 1000))
endtime=starttime+int(duration*1000)
playtime = 0
kill()
p = subprocess.Popen(
'ffmpeg -re -nostats -i "tmp.mp3" -acodec libopus -ab 128k -f mpegts zmq:tcp://127.0.0.1:'+config["port"],
shell=True
)
cm = [ { "type": "card", "theme": "secondary", "color": "#DD001B", "size": "lg", "modules": [ { "type": "header", "text": { "type": "plain-text", "content": "正在播放:" + song_name }, }, { "type": "section", "text": { "type": "kmarkdown", "content":"(met)" +playlist[0]['userid']+"(met)", }, }, { "type": "context", "elements": [ { "type": "kmarkdown", "content": "歌手: [" + singer_name + "](" + singer_url + ") —出自专辑 [" + album_name + "](" + album_url + ")", } ], }, { "type": "audio", "title": song_name, "src": response, "cover": pic_url, }, {"type": "countdown","mode": "second","startTime": starttime,"endTime": endtime},{"type": "divider"}, { "type": "context", "elements": [ { "type": "image", "src": "https://img.kaiheila.cn/assets/2022-06/cqzmClO3Sq07s07x.png/ld", }, { "type": "kmarkdown", "content": "QQ音乐 [在网页查看](" + song_url + ")", }, ], }, ], } ]
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
cm,
)
except:
await bot.send(
await bot.fetch_public_channel(
config["channel"]
),
'发生错误,正在重试',
)
LOCK=False
duration=0
playtime=0
return None
playtime += deltatime
LOCK = False
return None
else:
if playtime + deltatime < duration:
playtime += deltatime
LOCK = False
return None
else:
playtime = 0
playlist.pop(0)
LOCK = False
return None
@bot.task.add_interval(minutes=30)
async def keep_login():
url='http://127.0.0.1:3000/login/refresh'
requests.get(url=url)
print('刷新登录')
bot.command.update_prefixes("")
bot.run()