from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
from PyQt6.QtCore import QThread, QObject, pyqtSignal
from pytube import Playlist, YouTube
from progress.bar import IncrementalBar
import sys
class Progress(QThread):
change_value = pyqtSignal(int)
completed = pyqtSignal(bool)
def download(link, path, mp, self): # <--------
l = 0
self.change_value.emit(l)
yt = YouTube(link)
stream = yt.streams.filter(only_audio=mp).first()
chars_to_remove = ["|", "<", ">", ":", "\"", "\\", "/", "?", "*"]
ti = yt.title
for char in chars_to_remove:
ti = ti.replace(char, "")
stream.download(filename=f"{path}{ti}.mp3")
l = 100
self.change_value.emit(l)
def download_playlist(link, path, mp, self):
link = Playlist(link)
p = 0
self.change_value.emit(p)
for i in link.video_urls:
yt = YouTube(i)
stream = yt.streams.filter(only_audio=mp).first()
chars_to_remove = ["|", "<", ">", ":", "\"", "\\", "/", "?", "*"]
ti = yt.title
for char in chars_to_remove:
ti = ti.replace(char, "")
stream.download(filename=f"{path}{ti}.mp3")
p += 1
l = len(link) / 100 * p
self.change_value.emit(l)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = uic.loadUi('design.ui', self)
self.ui.browse.clicked.connect(self.open_folder)
self.folderpath = ''
self.ui.convert.clicked.connect(self.Change)
def open_folder(self):
self.folderpath = QFileDialog.getExistingDirectory(self, 'Select Folder')
self.ui.lineSrc.setText(str(self.folderpath))
def Change(self):
if self.lineUrl != '' and self.lineSrc != '':
if self.ui.QChange.currentIndex() == 0:
if self.ui.TChange.currentIndex() == 0:
i = False
elif self.ui.TChange.currentIndex() == 1:
i = True
else:
print('error')
self.download_progress(self.ui.lineUrl.text(), str(self.folderpath), i)
elif self.ui.QChange.currentIndex() == 1:
if self.ui.TChange.currentIndex() == 0:
i = False
elif self.ui.TChange.currentIndex() == 1:
i = True
else:
print('error')
self.download_playlist_progress(self.ui.lineUrl.text(), str(self.folderpath), i)
else:
print('error')
def download_progress(self, link, path, mp):
self.thread = Progress.download(link, path, mp)
self.thread.change_value.connect(self.setProgressVal)
self.thread.start()
def download_playlist_progress(self, link, path, mp):
self.thread = Progress.download_playlist(link, path, mp)
self.thread.change_value.connect(self.setProgressVal)
self.thread.start()
def setProgressVal(self, val):
self.ui.progressBar.setValue(val)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
def download(link, path, mp, self): # требуется self аргументом
l = 0
self.change_value.emit(l) # используется self
...
Progress.download(link, path, mp) # Метод вызывается у класса, никакие экземпляры не передаются
...
progress = Progress() # создание экземпляра класса
thread = Progress.download(link, path, mp, progress) # вызов метода и передача экземпляра класса
self.thread.start()
...
def download(self, link, path, mp,): # ставим self первым
l = 0
self.change_value.emit(l)
...
...
progress = Progress() # создание экземпляра класса
thread = progress.download(link, path, mp) # то же самое что и Progress.download(progress, link, path, mp)
self.thread.start()