diff --git a/GUI/MainWindow.ui b/GUI/MainWindow.ui index 724dbc3..d229794 100644 --- a/GUI/MainWindow.ui +++ b/GUI/MainWindow.ui @@ -58,7 +58,7 @@ 20 - 86 + 90 551 24 @@ -83,7 +83,7 @@ 20 - 120 + 125 551 31 diff --git a/GUI/ProgressDialog.ui b/GUI/ProgressDialog.ui new file mode 100644 index 0000000..91539b0 --- /dev/null +++ b/GUI/ProgressDialog.ui @@ -0,0 +1,81 @@ + + + Dialog + + + + 0 + 0 + 500 + 120 + + + + + 0 + 0 + + + + + 500 + 120 + + + + + 500 + 120 + + + + Dialog + + + + + 20 + 30 + 451 + 21 + + + + + Courier New + 14 + + + + Downloading... + + + + + + 20 + 60 + 451 + 25 + + + + 100 + + + 0 + + + true + + + Qt::Horizontal + + + false + + + + + + diff --git a/NGAudioDownloader.exe b/NGAudioDownloader.exe index 977d58d..ac6bcbd 100644 Binary files a/NGAudioDownloader.exe and b/NGAudioDownloader.exe differ diff --git a/NGAudioDownloader.py b/NGAudioDownloader.py index 353433e..225d331 100644 --- a/NGAudioDownloader.py +++ b/NGAudioDownloader.py @@ -1,10 +1,14 @@ +import sys from PyQt5.uic import loadUi from PyQt5.QtWidgets import ( QApplication, QMainWindow, QWidget, QDialog, QFileDialog, QInputDialog ) +from PyQt5.Qt import QThread, pyqtSignal +from PyQt5 import QtCore from requests import get as load from bs4 import BeautifulSoup as parse +from time import sleep class GUI(QMainWindow): @@ -12,8 +16,10 @@ class GUI(QMainWindow): super().__init__() loadUi('GUI/MainWindow.ui',self) self.dlbtn.clicked.connect(self.download) + self.inprocess=False def download(self): + self.inprocess=True # Validating input songID=self.input.text() 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] if not dist: return # Downloading - with open(dist,'wb') as out: - out.write(load(link).content) + file=load(link,stream=True) + 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.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__': from sys import argv app=QApplication(argv) ngad=GUI() + progress=ProgressDialog() ngad.show() - app.exec_() \ No newline at end of file + sys.exit(app.exec_()) \ No newline at end of file