-
Notifications
You must be signed in to change notification settings - Fork 3
/
music.py
49 lines (43 loc) · 1.63 KB
/
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
"""
网易云音乐
"""
import requests
import json
import data.log
import api.gocqhttp
from handlers.message import Message
# HOST = 'inuyasha.love'
# PORT = 8001
URL = 'https://netease.inuyasha.love/search'
myLogger = data.log.get_logger()
def run(message: Message) -> str:
help_msg = '小白会试着从网易云搜索~'
req = list(message.raw_message.split(' ', 1))
ans = ''
if len(req) > 1:
req[1] = req[1].strip()
if len(req) > 1 and len(req[1]) > 0:
try:
resp = requests.get(url=URL, params={'keywords': req[1]}, timeout=30)
except Exception as e:
myLogger.exception(f'RuntimeError {e}')
return '啊嘞嘞好像出错了'
else:
if resp.status_code == 200:
rejson = json.loads(resp.text)
# print(resp.text)
if rejson['result'].get('songs'):
mscid = rejson['result']['songs'][0]['id']
# mscname = rejson['result']['songs'][0]['name']
# ans = '[CQ:share,url=https://music.163.com/song/' + str(mscid) + '/,title=' + str(mscname) + ']'
if message.message_type == 'group':
api.gocqhttp.send_group_share_music(message.group_id, '163', mscid)
elif message.message_type == 'private':
api.gocqhttp.send_private_share_music(message.user_id, '163', mscid)
else:
ans = '网易云里没有诶~'
else:
ans = '好像返回了奇怪的东西: ' + str(resp.status_code)
else:
ans = help_msg
return ans