Ahmed_Ab96

youtubeDownloader

Aug 27th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.96 KB | None | 0 0
  1. # i uploaded widget.ui here: https://drive.google.com/open?id=1m0lzqafwirpAZesLLkbFtnKIBQjuAQSs
  2.  
  3. from PyQt5.QtWidgets import *
  4. from PyQt5.QtGui import *
  5. from PyQt5.QtCore import *
  6. import os,time,sys,pafy,humanize
  7. import threading
  8. import os.path
  9. import random
  10. import pyspeedtest
  11. from PyQt5 import uic
  12. from PyQt5.uic import loadUiType
  13. Window = uic.loadUiType("widget.ui")
  14. from os import path
  15.  
  16. ui_path = os.path.dirname(os.path.abspath(__file__))
  17. myui = uic.loadUiType(os.path.join(ui_path, "widget.ui"))[0]
  18. class KYD(QWidget,myui):
  19.     urlSignal = pyqtSignal(str)
  20.     selectedQuality=pyqtSignal(int,str)
  21.     locationToSave=pyqtSignal(str)
  22.     filename=pyqtSignal(str)
  23.     play_list=pyqtSignal(str,str)
  24.     pause=pyqtSignal()
  25.  
  26.     TITLE, SIZE,DURATION = range(3)
  27.  
  28.     def __init__(self,parent=None):
  29.         super(QWidget,self).__init__(parent)
  30.  
  31.         self.setupUi(self)
  32.         self.setWindowTitle('داگرتنی ڤیدیۆکانی یوتوب')
  33.  
  34.         QApplication.clipboard().dataChanged.connect(self.clip)
  35.  
  36.         self.Url.setPlaceholderText('تکایەبەستەری ڤیدیۆ لیرە دابنێ !')
  37.  
  38.         self.pasteBtn.clicked.connect(self.get_qualites)
  39.  
  40.         path=QStandardPaths.writableLocation(QStandardPaths.DownloadLocation)
  41.         self.savelocation.setText(path)
  42.  
  43.         self.savelocation.setReadOnly(1)
  44.         self.savelocation.setPlaceholderText('لەکوێ پاشەکەوتی دەکەیت؟')
  45.         self.getlocation.clicked.connect(self.getpath)
  46.  
  47.         self.dataView.setRootIsDecorated(False)
  48.         self.dataView.setAlternatingRowColors(True)
  49.  
  50.         self.model=self.createModel(self)
  51.         self.dataView.setModel(self.model)
  52.  
  53.  
  54.         self.downloadsBtn.clicked.connect(self.downloading)
  55.         self.stopBtn.clicked.connect(self.control_download)
  56.  
  57.         self.playlist_check.setToolTip('چالاکی بکە ئەگەر ڤیدیۆکەت پلەی لیستە!')
  58.  
  59.         self.worker=workers()
  60.         self.mythread=QThread()
  61.  
  62.     def information(self,name):
  63.         self.addData(self.model,name)
  64.  
  65.     def createModel(self,parent):
  66.         model=QStandardItemModel(0,1,parent)
  67.         model.setHeaderData(self.TITLE,Qt.Horizontal,'ناونیشان')
  68.  
  69.         return model
  70.  
  71.     def addData(self,model,TITLE):
  72.         model.insertRow(0)
  73.         model.setData(model.index(0,self.TITLE),TITLE)
  74.  
  75.  
  76.     def getQuality_vdieos(self):
  77.         try:
  78.             url=self.Url.text()
  79.             self.worker.moveToThread(self.mythread)
  80.             self.urlSignal.connect(self.worker.qualities)
  81.             self.urlSignal.emit(url)
  82.             self.mythread.start()
  83.             self.worker.info.connect(self.information)
  84.             self.worker.urlLink.connect(self.insertQulaity)
  85.             #self.worker.finished.connect(self.stop_thread)
  86.         except Exception as e:
  87.             print(str(e))
  88.  
  89.     def getQuality_audio(self):
  90.         try:
  91.             url = self.Url.text()
  92.             self.worker.moveToThread(self.mythread)
  93.             self.urlSignal.connect(self.worker.qualities_sound)
  94.             self.urlSignal.emit(url)
  95.             self.mythread.start()
  96.             self.worker.info.connect(self.information)
  97.             self.worker.urlLink.connect(self.insertQulaity)
  98.         except Exception as e:
  99.             print(str(e))
  100.  
  101.     def get_qualites(self):
  102.         try:
  103.             self.chooseQuality.clear()
  104.             if self.radioButton_3.isChecked()==False:
  105.                self.getQuality_vdieos()
  106.             else:
  107.                 self.getQuality_audio()
  108.         except Exception as e:
  109.             print(str(e))
  110.  
  111.     def insertQulaity(self,values):
  112.         try:
  113.             for qualities in values:
  114.                 self.chooseQuality.addItem(str(qualities),qualities)
  115.         except Exception as e:
  116.             print(str(e))
  117.  
  118.  
  119.     def download_OneVideo(self):
  120.         try:
  121.             quality=self.chooseQuality.currentIndex()
  122.             location=self.savelocation.text()
  123.  
  124.             self.worker.moveToThread(self.mythread)
  125.             self.selectedQuality.connect(self.worker.download_video)
  126.             self.selectedQuality.emit(quality,location)
  127.             self.mythread.start()
  128.  
  129.             self.worker.selected_quality.connect(self.worker.download_video)
  130.             self.worker.progressChanged.connect(self.progressBar.setValue)
  131.             self.worker.totalANDrecvd.connect(self.totlasize.setText)
  132.             self.worker.downloadANDUploates.connect(self.speedDownloadUpload.setText)
  133.             self.worker.existsFile.connect(self.ErrorMessages)
  134.             self.worker.finished.connect(self.stop_thread)
  135.         except Exception as e:
  136.             print(str(e))
  137.  
  138.     def download_OneAudio(self):
  139.         try:
  140.             quality=self.chooseQuality.currentIndex()
  141.             location=self.savelocation.text()
  142.  
  143.             self.worker.moveToThread(self.mythread)
  144.             self.selectedQuality.connect(self.worker.download_sound)
  145.             self.selectedQuality.emit(quality,location)
  146.             self.mythread.start()
  147.  
  148.             self.worker.selected_quality.connect(self.worker.download_sound)
  149.             self.worker.progressChanged.connect(self.progressBar.setValue)
  150.             self.worker.totalANDrecvd.connect(self.totlasize.setText)
  151.             self.worker.downloadANDUploates.connect(self.speedDownloadUpload.setText)
  152.  
  153.             self.worker.existsFile.connect(self.ErrorMessages)
  154.         except Exception as e:
  155.             print(str(e))
  156.  
  157.     def download_playList(self):
  158.         try:
  159.             url=self.Url.text()
  160.             location=self.savelocation.text()
  161.             self.worker.moveToThread(self.mythread)
  162.             self.play_list.connect(self.worker.download_playlist)
  163.             self.play_list.emit(url,location)
  164.             self.mythread.start()
  165.             self.worker.play_list_Download.connect(self.worker.download_playlist)
  166.             self.worker.progressChanged.connect(self.progressBar.setValue)
  167.             self.worker.totalANDrecvd.connect(self.totlasize.setText)
  168.             self.worker.downloadANDUploates.connect(self.speedDownloadUpload.setText)
  169.             self.worker.info.connect(self.information)
  170.             self.worker.existsFile.connect(self.ErrorMessages)
  171.  
  172.         except Exception as e:
  173.             print(str(e))
  174.  
  175.     def download_playList_Audio(self):
  176.         try:
  177.             url=self.Url.text()
  178.             location=self.savelocation.text()
  179.  
  180.             self.worker.moveToThread(self.mythread)
  181.             self.play_list.connect(self.worker.download_playlist_sound)
  182.             self.play_list.emit(url,location)
  183.             self.mythread.start()
  184.             self.worker.play_list_Download.connect(self.worker.download_playlist_sound)
  185.             self.worker.progressChanged.connect(self.progressBar.setValue)
  186.             self.worker.totalANDrecvd.connect(self.totlasize.setText)
  187.             self.worker.downloadANDUploates.connect(self.speedDownloadUpload.setText)
  188.             self.worker.info.connect(self.information)
  189.             self.worker.existsFile.connect(self.ErrorMessages)
  190.  
  191.         except Exception as e:
  192.             print(str(e))
  193.  
  194.  
  195.     def downloading(self):
  196.         if self.playlist_check.isChecked() and self.radioButton_3.isChecked()==False:
  197.             self.stopBtn.setDisabled(0)
  198.             self.download_playList()
  199.  
  200.         elif self.playlist_check.isChecked() and self.radioButton_3.isChecked():
  201.             self.stopBtn.setDisabled(0)
  202.             self.download_playList_Audio()
  203.         elif self.radioButton_3.isChecked():
  204.             if self.chooseQuality.currentText() !='':
  205.                 self.download_OneAudio()
  206.             else:
  207.                 pass
  208.         else:
  209.             if self.chooseQuality.currentText() !='':
  210.                 self.download_OneVideo()
  211.             else:
  212.                 pass
  213.  
  214.     def clip(self):
  215.         t=QApplication.clipboard().text()
  216.         self.Url.setText(t)
  217.  
  218.     def getpath(self):
  219.         try:
  220.             path=QFileDialog.getExistingDirectory(self,'شوێنێک دیاری بکە')
  221.             self.savelocation.setText(path)
  222.  
  223.         except Exception as e:
  224.             print(str(e))
  225.  
  226.     def ErrorMessages(self,error):
  227.         try:
  228.             mbox=QMessageBox()
  229.             mbox.setText(error)
  230.             OK=mbox.addButton('باشە',QMessageBox.YesRole)
  231.             mbox.exec_()
  232.             if mbox.buttonClicked()==OK:
  233.                 self.mythread.wait(msecs=10)
  234.  
  235.             # self.worker.moveToThread(self.mythread)
  236.             # self.mythread.start()
  237.             # self.worker.existsFile.connect(self.mbox)
  238.  
  239.         except Exception as e:
  240.             print(str(e))
  241.  
  242.     def control_download(self):
  243.         #self.worker.moveToThread(self.mythread)
  244.         self.pause.connect(self.worker.pauseDownload)
  245.         self.pause.emit()
  246.         if self.stopBtn.text()=='بوەستە':
  247.             self.worker.PauseDownloadPlaylist=True
  248.             self.stopBtn.setText('بەردەوام بە')
  249.         else:
  250.             self.stopBtn.setText('بوەستە')
  251.             self.worker.PauseDownloadPlaylist=False
  252.  
  253.     def stop_thread(self):
  254.         print('hhh')
  255.         self.mythread.terminate()
  256.  
  257.  
  258. class workers(QObject):
  259.  
  260.     selected_quality=pyqtSignal(int,str)
  261.     play_list_Download = pyqtSignal(str, str)
  262.     progressChanged=pyqtSignal(int)
  263.     urlLink=pyqtSignal(list)
  264.     play_listLink=pyqtSignal(list)
  265.     existsFile=pyqtSignal(str)
  266.     info=pyqtSignal(str)
  267.  
  268.     downloadANDUploates=pyqtSignal(str)
  269.     totalANDrecvd=pyqtSignal(str)
  270.     finished=pyqtSignal()
  271.  
  272.     def __init__(self):
  273.         super().__init__()
  274.         self.PauseDownloadPlaylist=False
  275.  
  276.     def qualities(self, url):
  277.         try:
  278.             if url !='':
  279.                 self.source = pafy.new(url)
  280.                 self.videos_streams = self.source.videostreams
  281.                 self.info.emit(self.source.title)
  282.                 for self.videos in self.videos_streams:
  283.                     size=humanize.naturalsize(self.videos.get_filesize())
  284.                     data=['{}     {}     {}'.format(self.videos.resolution,self.videos.extension,size)]
  285.                     self.urlLink.emit(data)
  286.  
  287.         except Exception as e:
  288.             print(str(e))
  289.  
  290.  
  291.     def download_video(self,quality,location):
  292.         try:
  293.             path_vid = location + '/' + self.source.title +'.'+ self.videos.extension
  294.  
  295.             if  os.path.exists(path_vid) ==False:
  296.                 self.videos_streams[quality].download(filepath=location,callback=self.callback)
  297.             else:
  298.                 self.existsFile.emit('ببورە ئەم فایلە پێشتر داونڵۆد کراوە\n ئەگەر دەتەوێت دووبارە داونڵۆدی بکە شوێنی پاشەکەوتکردن بگۆڕە!')
  299.  
  300.             self.finished.emit()
  301.  
  302.         except Exception as e:
  303.             print(str(e))
  304.  
  305.     def qualities_sound(self, url):
  306.         try:
  307.             if url !='':
  308.                 self.source = pafy.new(url)
  309.                 self.audiostream = self.source.audiostreams
  310.                 self.info.emit(self.source.title)
  311.                 for self.audios in self.audiostream:
  312.                     size=humanize.naturalsize(self.audios.get_filesize())
  313.                     data=['{}     {}'.format(self.audios.extension,size)]
  314.                     self.urlLink.emit(data)
  315.             else:
  316.                 pass
  317.         except Exception as e:
  318.             print(str(e))
  319.  
  320.  
  321.     def download_sound(self,quality,location):
  322.         try:
  323.             self.audiostream = self.source.audiostreams
  324.             path = location + '/' + self.source.title +'.'+ self.audios.extension
  325.  
  326.             if  os.path.exists(path) ==False:
  327.                 self.audiostream[quality].download(filepath=location,callback=self.callback)
  328.             else:
  329.                 self.existsFile.emit('ببورە ئەم فایلە پێشتر داونڵۆد کراوە\n ئەگەر دەتەوێت دووبارە داونڵۆدی بکە شوێنی پاشەکەوتکردن بگۆڕە!')
  330.  
  331.         except Exception as e:
  332.             print(str(e))
  333.             self.audiostream[quality].download(filepath=location, callback=self.callback)
  334.  
  335.     def download_playlist(self, url,location):
  336.         try:
  337.             self.playlists = pafy.get_playlist(str(url))
  338.             self.allvideo = self.playlists['items']
  339.  
  340.             if os.path.exists(location + '/' +self.playlists['title']):
  341.                 os.chdir(location + '/' +self.playlists['title'])
  342.             else:
  343.                 os.mkdir(location + '/' +self.playlists['title'])
  344.                 os.chdir(location + '/' +self.playlists['title'])
  345.  
  346.             for vidoes in range(len(self.playlists['items'])):
  347.                 path1=os.path.isfile(location +  '/' + self.allvideo[vidoes]['pafy'].title  + '.' +'mp4')
  348.                 path2=os.path.isfile(location +  '/' + self.allvideo[vidoes]['pafy'].title  + '.' +'webm')
  349.                 path3 = os.path.isfile(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' + 'webm.temp')
  350.                 print(location +  '/' + self.allvideo[vidoes]['pafy'].title  + '.' +'webm')
  351.  
  352.                 v=self.allvideo[vidoes]['pafy'].getbest()
  353.                 if True:
  354.                     self.info.emit(str(vidoes+1)+' -  ' + self.allvideo[vidoes]['pafy'].title)
  355.                     if  path1==False and path2 ==False and path3==False:
  356.                         while self.PauseDownloadPlaylist==True:
  357.                             time.sleep(1)
  358.                         v.download(callback=self.callback)
  359.                     else:
  360.                         self.existsFile.emit('ئەم ڤیدیۆیە دووبارەیە!')
  361.  
  362.                 else:
  363.                     pass
  364.         except Exception as e:
  365.             print(str(e))
  366.  
  367.  
  368.     def download_playlist_sound(self, url,location):
  369.         try:
  370.             self.playlists = pafy.get_playlist(str(url))
  371.             self.allvideo = self.playlists['items']
  372.  
  373.             if os.path.exists(location + '/' +self.playlists['title']):
  374.                 os.chdir(location + '/' +self.playlists['title'])
  375.             else:
  376.                 os.mkdir(location + '/' +self.playlists['title'])
  377.                 os.chdir(location + '/' +self.playlists['title'])
  378.  
  379.             for vidoes in range(len(self.playlists['items'])):
  380.                 path1=os.path.isfile(location +  '/' + self.allvideo[vidoes]['pafy'].title  + '.' +'mp4')
  381.                 path2=os.path.isfile(location +  '/' + self.allvideo[vidoes]['pafy'].title  + '.' +'webm')
  382.                 path3 = os.path.isfile(location + '/' + self.allvideo[vidoes]['pafy'].title + '.' + 'webm.temp')
  383.                 print(location +  '/' + self.allvideo[vidoes]['pafy'].title  + '.' +'webm')
  384.  
  385.                 print(path1,path2)
  386.  
  387.                 v=self.allvideo[vidoes]['pafy'].getbestaudio()
  388.                 print(v)
  389.                 if True:
  390.                     self.info.emit(str(vidoes+1)+' -  ' + self.allvideo[vidoes]['pafy'].title)
  391.                     if  path1==False and path2 ==False and path3==False:
  392.                         v.download(callback=self.callback)
  393.                     else:
  394.                         self.existsFile.emit('ئەم ڤیدیۆیە دووبارەیە!')
  395.  
  396.                 else:
  397.                     pass
  398.         except Exception as e:
  399.             print(str(e))
  400.  
  401.     def callback(self, total, recvd, ratio, rate, etc):
  402.         try:
  403.             value = int(ratio * 100)
  404.             if value==100:
  405.                 self.finished.emit()
  406.  
  407.             totalsizes=humanize.naturalsize(total)
  408.             recvid=humanize.naturalsize(recvd)
  409.  
  410.             self.progressChanged.emit(value)
  411.             transfer=humanize.naturalsize(rate *1024)
  412.             self.totalANDrecvd.emit('   {}  |  {}'.format(recvid,totalsizes))
  413.             self.downloadANDUploates.emit('{}'.format(transfer))
  414.  
  415.         except Exception as e:
  416.             print(str(e))
  417.  
  418.     def pauseDownload(self):
  419.         self.pauseDownload=True
  420.  
  421. if __name__=='__main__':
  422.     APP=QApplication(sys.argv)
  423.     kyd=KYD()
  424.    # kyd.resize(600,480)
  425.     kyd.show()
  426.    # splashScreen.finish(SP)
  427.     sys.exit(APP.exec_())
Advertisement
Add Comment
Please, Sign In to add comment