Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # i uploaded widget.ui here: https://drive.google.com/open?id=1m0lzqafwirpAZesLLkbFtnKIBQjuAQSs
- from PyQt5.QtWidgets import *
- from PyQt5.QtGui import *
- from PyQt5.QtCore import *
- import os,time,sys,pafy,humanize
- import threading
- import os.path
- import random
- import pyspeedtest
- from PyQt5 import uic
- from PyQt5.uic import loadUiType
- Window = uic.loadUiType("widget.ui")
- from os import path
- ui_path = os.path.dirname(os.path.abspath(__file__))
- myui = uic.loadUiType(os.path.join(ui_path, "widget.ui"))[0]
- class KYD(QWidget,myui):
- urlSignal = pyqtSignal(str)
- selectedQuality=pyqtSignal(int,str)
- locationToSave=pyqtSignal(str)
- filename=pyqtSignal(str)
- play_list=pyqtSignal(str,str)
- pause=pyqtSignal()
- TITLE, SIZE,DURATION = range(3)
- def __init__(self,parent=None):
- super(QWidget,self).__init__(parent)
- self.setupUi(self)
- self.setWindowTitle('داگرتنی ڤیدیۆکانی یوتوب')
- QApplication.clipboard().dataChanged.connect(self.clip)
- self.Url.setPlaceholderText('تکایەبەستەری ڤیدیۆ لیرە دابنێ !')
- self.pasteBtn.clicked.connect(self.get_qualites)
- path=QStandardPaths.writableLocation(QStandardPaths.DownloadLocation)
- self.savelocation.setText(path)
- self.savelocation.setReadOnly(1)
- self.savelocation.setPlaceholderText('لەکوێ پاشەکەوتی دەکەیت؟')
- self.getlocation.clicked.connect(self.getpath)
- self.dataView.setRootIsDecorated(False)
- self.dataView.setAlternatingRowColors(True)
- self.model=self.createModel(self)
- self.dataView.setModel(self.model)
- self.downloadsBtn.clicked.connect(self.downloading)
- self.stopBtn.clicked.connect(self.control_download)
- self.playlist_check.setToolTip('چالاکی بکە ئەگەر ڤیدیۆکەت پلەی لیستە!')
- self.worker=workers()
- self.mythread=QThread()
- def information(self,name):
- self.addData(self.model,name)
- def createModel(self,parent):
- model=QStandardItemModel(0,1,parent)
- model.setHeaderData(self.TITLE,Qt.Horizontal,'ناونیشان')
- return model
- def addData(self,model,TITLE):
- model.insertRow(0)
- model.setData(model.index(0,self.TITLE),TITLE)
- def getQuality_vdieos(self):
- try:
- url=self.Url.text()
- self.worker.moveToThread(self.mythread)
- self.urlSignal.connect(self.worker.qualities)
- self.urlSignal.emit(url)
- self.mythread.start()
- self.worker.info.connect(self.information)
- self.worker.urlLink.connect(self.insertQulaity)
- #self.worker.finished.connect(self.stop_thread)
- except Exception as e:
- print(str(e))
- def getQuality_audio(self):
- try:
- url = self.Url.text()
- self.worker.moveToThread(self.mythread)
- self.urlSignal.connect(self.worker.qualities_sound)
- self.urlSignal.emit(url)
- self.mythread.start()
- self.worker.info.connect(self.information)
- self.worker.urlLink.connect(self.insertQulaity)
- except Exception as e:
- print(str(e))
- def get_qualites(self):
- try:
- self.chooseQuality.clear()
- if self.radioButton_3.isChecked()==False:
- self.getQuality_vdieos()
- else:
- self.getQuality_audio()
- except Exception as e:
- print(str(e))
- def insertQulaity(self,values):
- try:
- for qualities in values:
- self.chooseQuality.addItem(str(qualities),qualities)
- except Exception as e:
- print(str(e))
- def download_OneVideo(self):
- try:
- quality=self.chooseQuality.currentIndex()
- location=self.savelocation.text()
- self.worker.moveToThread(self.mythread)
- self.selectedQuality.connect(self.worker.download_video)
- self.selectedQuality.emit(quality,location)
- self.mythread.start()
- self.worker.selected_quality.connect(self.worker.download_video)
- self.worker.progressChanged.connect(self.progressBar.setValue)
- self.worker.totalANDrecvd.connect(self.totlasize.setText)
- self.worker.downloadANDUploates.connect(self.speedDownloadUpload.setText)
- self.worker.existsFile.connect(self.ErrorMessages)
- self.worker.finished.connect(self.stop_thread)
- except Exception as e:
- print(str(e))
- def download_OneAudio(self):
- try:
- quality=self.chooseQuality.currentIndex()
- location=self.savelocation.text()
- self.worker.moveToThread(self.mythread)
- self.selectedQuality.connect(self.worker.download_sound)
- self.selectedQuality.emit(quality,location)
- self.mythread.start()
- self.worker.selected_quality.connect(self.worker.download_sound)
- self.worker.progressChanged.connect(self.progressBar.setValue)
- self.worker.totalANDrecvd.connect(self.totlasize.setText)
- self.worker.downloadANDUploates.connect(self.speedDownloadUpload.setText)
- self.worker.existsFile.connect(self.ErrorMessages)
- except Exception as e:
- print(str(e))
- def download_playList(self):
- try:
- url=self.Url.text()
- location=self.savelocation.text()
- self.worker.moveToThread(self.mythread)
- self.play_list.connect(self.worker.download_playlist)
- self.play_list.emit(url,location)
- self.mythread.start()
- self.worker.play_list_Download.connect(self.worker.download_playlist)
- self.worker.progressChanged.connect(self.progressBar.setValue)
- self.worker.totalANDrecvd.connect(self.totlasize.setText)
- self.worker.downloadANDUploates.connect(self.speedDownloadUpload.setText)
- self.worker.info.connect(self.information)
- self.worker.existsFile.connect(self.ErrorMessages)
- except Exception as e:
- print(str(e))
- def download_playList_Audio(self):
- try:
- url=self.Url.text()
- location=self.savelocation.text()
- self.worker.moveToThread(self.mythread)
- self.play_list.connect(self.worker.download_playlist_sound)
- self.play_list.emit(url,location)
- self.mythread.start()
- self.worker.play_list_Download.connect(self.worker.download_playlist_sound)
- self.worker.progressChanged.connect(self.progressBar.setValue)
- self.worker.totalANDrecvd.connect(self.totlasize.setText)
- self.worker.downloadANDUploates.connect(self.speedDownloadUpload.setText)
- self.worker.info.connect(self.information)
- self.worker.existsFile.connect(self.ErrorMessages)
- except Exception as e:
- print(str(e))
- def downloading(self):
- if self.playlist_check.isChecked() and self.radioButton_3.isChecked()==False:
- self.stopBtn.setDisabled(0)
- self.download_playList()
- elif self.playlist_check.isChecked() and self.radioButton_3.isChecked():
- self.stopBtn.setDisabled(0)
- self.download_playList_Audio()
- elif self.radioButton_3.isChecked():
- if self.chooseQuality.currentText() !='':
- self.download_OneAudio()
- else:
- pass
- else:
- if self.chooseQuality.currentText() !='':
- self.download_OneVideo()
- else:
- pass
- def clip(self):
- t=QApplication.clipboard().text()
- self.Url.setText(t)
- def getpath(self):
- try:
- path=QFileDialog.getExistingDirectory(self,'شوێنێک دیاری بکە')
- self.savelocation.setText(path)
- except Exception as e:
- print(str(e))
- def ErrorMessages(self,error):
- try:
- mbox=QMessageBox()
- mbox.setText(error)
- OK=mbox.addButton('باشە',QMessageBox.YesRole)
- mbox.exec_()
- if mbox.buttonClicked()==OK:
- self.mythread.wait(msecs=10)
- # self.worker.moveToThread(self.mythread)
- # self.mythread.start()
- # self.worker.existsFile.connect(self.mbox)
- except Exception as e:
- print(str(e))
- def control_download(self):
- #self.worker.moveToThread(self.mythread)
- self.pause.connect(self.worker.pauseDownload)
- self.pause.emit()
- if self.stopBtn.text()=='بوەستە':
- self.worker.PauseDownloadPlaylist=True
- self.stopBtn.setText('بەردەوام بە')
- else:
- self.stopBtn.setText('بوەستە')
- self.worker.PauseDownloadPlaylist=False
- def stop_thread(self):
- print('hhh')
- self.mythread.terminate()
- class workers(QObject):
- selected_quality=pyqtSignal(int,str)
- play_list_Download = pyqtSignal(str, str)
- progressChanged=pyqtSignal(int)
- urlLink=pyqtSignal(list)
- play_listLink=pyqtSignal(list)
- existsFile=pyqtSignal(str)
- info=pyqtSignal(str)
- downloadANDUploates=pyqtSignal(str)
- totalANDrecvd=pyqtSignal(str)
- finished=pyqtSignal()
- def __init__(self):
- super().__init__()
- self.PauseDownloadPlaylist=False
- def qualities(self, url):
- try:
- if url !='':
- self.source = pafy.new(url)
- self.videos_streams = self.source.videostreams
- self.info.emit(self.source.title)
- for self.videos in self.videos_streams:
- size=humanize.naturalsize(self.videos.get_filesize())
- data=['{} {} {}'.format(self.videos.resolution,self.videos.extension,size)]
- self.urlLink.emit(data)
- except Exception as e:
- print(str(e))
- def download_video(self,quality,location):
- try:
- path_vid = location + '/' + self.source.title +'.'+ self.videos.extension
- if os.path.exists(path_vid) ==False:
- self.videos_streams[quality].download(filepath=location,callback=self.callback)
- else:
- self.existsFile.emit('ببورە ئەم فایلە پێشتر داونڵۆد کراوە\n ئەگەر دەتەوێت دووبارە داونڵۆدی بکە شوێنی پاشەکەوتکردن بگۆڕە!')
- self.finished.emit()
- except Exception as e:
- print(str(e))
- def qualities_sound(self, url):
- try:
- if url !='':
- self.source = pafy.new(url)
- self.audiostream = self.source.audiostreams
- self.info.emit(self.source.title)
- for self.audios in self.audiostream:
- size=humanize.naturalsize(self.audios.get_filesize())
- data=['{} {}'.format(self.audios.extension,size)]
- self.urlLink.emit(data)
- else:
- pass
- except Exception as e:
- print(str(e))
- def download_sound(self,quality,location):
- try:
- self.audiostream = self.source.audiostreams
- path = location + '/' + self.source.title +'.'+ self.audios.extension
- if os.path.exists(path) ==False:
- self.audiostream[quality].download(filepath=location,callback=self.callback)
- else:
- self.existsFile.emit('ببورە ئەم فایلە پێشتر داونڵۆد کراوە\n ئەگەر دەتەوێت دووبارە داونڵۆدی بکە شوێنی پاشەکەوتکردن بگۆڕە!')
- except Exception as e:
- print(str(e))
- self.audiostream[quality].download(filepath=location, callback=self.callback)
- def download_playlist(self, url,location):
- try:
- self.playlists = pafy.get_playlist(str(url))
- self.allvideo = self.playlists['items']
- if os.path.exists(location + '/' +self.playlists['title']):
- os.chdir(location + '/' +self.playlists['title'])
- else:
- os.mkdir(location + '/' +self.playlists['title'])
- os.chdir(location + '/' +self.playlists['title'])
- for vidoes in range(len(self.playlists['items'])):
- path1=os.path.isfile(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' +'mp4')
- path2=os.path.isfile(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' +'webm')
- path3 = os.path.isfile(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' + 'webm.temp')
- print(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' +'webm')
- v=self.allvideo[vidoes]['pafy'].getbest()
- if True:
- self.info.emit(str(vidoes+1)+' - ' + self.allvideo[vidoes]['pafy'].title)
- if path1==False and path2 ==False and path3==False:
- while self.PauseDownloadPlaylist==True:
- time.sleep(1)
- v.download(callback=self.callback)
- else:
- self.existsFile.emit('ئەم ڤیدیۆیە دووبارەیە!')
- else:
- pass
- except Exception as e:
- print(str(e))
- def download_playlist_sound(self, url,location):
- try:
- self.playlists = pafy.get_playlist(str(url))
- self.allvideo = self.playlists['items']
- if os.path.exists(location + '/' +self.playlists['title']):
- os.chdir(location + '/' +self.playlists['title'])
- else:
- os.mkdir(location + '/' +self.playlists['title'])
- os.chdir(location + '/' +self.playlists['title'])
- for vidoes in range(len(self.playlists['items'])):
- path1=os.path.isfile(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' +'mp4')
- path2=os.path.isfile(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' +'webm')
- path3 = os.path.isfile(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' + 'webm.temp')
- print(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' +'webm')
- print(path1,path2)
- v=self.allvideo[vidoes]['pafy'].getbestaudio()
- print(v)
- if True:
- self.info.emit(str(vidoes+1)+' - ' + self.allvideo[vidoes]['pafy'].title)
- if path1==False and path2 ==False and path3==False:
- v.download(callback=self.callback)
- else:
- self.existsFile.emit('ئەم ڤیدیۆیە دووبارەیە!')
- else:
- pass
- except Exception as e:
- print(str(e))
- def callback(self, total, recvd, ratio, rate, etc):
- try:
- value = int(ratio * 100)
- if value==100:
- self.finished.emit()
- totalsizes=humanize.naturalsize(total)
- recvid=humanize.naturalsize(recvd)
- self.progressChanged.emit(value)
- transfer=humanize.naturalsize(rate *1024)
- self.totalANDrecvd.emit(' {} | {}'.format(recvid,totalsizes))
- self.downloadANDUploates.emit('{}'.format(transfer))
- except Exception as e:
- print(str(e))
- def pauseDownload(self):
- self.pauseDownload=True
- if __name__=='__main__':
- APP=QApplication(sys.argv)
- kyd=KYD()
- # kyd.resize(600,480)
- kyd.show()
- # splashScreen.finish(SP)
- sys.exit(APP.exec_())
Advertisement
Add Comment
Please, Sign In to add comment