Optimized and added progressbar dialog

This commit is contained in:
Masahiko AMANO 2020-08-23 22:39:29 +03:00
parent e17533e148
commit 6e704e27a9
4 changed files with 134 additions and 5 deletions

View File

@ -58,7 +58,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>20</x> <x>20</x>
<y>86</y> <y>90</y>
<width>551</width> <width>551</width>
<height>24</height> <height>24</height>
</rect> </rect>
@ -83,7 +83,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>20</x> <x>20</x>
<y>120</y> <y>125</y>
<width>551</width> <width>551</width>
<height>31</height> <height>31</height>
</rect> </rect>

81
GUI/ProgressDialog.ui Normal file
View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>500</width>
<height>120</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>500</width>
<height>120</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>500</width>
<height>120</height>
</size>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>451</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Courier New</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="text">
<string>Downloading...</string>
</property>
</widget>
<widget class="QProgressBar" name="bar">
<property name="geometry">
<rect>
<x>20</x>
<y>60</y>
<width>451</width>
<height>25</height>
</rect>
</property>
<property name="maximum">
<number>100</number>
</property>
<property name="value">
<number>0</number>
</property>
<property name="textVisible">
<bool>true</bool>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="invertedAppearance">
<bool>false</bool>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

Binary file not shown.

View File

@ -1,10 +1,14 @@
import sys
from PyQt5.uic import loadUi from PyQt5.uic import loadUi
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QApplication, QMainWindow, QWidget,
QDialog, QFileDialog, QInputDialog QDialog, QFileDialog, QInputDialog
) )
from PyQt5.Qt import QThread, pyqtSignal
from PyQt5 import QtCore
from requests import get as load from requests import get as load
from bs4 import BeautifulSoup as parse from bs4 import BeautifulSoup as parse
from time import sleep
class GUI(QMainWindow): class GUI(QMainWindow):
@ -12,8 +16,10 @@ class GUI(QMainWindow):
super().__init__() super().__init__()
loadUi('GUI/MainWindow.ui',self) loadUi('GUI/MainWindow.ui',self)
self.dlbtn.clicked.connect(self.download) self.dlbtn.clicked.connect(self.download)
self.inprocess=False
def download(self): def download(self):
self.inprocess=True
# Validating input # Validating input
songID=self.input.text() songID=self.input.text()
page=parse(load(f'https://www.newgrounds.com/audio/listen/{songID}').text,'html.parser') page=parse(load(f'https://www.newgrounds.com/audio/listen/{songID}').text,'html.parser')
@ -34,14 +40,56 @@ class GUI(QMainWindow):
'MP3 Audio File (*.mp3)')[0] 'MP3 Audio File (*.mp3)')[0]
if not dist: return if not dist: return
# Downloading # Downloading
with open(dist,'wb') as out: file=load(link,stream=True)
out.write(load(link).content) global progress
progress.label.setText(f'Downloading "{songTitle}"...')
progress.setWindowTitle(f'Downloading "{songTitle}"...')
progress.bar.setValue(0)
progress.show()
self.thread=DownloadThread(file,dist)
self.thread.got_chunk.connect(lambda done:progress.bar.setValue(done))
self.thread.finished.connect(self.thread.finish)
self.thread.start()
self.input.clear() self.input.clear()
self.inprocess=False
class ProgressDialog(QDialog):
def __init__(self):
super().__init__()
loadUi('GUI/ProgressDialog.ui',self)
self.setWindowFlags(self.windowFlags()&~QtCore.Qt.WindowCloseButtonHint)
class DownloadThread(QThread):
got_chunk=pyqtSignal(object)
def __init__(self,file,dist):
super().__init__()
self.file,self.dist=file,dist
def run(self):
global progress
total=self.file.headers.get('content-length')
if total is None: total=-1
else: total=int(total)
downloaded=0
with open(self.dist,'wb') as out:
for data in self.file.iter_content(chunk_size=max(total//100,1024)):
downloaded+=len(data)
out.write(data)
self.got_chunk.emit(100*downloaded//total)
def finish(self):
global progress
progress.hide()
self.deleteLater()
if __name__ == '__main__': if __name__ == '__main__':
from sys import argv from sys import argv
app=QApplication(argv) app=QApplication(argv)
ngad=GUI() ngad=GUI()
progress=ProgressDialog()
ngad.show() ngad.show()
app.exec_() sys.exit(app.exec_())