运行效果
main
import requests,os,re
from gui import Ui_Form #UI
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import Qt, QPoint, QThread,pyqtSignal
import q #qrc生成的py #样式表
import logo #任务栏图标
#设置任务栏
import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("com.example.myapp")
#下载位置 #桌面
fl=os.path.join(os.path.expanduser("~"),"Desktop")
#API
ur='https://api.mu-jie.cc/douyin?url='
#子线程下载
class MyThread(QThread):
progress_updated = pyqtSignal(int) #c
name_received = pyqtSignal(str) # 定义信号,传递 str 类型的参数
def __init__(self,url):
super().__init__()
self.url = url #网址参数
def run(self):
try:
gu = requests.get(self.url).json()
code = gu.get('msg') #是否获取成功
author = gu.get('data').get('author') #作者
title = gu.get('data').get('title') #标题
music_url = gu.get('data').get('music').get('url')#音乐网址
self.name_received.emit(code) #发送获取信号
lis = title + '-' + author
for i in lis.split('\n'):
name = '' + i
r = requests.get(music_url) #获取音乐
with open(os.path.join(fl, f'{name}.mp3'), 'wb') as f:#下载
total_length = int(r.headers.get('content-length'))
if total_length is None:
f.write(r.content)
else:
dl = 0
for data in r.iter_content(chunk_size=1024):
dl += len(data)
f.write(data)
percent = int(100 * dl / total_length)
self.progress_updated.emit(percent) # 发送下载进度信号
self.name_received.emit(f'{name} 下载完毕') # 发送下载完毕信号
except Exception as e:
self.name_received.emit(f'下载错误: {e}')
class guWindow(QWidget):
def __init__(self):
super().__init__()
self.gu = Ui_Form()
self.gu.setupUi(self)
# print(self.ui.__dict__) # 查看ui文件中有哪些控件
an = self.gu.pushButton_3 # 按钮
self.gu.lineEdit.returnPressed.connect(self.gumou) #lineEdit回车运行
an.clicked.connect(self.gumou) # 给按钮绑定函数
self.user_name_qwidget = self.gu.lineEdit
# ✦✦✦✦✦✦✦✦✦✦设置无边框 和可拖动✦✦✦✦✦✦✦✦✦✦✦✦✦固定代码
self.setWindowOpacity(0.90) # 设置窗口透明度
self.setWindowFlag(Qt.FramelessWindowHint) # 去除边框
self.setAttribute(Qt.WA_TranslucentBackground) # 去除白色背景
self.offset = QPoint() # 记录鼠标按下的初始位置
def mousePressEvent(self, event):
self.offset = event.pos()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(self.pos() + event.pos() - self.offset) # 移动窗口位置
def gumou(self): # 按钮绑定的函数 功能
s = self.user_name_qwidget.text()
url_pattern = r'https://\S+'
urls = re.findall(url_pattern, s)
# 输出提取到的网址
for ul in urls:
print('开始解析')
url = ur + ul
self.user_name_qwidget.clear()
self.my_thread = MyThread(url) # 创建线程
self.my_thread.progress_updated.connect(self.update_progress) # 连接下载进度信号
self.my_thread.name_received.connect(self.receive_name) # 连接信号
self.my_thread.start() # 开始线程
def receive_name(self, name): #接收name
self.gu.textBrowser.clear()
self.gu.textBrowser.append(name)
def update_progress(self, percent): #进度条
self.gu.progressBar.setValue(percent)
if __name__ == '__main__':
app = QApplication(sys.argv)
icon = QtGui.QIcon(':/ks.png')
app.setWindowIcon(icon)
# 创建可拖动窗口实例
ui = guWindow() #函数
# 显示窗口
ui.show()
# 启动应用程序事件循环
sys.exit(app.exec_())
q.qrc
<RCC>
<qresource prefix="img">
<file>img/hua.png</file>
<file>img/sdz.jpeg</file>
<file>img/url.png</file>
</qresource>
<qresource prefix="icons">
<file>img/关闭.png</file>
<file>img/最小化.png</file>
</qresource>
</RCC>
logo.qrc
<RCC>
<qresource>
<file>ks.png</file>
</qresource>
</RCC>
图片