吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2775|回复: 13
收起左侧

[Python 转载] 来个酷我音乐带界历的下载器,在自学状态,帮多指点下

[复制链接]
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", "退出 程序"))

感觉不太稳定,下载时有时会有闪退,有高手帮忙指点下!不知道哪里出了问题!

免费评分

参与人数 6吾爱币 +6 热心值 +5 收起 理由
深水夜藏 + 1 + 1 我很赞同!
hj170520 + 1 + 1 我很赞同!
Pau250 + 1 支持原创
xiaoxiao0633 + 1 用心讨论,共获提升!
amus + 1 + 1 我很赞同!
chermy + 2 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

baby528 发表于 2021-2-19 12:06
本帖最后由 baby528 于 2021-2-19 12:18 编辑

楼上说的有道理,可能是firstfrom类中down函数里 self.down = down_thread() 在每次循环都初始化造成的
但是这个down_thread类又和每一个下载任务相关(下载几个mp3就需要定义几个实例),所以在这里加个延时函数应该能行
在 self.down = down_thread() 这一句之前加了 time.sleep(1) ,测试了2次没出问题
licongliyu 发表于 2021-2-20 11:23
没必要等待 你说提前生成不行 是你的代码逻辑问题 在界面初始化的时候 self.xx=xx_thread(), self.xx.xx_signal_thread.connect(self.taks_1) , self.download_button.clicked.connect(self.task_2)
在self.task_1函数里取下载线程返回的结果,在self.task_2里给线程传值,同时在这里用self.xx.start()
rhtk1 发表于 2021-2-17 18:57
Pau250 发表于 2021-2-17 19:07
支持原创
biostu 发表于 2021-2-17 19:18
学习了,不错。
sizhubiao 发表于 2021-2-17 19:55
不明觉厉,感谢楼主分享!
洋流的爱 发表于 2021-2-17 20:56
虽然看不懂,但我一直在学习
xzcnmb 发表于 2021-2-17 22:43
学习学习
bloodwolf4177 发表于 2021-2-18 17:41
楼主,厉害了,学习学习
yoyoma211 发表于 2021-2-18 18:33
不明觉厉,进来学习下
深水夜藏 发表于 2021-2-18 21:33
感谢分享,支持
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 19:36

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表