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
| import base64 import binascii import json import random import string from urllib import parse
import requests import webbrowser from Crypto.Cipher import AES
import sys import os from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import *
class Music(): def get_random(self): random_str = ''.join(random.sample(string.ascii_letters + string.digits, 16)) return random_str
def len_change(self,text): pad = 16 - len(text) % 16 text = text + pad * chr(pad) text = text.encode("utf-8") return text
def aes(self,text, key): iv = b'0102030405060708' text = self.len_change(text) cipher = AES.new(key.encode(), AES.MODE_CBC, iv) encrypted = cipher.encrypt(text) encrypt = base64.b64encode(encrypted).decode() return encrypt
def b(self,text, str): first_data = self.aes(text, '0CoJUm6Qyw8W8jud') second_data = self.aes(first_data, str) return second_data
def c(self,text): e = '010001' f = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7' text = text[::-1] result = pow(int(binascii.hexlify(text.encode()), 16), int(e, 16), int(f, 16)) return format(result, 'x').zfill(131)
def get_final_param(self,text, str): params = self.b(text, str) encSecKey = self.c(str) return {'params': params, 'encSecKey': encSecKey}
def get_music_list(self,params, encSecKey): url = "https://music.163.com/weapi/cloudsearch/get/web?csrf_token="
payload = 'params=' + parse.quote(params) + '&encSecKey=' + parse.quote(encSecKey) headers = { 'authority': 'music.163.com', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36', 'content-type': 'application/x-www-form-urlencoded', 'accept': '*/*', 'origin': 'https://music.163.com', 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'empty', 'referer': 'https://music.163.com/search/', 'accept-language': 'zh-CN,zh;q=0.9', } response = requests.request("POST", url, headers=headers, data=payload) return response.text
def get_reply(self,params, encSecKey): url = "https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=" payload = 'params=' + parse.quote(params) + '&encSecKey=' + parse.quote(encSecKey) headers = { 'authority': 'music.163.com', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36', 'content-type': 'application/x-www-form-urlencoded', 'accept': '*/*', 'origin': 'https://music.163.com', 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'empty', 'referer': 'https://music.163.com/', 'accept-language': 'zh-CN,zh;q=0.9' } response = requests.request("POST", url, headers=headers, data=payload) return response.text
def start(self,song_search): song_url_list = [] song_name_list = [] song_num = 0 d = {"hlpretag": "<span class=\"s-fc7\">", "hlposttag": "</span>", "s": song_search, "type": "1", "offset": "0", "total": "true", "limit": "30", "csrf_token": ""} d = json.dumps(d) random_param = self.get_random() param = self.get_final_param(d, random_param) song_list = self.get_music_list(param['params'], param['encSecKey']) print('搜索结果如下:') if len(song_list) > 0: song_list = json.loads(song_list)['result']['songs'] for i, item in enumerate(song_list): item = json.dumps(item) song_name = json.loads(str(item))['name'] print(str(i) + ":" + song_name) d = {"ids": "[" + str(json.loads(str(item))['id']) + "]", "level": "standard", "encodeType": "", "csrf_token": ""} d = json.dumps(d) param = self.get_final_param(d, random_param) song_info = self.get_reply(param['params'], param['encSecKey']) if len(song_info) > 0: song_info = json.loads(song_info) song_url = json.dumps(song_info['data'][0]['url'], ensure_ascii=False) song_name_list.append(song_name) song_url_list.append(song_url) else: print("该首歌曲解析失败,可能是因为歌曲格式问题") song_num = i+1 print("一共搜索到{}个目标".format(song_num)) return song_name_list,song_url_list else: return None
class MainWin(QtWidgets.QWidget): def __init__(self,parent=None): super(MainWin, self).__init__(parent) self.song_result = "" self.song_num = 0 self.layout = QGridLayout() self.setWindowFlags(Qt.SubWindow) self.setupUI()
def searchMusic(self): print('* searchMusic ') self.song_name_list = [] self.song_url_list = [] self.song_num = 0 self.song_result = "" self.song_search = self.searchBox.text() if len(self.song_search) == 0: self.resultText.setText("请先输入歌曲名称") return mus = Music() self.song_name_list,self.song_url_list=mus.start(self.song_search) for song_name in self.song_name_list: self.song_num += 1 self.song_result+=(str(self.song_num)+"."+str(song_name)+"\n") self.song_result=self.song_result[:-1] self.resultText.setText(self.song_result)
def choiceMusic(self): print('* choiceMusic ') self.song_choice = self.choiceBox.text() if len(self.song_choice) == 0: self.resultText.setText("请先输入编号") return self.song_choice = eval(self.song_choice) if len(self.song_result) == 0: self.resultText.setText("请先搜索歌曲") return if self.song_choice > self.song_num: self.resultText.setText("输入的编号有误") return self.resultText.setText("开始播放:{}".format(self.song_name_list[self.song_choice-1])) wb = webbrowser.get('windows-default') wb.open(self.song_url_list[self.song_choice-1])
def quitWindows(self): print('* quitWindows ') self.close()
def setupUI(self): self.resize(550,450) self.groupBox = QtWidgets.QGroupBox(self) self.groupBox.setGeometry(QtCore.QRect(10,10,530,430)) self.groupBox.setObjectName("groupBox") self.groupBox.setStyleSheet("color:white") self.searchBox = QtWidgets.QLineEdit(self.groupBox) self.searchBox.setGeometry(QtCore.QRect(105,25,160,20)) self.searchBox.setObjectName("searchBox") self.searchBox.setStyleSheet("color:black") self.choiceBox = QtWidgets.QLineEdit(self.groupBox) self.choiceBox.setGeometry(QtCore.QRect(105,315,160,20)) self.choiceBox.setObjectName("choiceBox") self.choiceBox.setStyleSheet("color:black") self.resultText = QtWidgets.QTextEdit(self.groupBox) self.resultText.setGeometry(QtCore.QRect(20,60,260,240)) self.resultText.setObjectName("resultText") self.resultText.setFont(QFont("微软雅黑",10,QFont.Bold)) self.resultText.setStyleSheet("color:black") self.label = QtWidgets.QLabel(self.groupBox) self.label.setGeometry(QtCore.QRect(20,20,80,30)) self.label.setObjectName("laber") self.label2 = QtWidgets.QLabel(self.groupBox) self.label2.setGeometry(QtCore.QRect(15,20,80,610)) self.label2.setObjectName("laber2") self.searchButton = QtWidgets.QPushButton(self) self.searchButton.setGeometry(QtCore.QRect(50,380,90,30)) self.searchButton.setObjectName("searchButton") self.choiceButton = QtWidgets.QPushButton(self) self.choiceButton.setGeometry(QtCore.QRect(160,380,90,30)) self.choiceButton.setObjectName("choiceButton") self.quitButton = QtWidgets.QPushButton(self) self.quitButton.setGeometry(QtCore.QRect(270,380,90,30)) self.quitButton.setObjectName("quitButton")
self.retranslateUI() self.searchButton.clicked.connect(self.searchMusic) self.choiceButton.clicked.connect(self.choiceMusic) self.quitButton.clicked.connect(self.quitWindows)
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUI(self): self.setWindowTitle("网易云API") self.groupBox.setTitle("输入搜索的目标") self.label.setText("歌曲&歌手") self.label2.setText("请输入编号") self.searchButton.setText("搜索") self.choiceButton.setText("播放") self.quitButton.setText("退出")
if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) win = MainWin() palette = QPalette() palette.setBrush(QPalette.Background, QBrush(QPixmap("D:\PythonProject\images\YHellow.png"))) win.setPalette(palette) win.show() sys.exit(app.exec_())
|