好友
阅读权限10
听众
最后登录1970-1-1
|
cjs25
发表于 2021-6-21 12:16
本帖最后由 cjs25 于 2021-6-28 17:33 编辑
论坛里音乐下载的好像没有界面,我试着用pyqt5作个界面,看好不好用。今天再更新一下,加入判断,尽量让搜索时不会闪退。我自己在这边试都没有闪退啊,不知道你们下载回去会不会我就不知道了。
[Python] 纯文本查看 复制代码 from mp30620 import Ui_MainWindow
import sys
import os
from PyQt5 import QtCore
from PyQt5.QtCore import QThread, pyqtSignal,QCoreApplication
import requests
import json
import time
import random
from PyQt5.QtWidgets import QDesktopWidget, QApplication, QFileDialog, QMessageBox, QTableWidgetItem
from PyQt5 import QtWidgets
def random_user():
user1 = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
user2 = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"
user3 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"
user4 = "Mozilla/5.0 (Windows NT 6.1; rv,2.0.1) Gecko/20100101 Firefox/4.0.1"
user5 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)"
user6 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)"
user7 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
user8 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)"
user = [user1, user2, user3, user4, user5, user6, user7, user8]
user = random.choice(user)
header = {"User-Agent": user}
return header
class BackendThread(QThread):
updata_data = pyqtSignal(str, int)
def __init__(self, parent=None):
super(BackendThread, self).__init__(parent)
def run_(self, url, muc_name,muc_lrc, count):
self.url = url
self.muc_lrc=muc_lrc
self.muc_name = muc_name
self.count = count
def run(self):
try:
reponse = requests.get(self.url, headers=random_user())
if reponse.status_code == 200:
with open(self.muc_name, 'wb') as f:
for chunk in reponse.iter_content(chunk_size=512):
if chunk:
f.write(chunk)
cc=os.path.getsize(self.muc_name)
if cc<=970000:
os.remove(self.muc_name)
self.updata_data.emit('文件太小,下载失败!', self.count)
else:
if self.muc_lrc != '':
lrc_name = self.muc_name.split('.')[-2]+'.lrc'
with open(lrc_name, 'wb') as f:
f.write(self.muc_lrc)
self.updata_data.emit('下载成功!', self.count)
except:
self.updata_data.emit('下载失败!', self.count)
class firstfrom(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.radioButton.setChecked(True)
self.ui.pushButton.clicked.connect(self.star_find)
self.ui.pushButton_4.clicked.connect(self.down)
self.ui.pushButton_2.clicked.connect(self.change_dir)
self.ui.checkBox.clicked.connect(self.check)
self.ui.pushButton_5.clicked.connect(QCoreApplication.instance().quit)
self.backend = BackendThread()
def check(self):
if self.ui.tableWidget.rowCount() != 0:
for i in range(0, self.ui.tableWidget.rowCount()):
self.ui.tableWidget.item(i, 0).setCheckState(self.ui.checkBox.checkState())
def change_dir(self):
dir_ = QFileDialog.getExistingDirectory(self, '选择文件夹', '/')
if dir_ != '':
self.ui.label_2.setText(dir_)
else:
self.ui.label_2.setText('./mp3')
def text_write(self, wrd, cont):
new_item = QTableWidgetItem(wrd)
new_item.setTextAlignment(QtCore.Qt.AlignCenter)
self.ui.tableWidget.setItem(cont, 4, new_item)
def down(self):
if self.ui.tableWidget.rowCount() == 0:
msg_box = QMessageBox(QMessageBox.Warning, '警告', '请先搜索歌曲!')
msg_box.exec_()
else:
if not os.path.exists(self.ui.label_2.text()):
os.mkdir(self.ui.label_2.text())
kk = self.ui.tableWidget.rowCount()
for num in range(0, kk):
if self.ui.tableWidget.item(num, 0).checkState() == 2:
new_item = QTableWidgetItem('正在下载')
new_item.setTextAlignment(QtCore.Qt.AlignCenter)
self.ui.tableWidget.setItem(num, 4, new_item )
muc_url = self.ui.tableWidget.item(num, 5).text()
muc_lrc = self.ui.tableWidget.item(num, 6).text().encode('utf-8')
muc_name = self.ui.label_2.text() + '/' + self.ui.tableWidget.item(num,
0).text() + '_' + self.ui.tableWidget.item(
num, 1).text() + '.' + muc_url.split('.')[-1]
count = num
time.sleep(1)
self.backend.run_(muc_url, muc_name,muc_lrc, count)
self.backend.start()
self.backend.updata_data.connect(self.text_write)
def star_find(self):
if self.ui.lineEdit.text() == '':
msg_box = QMessageBox(QMessageBox.Warning, '警告', '请输入要搜索的关键词')
msg_box.exec_()
else:
self.ui.tableWidget.clear()
self.ui.tableWidget.setColumnCount(7)
self.ui.tableWidget.setColumnWidth(0, 250)
self.ui.tableWidget.setColumnWidth(1, 150)
self.ui.tableWidget.setColumnWidth(2, 100)
self.ui.tableWidget.setColumnWidth(3, 120)
self.ui.tableWidget.setColumnWidth(4, 120)
self.ui.tableWidget.setColumnWidth(5, 0)
self.ui.tableWidget.setColumnWidth(6, 0)
# self.ui.tableWidget.setRowCount(30)
self.ui.tableWidget.setHorizontalHeaderLabels(['歌曲名', '歌手信息', '歌曲时长', '歌曲尺寸', '下载状态'])
key_word = self.ui.lineEdit.text()
url = 'https://songsearch.kugou.com/song_search_v2?callback=jQuery112404852453033521309_1585374178035&keyword={}&page=1&pagesize=100&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1585374178039'.format(
key_word)
try:
r = requests.get(url, headers=random_user())
r.encoding = r.apparent_encoding
JsonText = json.loads(str(r.text).strip('jQuery112404852453033521309_1585374178035(')[:-2])
except:
msg_box = QMessageBox(QMessageBox.Warning, '警告', '网站有点卡,你再试试看')
msg_box.exec_()
m = 0
for i in JsonText["data"]["lists"]:
if self.ui.radioButton.isChecked() == True:
FileHash = i['SQFileHash']
elif self.ui.radioButton_2.isChecked() == True:
FileHash = i['FileHash']
AlbumID = i['AlbumID']
downurl = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191012215974743400859_1585619590127&hash={}&album_id={}&dfid=0zI24KbQfP0cPOWD2MSu40&mid=415d42b77b039a48daf4f63d5b411ceb&platid=4&_=1585619590128'.format(
FileHash, AlbumID)
try:
response = requests.get(downurl, headers=random_user())
if 'play_url' in response.text:
json_TEXt = json.loads(str(response.text).strip("jQuery19108584376284926096_1585328784250(")[:-2])[
'data']
url = json_TEXt['play_url']
if url != '':
m += 1
self.ui.tableWidget.setRowCount(m)
music_mane = json_TEXt['song_name']
singer = json_TEXt['author_name']
lyr = json_TEXt['lyrics']
music_time = str(int(json_TEXt['timelength']) // 60000) + '分' + str(
int(json_TEXt['timelength'] / 1000) % 60) + '秒'
music_size = round(int(json_TEXt['filesize']) / 1024 / 1024, 2)
new_item1 = QTableWidgetItem(music_mane)
new_item1.setTextAlignment(QtCore.Qt.AlignCenter)
new_item2 = QTableWidgetItem(singer)
new_item2.setTextAlignment(QtCore.Qt.AlignCenter)
new_item3 = QTableWidgetItem(music_time)
new_item3.setTextAlignment(QtCore.Qt.AlignCenter)
new_item4 = QTableWidgetItem(str(music_size) + 'MB')
new_item4.setTextAlignment(QtCore.Qt.AlignCenter)
new_item6 = QTableWidgetItem(url)
new_item6.setTextAlignment(QtCore.Qt.AlignCenter)
new_item7 = QTableWidgetItem(lyr)
new_item7.setTextAlignment(QtCore.Qt.AlignCenter)
self.ui.tableWidget.setItem(m-1, 0, QTableWidgetItem(new_item1))
self.ui.tableWidget.item(m-1, 0).setCheckState(0)
self.ui.tableWidget.setItem(m-1, 1, QTableWidgetItem(new_item2))
self.ui.tableWidget.setItem(m-1, 2, QTableWidgetItem(new_item3))
self.ui.tableWidget.setItem(m-1, 3, QTableWidgetItem(new_item4))
self.ui.tableWidget.setItem(m-1, 5, QTableWidgetItem(new_item6))
self.ui.tableWidget.setItem(m-1, 6, QTableWidgetItem(new_item7))
QApplication.processEvents()
except:
continue
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = firstfrom()
screen = QDesktopWidget().screenGeometry()
size = w.geometry()
w.setWindowTitle('全网音乐下载器')
left = (screen.width() - size.width()) / 2
hight = (screen.height() - size.height()) / 2
w.move(left, hight)
w.show()
sys.exit(app.exec_())
上面的是主程序代码,下面是界面的代码
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '0501mp3.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(772, 528)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(0, 160, 791, 351))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(550, 20, 201, 121))
self.groupBox.setObjectName("groupBox")
self.layoutWidget = QtWidgets.QWidget(self.groupBox)
self.layoutWidget.setGeometry(QtCore.QRect(20, 20, 161, 101))
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.radioButton = QtWidgets.QRadioButton(self.layoutWidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(18)
self.radioButton.setFont(font)
self.radioButton.setObjectName("radioButton")
self.verticalLayout.addWidget(self.radioButton)
self.radioButton_2 = QtWidgets.QRadioButton(self.layoutWidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(18)
self.radioButton_2.setFont(font)
self.radioButton_2.setObjectName("radioButton_2")
self.verticalLayout.addWidget(self.radioButton_2)
self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(350, 110, 191, 31))
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(18)
self.checkBox.setFont(font)
self.checkBox.setObjectName("checkBox")
self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget1.setGeometry(QtCore.QRect(10, 10, 521, 34))
self.layoutWidget1.setObjectName("layoutWidget1")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget1)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.layoutWidget1)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.layoutWidget1)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.layoutWidget1)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(18)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.layoutWidget2 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget2.setGeometry(QtCore.QRect(10, 60, 521, 31))
self.layoutWidget2.setObjectName("layoutWidget2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget2)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(16)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_2.addWidget(self.pushButton_2)
self.label_2 = QtWidgets.QLabel(self.layoutWidget2)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(18)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.layoutWidget3 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget3.setGeometry(QtCore.QRect(10, 110, 306, 33))
self.layoutWidget3.setObjectName("layoutWidget3")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.layoutWidget3)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.pushButton_4 = QtWidgets.QPushButton(self.layoutWidget3)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(16)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_3.addWidget(self.pushButton_4)
self.pushButton_5 = QtWidgets.QPushButton(self.layoutWidget3)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(16)
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_3.addWidget(self.pushButton_5)
self.horizontalLayout_4.addLayout(self.horizontalLayout_3)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.groupBox.setTitle(_translate("MainWindow", "歌曲音质:"))
self.radioButton.setText(_translate("MainWindow", "无损音质"))
self.radioButton_2.setText(_translate("MainWindow", "高清音质"))
self.checkBox.setText(_translate("MainWindow", "全选/全不选"))
self.label.setText(_translate("MainWindow", "请输入要搜索的歌曲"))
self.pushButton.setText(_translate("MainWindow", "搜索"))
self.pushButton_2.setText(_translate("MainWindow", "保存路径选择"))
self.label_2.setText(_translate("MainWindow", "./mp3"))
self.pushButton_4.setText(_translate("MainWindow", "下载"))
self.pushButton_5.setText(_translate("MainWindow", "退出"))
都是在刚学习状态,大伙多多指教。下面是成品链接:
无损高清MP3下载
https://wws.lanzoui.com/iAnIRqqp10d
密码:3xmu |
免费评分
-
查看全部评分
本帖被以下淘专辑推荐:
- · 精品工具盒|主题: 2345, 订阅: 1052
|