""" @Time : 2022/7/11 01:42 @Author : superhero @Email : 838210720@qq.com @File : wyy.py @IDE: PyCharm """ from nonebot import on_command import nonebot import execjs import base64 import requests import codecs from Crypto.Cipher import AES from Crypto.Util.Padding import pad from nonebot.adapters.onebot.v11 import Message from nonebot.params import Arg, CommandArg, ArgPlainText from lxml import etree from nonebot.matcher import Matcher import random
__plugin_meta__ = nonebot.plugin.PluginMetadata( name='网易云音乐', description='网易云音乐搜索下载', usage=''' 命令1/wyy/网易云音乐/点歌<参数:歌曲名,序号>不含下载 命令2/wyys/网易云音乐s/下载<参数:歌曲名,序号>含下载 命令3/pushy/歌曲推荐/推荐<参数:无>随机推荐 命令4/pushys/歌曲推荐s/推荐s<参数:无>根据作者的歌单推荐 ''', extra={'version': '1.3'} ) __help_version__ = '1.3'
wyys = on_command("wyys", aliases={"网易云音乐s", "下载"}, priority=5)
@wyys.handle() async def handle_first_receive(matcher: Matcher, args: nonebot.adapters.Message = CommandArg()): list_name = args.extract_plain_text().strip() if list_name: matcher.set_arg("music_name", args)
@wyys.got("music_name", prompt="你想下载的歌曲是?") async def _(matcher: Matcher, music_name: nonebot.adapters.Message = Arg(), text: str = ArgPlainText("music_name")): if not text: await wyys.reject(music_name.template("待下载的歌曲名不能为空")) if "," in text: stt = text.split(",") name = stt[0] ids = stt[1] matcher.set_arg("id", Message(ids)) matcher.set_arg("music_name", Message(name)) elif "," in text: stt = text.split(",") name = stt[0] ids = stt[1] matcher.set_arg("id", Message(ids)) matcher.set_arg("music_name", Message(name)) else: ids = '0' name = text
wyy2 = WangYiYun() ret = wyy2.get_wyy_kwd(name) n = 0 music_names = '' if ids != '0': music_id = ret['result']['songs'][(int(ids) - 1)]['id'] if music_id: url = wyy2.get_wyy_playurl(music_id) await wyys.finish("[{0}]下载地址:".format(ret['result']['songs'][(int(ids) - 1)]['name']) + url) else: await wyys.finish("音乐ID获取失败") else: for i in ret['result']['songs']: n += 1 music_names += '[{0}]'.format(n) + i['name'] + ' - ' + i['ar'][(int(ids) - 1)]['name'] + '\n' if n >= 10: break await wyys.send(music_names)
@wyys.got("id", prompt="请选择要下载第几个?") async def _(music_name: nonebot.adapters.Message = Arg(), text: str = ArgPlainText("music_name"), ids: str = ArgPlainText("id")): if not ids: await wyys.reject(music_name.template("你还没有选择第几个哦")) wyy2 = WangYiYun() ret = wyy2.get_wyy_kwd(text) music_id = ret['result']['songs'][(int(ids) - 1)]['id'] if music_id: url = wyy2.get_wyy_playurl(music_id) await wyys.finish("[{0}]下载地址:".format(ret['result']['songs'][(int(ids) - 1)]['name']) + url) else: await wyys.finish("音乐ID获取失败")
wyy = on_command("wyy", aliases={"网易云音乐", "点歌"}, priority=5)
@wyy.handle() async def handle_first_receive(matcher: Matcher, args: nonebot.adapters.Message = CommandArg()): list_name = args.extract_plain_text().strip() if list_name: matcher.set_arg("music_name", args)
@wyy.got("music_name", prompt="你想搜索的歌曲是?") async def _(matcher: Matcher, music_name: nonebot.adapters.Message = Arg(), text: str = ArgPlainText("music_name")): if not text: await wyy.reject(music_name.template("待搜索的歌曲名不能为空")) if "," in text: stt = text.split(",") name = stt[0] ids = stt[1] matcher.set_arg("id", Message(ids)) matcher.set_arg("music_name", Message(name)) elif "," in text: stt = text.split(",") name = stt[0] ids = stt[1] matcher.set_arg("id", Message(ids)) matcher.set_arg("music_name", Message(name)) else: ids = '0' name = text
wyy2 = WangYiYun() ret = wyy2.get_wyy_kwd(name) n = 0 music_names = '' if ids != '0': music_id = ret['result']['songs'][(int(ids) - 1)]['id'] msg = "[CQ:music,type=163,id={0}]".format(str(music_id)) await wyy.finish(Message(msg)) else: for i in ret['result']['songs']: n += 1 music_names += '[{0}]'.format(n) + i['name'] + ' - ' + i['ar'][(int(ids) - 1)]['name'] + '\n' if n >= 10: break await wyy.send(music_names)
@wyy.got("id", prompt="请选择要播放第几个?") async def _(music_name: nonebot.adapters.Message = Arg(), text: str = ArgPlainText("music_name"), ids: str = ArgPlainText("id")): if not ids: await wyy.reject(music_name.template("你还没有选择第几个哦")) wyy2 = WangYiYun() ret = wyy2.get_wyy_kwd(text) music_id = ret['result']['songs'][(int(ids) - 1)]['id'] msg = "[CQ:music,type=163,id={0}]".format(str(music_id)) await wyy.finish(Message(msg))
song = on_command("pushy", aliases={"推荐", "歌曲推荐"}, priority=5) @song.handle()
async def _(): wyys = WangYiYun() listplay_id = wyys.get_wyy_pushy() msg = "[CQ:music,type=163,id={0}]".format(wyys.get_wyy_discover(listplay_id)) await song.send(Message(msg))
songs = on_command("pushys", aliases={"推荐s", "歌曲推荐s"}, priority=5) @songs.handle()
async def _(): wyys = WangYiYun() listplay_id = ['/playlist?id=7480897649'] msg = "[CQ:music,type=163,id={0}]".format(wyys.get_wyy_discover(listplay_id)) await songs.send(Message(msg))
def to_16(key): while len(key) % 16 != 0: key += '\0' return str.encode(key)
def AES_encrypt(text, key, iv): bs = AES.block_size pad2 = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs) encryptor = AES.new(to_16(key), AES.MODE_CBC, to_16(iv))
pd2 = pad(str.encode(pad2(text)), 16)
encrypt_aes = encryptor.encrypt(pd2) encrypt_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8') return encrypt_text
def RSA_encrypt(text, pubKey, modulus): text = text[::-1] rs = int(codecs.encode(text.encode('utf-8'), 'hex_codec'), 16) ** int(pubKey, 16) % int(modulus, 16) return format(rs, 'x').zfill(256)
get_i = execjs.compile(r""" function a(a) { var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = ""; for (d = 0; a > d; d += 1) e = Math.random() * b.length, e = Math.floor(e), c += b.charAt(e); return c } """)
class WangYiYun(): def __init__(self): self.csrf_token = '' self.MUSIC_U = '' self.g = '0CoJUm6Qyw8W8jud' self.b = "010001" self.c = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7' self.i = get_i.call('a', 16) self.iv = "0102030405060708" self.ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36'
def get_encSecKey(self): return RSA_encrypt(self.i, self.b, self.c)
def get_wyy_pushy(self): url = 'https://music.163.com/discover' ret = requests.get(url, headers={"User-Agent": self.ua}).text soup = etree.HTML(ret) hot_id = soup.xpath('//ul[@class="m-cvrlst f-cb"]//li/div[@class="u-cover u-cover-1"]/a/@href') n = len(hot_id) bek = [] for i in range(n): if "/dj" in hot_id[i]: bek.append(i) hot_id = [hot_id[i] for i in range(n) if (i not in bek)] return hot_id
def get_wyy_discover(self, list_id): """ 从列表中随机取一首 :param list_id: 列表id :return: """ playlist_id = list_id[random.randint(0, len(list_id) - 1)] cookie = '' ret = requests.get("https://music.163.com" + playlist_id, headers={"User-Agent": self.ua, "Cookie": cookie}).text soup = etree.HTML(ret) m_id = soup.xpath('//ul[@class="f-hide"]/li/a/@href') song_id = m_id[random.randint(0, len(m_id) - 1)][9:] return song_id
def get_wyy_kwd(self, keyword):
url = 'https://music.163.com/weapi/cloudsearch/get/web?csrf_token=' + self.csrf_token encText = str( {'hlpretag': '<span class=\"s-fc7\">', 'hlposttag': '</span>', '#/discover': '', 's': keyword, 'type': '1', 'offset': "0", 'total': 'true', 'limit': '30', 'csrf_token': self.csrf_token}) params = AES_encrypt(AES_encrypt(encText, self.g, self.iv), self.i, self.iv) data = { 'params': params, 'encSecKey': self.get_encSecKey() }
header = { 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'music.163.com', 'Referer': 'https://music.163.com/search/', 'User-Agent': self.ua } ret = requests.post(url, headers=header, data=data) return ret.json()
def get_wyy_playurl(self, music_id):
url = 'https://music.163.com/weapi/song/enhance/player/url?csrf_token=' + self.csrf_token encText = str({'ids': "[" + str(music_id) + "]", 'br': 128000, 'csrf_token': self.csrf_token, 'MUSIC_U': self.MUSIC_U}) params = AES_encrypt(AES_encrypt(encText, self.g, self.iv), self.i, self.iv) data = { 'params': params, 'encSecKey': self.get_encSecKey() } headeer = { 'User-Agent': self.ua, 'Referer': 'https://music.163.com/', 'Content-Type': 'application/x-www-form-urlencoded' }
ret = requests.post(url, headers=headeer, data=data).json() download_url = ret['data'][0]['url'] if download_url: try: msg = download_url except: msg = '出错了~' else: msg = '获取失败' return msg
|