好友
阅读权限10
听众
最后登录1970-1-1
|
cjs25
发表于 2021-2-17 18:19
[Python] 纯文本查看 复制代码 from PyQt5 import QtWidgets
from 酷我 import Ui_MainWindow
from PyQt5.QtWidgets import QDesktopWidget,QFileDialog,QMessageBox,QTableWidgetItem,QApplication,QWidget,QTableWidget,QCheckBox
from PyQt5 import QtCore
import sys
import os
import requests
import random
from PyQt5.QtCore import QThread,pyqtSignal,QCoreApplication
def get_url( url):
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}
req= requests.get(url, headers=header,timeout=20)
return req
class down_thread(QThread):
updata_data=pyqtSignal(str,int)
def __init__(self, parent=None):
super(down_thread, self).__init__(parent)
def run_(self, music_id, filename,count):
self.ID = music_id
self.filename = filename
self.count = count
def run(self):
url = 'https://yueyue.kuwo.cn/api/service/music/audioUrl/{}'.format(self.ID)
music_url = get_url(url).json()['data']['audioUrl']
repone = requests.get(music_url, stream=True, timeout=20)
try:
if repone.status_code == 200:
with open(self.filename, mode='wb') as f:
for chunk in repone.iter_content(chunk_size=1024):
f.write(chunk)
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.pushButton_4.clicked.connect(self.change_dir)
self.ui.pushButton.clicked.connect(self.find)
self.ui.checkBox.clicked.connect(self.check)
self.ui.pushButton_2.clicked.connect(self.down)
self.ui.pushButton_3.clicked.connect(QCoreApplication.instance().quit)
def down(self):
if self.ui.tableWidget.rowCount()==0 :
msg_box = QMessageBox(QMessageBox.Warning, '告示', '请先搜索音乐')
msg_box.exec_()
else:
for cont in range(self.ui.tableWidget.rowCount()):
if self.ui.tableWidget.item(cont, 0).checkState() == 2:
new_item = QTableWidgetItem('正在下载。。。。。')
new_item.setTextAlignment(QtCore.Qt.AlignCenter)
self.ui.tableWidget.setItem(cont, 3, QTableWidgetItem(new_item))
mp3_name = self.ui.label_2.text() + '/' + self.ui.tableWidget.item(cont, 0).text()+'_'+self.ui.tableWidget.item(cont, 1).text()+'.mp3'
self.down = down_thread()
self.down.run_(self.ui.tableWidget.item(cont, 2).text(), mp3_name, cont)
self.down.start()
self.down.updata_data.connect(self.write_word)
def write_word(self,word,cont):
new_item4 = QTableWidgetItem(word)
new_item4.setTextAlignment(QtCore.Qt.AlignCenter)
self.ui.tableWidget.setItem(cont, 3, QTableWidgetItem(new_item4))
def find(self):
if self.ui.lineEdit.text() =='':
msg_box = QMessageBox(QMessageBox.Warning, '警告', '请输入要搜索的关键词')
msg_box.exec_()
else:
url = 'http://yueyue.kuwo.cn/api/search/music/list?pn=0&rn=300&keyword={}&uid=-1&token='.format(
self.ui.lineEdit.text())
music_name = get_url(url).json()['data']['resultList']
self.ui.tableWidget.clear()
self.ui.tableWidget.setColumnWidth(0, 200)
self.ui.tableWidget.setColumnWidth(1, 200)
self.ui.tableWidget.setColumnWidth(2, 120)
self.ui.tableWidget.setColumnWidth(3, 200)
self.ui.tableWidget.setColumnCount(4)
self.ui.tableWidget.setRowCount(len(music_name))
self.ui.tableWidget.setHorizontalHeaderLabels(['歌曲名', '歌手信息', '下载ID', '下载进度'])
for row in range(len(music_name)):
new_item1 = QTableWidgetItem(music_name[row]['songName'])
new_item1.setTextAlignment(QtCore.Qt.AlignCenter)
new_item2 = QTableWidgetItem(music_name[row]['artist'])
new_item2.setTextAlignment(QtCore.Qt.AlignCenter)
new_item3 = QTableWidgetItem(music_name[row]['musicRid'].split('_')[1])
new_item3.setTextAlignment(QtCore.Qt.AlignCenter)
self.ui.tableWidget.setItem(row, 0, QTableWidgetItem(new_item1))
self.ui.tableWidget.item(row, 0).setCheckState(0)
self.ui.tableWidget.setItem(row, 1, QTableWidgetItem(new_item2))
self.ui.tableWidget.setItem(row, 2, QTableWidgetItem(new_item3))
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):
fname = QFileDialog.getExistingDirectory(self, '选择文件夹', '/')
if fname != '':
self.ui.label_2.setText(fname)
else:
self.ui.label_2.setText('./mp3')
if __name__ =='__main__':
app=QtWidgets.QApplication(sys.argv)
w =firstfrom()
screen=QDesktopWidget().screenGeometry()
size=w.geometry()
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 '酷我.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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(783, 606)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(0, 130, 781, 451))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(10, 90, 131, 41))
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(12)
self.checkBox.setFont(font)
self.checkBox.setObjectName("checkBox")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(190, 90, 111, 31))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(12)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(310, 90, 191, 31))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(12)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(0, 10, 761, 71))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.widget1 = QtWidgets.QWidget(self.centralwidget)
self.widget1.setGeometry(QtCore.QRect(520, 80, 241, 41))
self.widget1.setObjectName("widget1")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget1)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton_2 = QtWidgets.QPushButton(self.widget1)
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(12)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_2.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(self.widget1)
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(12)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_2.addWidget(self.pushButton_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"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "歌曲名"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "歌手名"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "歌曲ID"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "下载状态"))
self.checkBox.setText(_translate("MainWindow", "全选 /全不选"))
self.pushButton_4.setText(_translate("MainWindow", "保存目录"))
self.label_2.setText(_translate("MainWindow", "./mp3"))
self.label.setText(_translate("MainWindow", "歌曲关键字:"))
self.pushButton.setText(_translate("MainWindow", "开始搜索"))
self.pushButton_2.setText(_translate("MainWindow", "开始下载"))
self.pushButton_3.setText(_translate("MainWindow", "退出 程序"))
感觉不太稳定,下载时有时会有闪退,有高手帮忙指点下!不知道哪里出了问题! |
免费评分
-
查看全部评分
|