getVideo/getvideo.py


Home Back

#!/usr/bin/env python3

import webview,os,subprocess,time,re
from utils import *

from sys import platform



class Api:
    
    proc=None
    wasKilled=False
    
    
    
    
    def killDownload(self):
        self.proc.kill()
        self.wasKilled=True
    
    
    def updateYoutubeDl(self):
        if os.path.exists(getConfigDirPath()+"youtube-dl.exe"):
            os.remove(getConfigDirPath()+"youtube-dl.exe")
        self.checkForYoutubeDl()
        
    def checkForYoutubeDl(self):
        if not os.path.exists(getConfigDirPath()):
            os.mkdir(getConfigDirPath())
        if not os.path.exists(getConfigDirPath()+"youtube-dl.exe"):
            js='''$("#out").html("<p>Scaricamento youtube-dl...</p>");'''
            window.evaluate_js(js)
            
            js='''$("#loading").removeClass("hidden")'''
            window.evaluate_js(js)
            downloadFile("https://yt-dl.org/downloads/latest/youtube-dl.exe", getConfigDirPath()+"youtube-dl.exe")
            js='''$("#loading").addClass("hidden")'''
            window.evaluate_js(js)
            js='''$("#out").html("<p>Youtube-dl aggiornato.</p>");'''
            window.evaluate_js(js)
        
    def download(self,url):
        si=None
            
        self.wasKilled=False
        if not os.path.exists(getDownloadPath()):
            os.mkdir(getDownloadPath())
        
        if platform == "linux" or platform == "linux2":
            os.chdir(getDownloadPath())
            youtubedl="youtube-dl"
        elif platform == "darwin":
            os.chdir(getDownloadPath())
            youtubedl="youtube-dl"
        elif platform == "win32":
            si=subprocess.STARTUPINFO()
            si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            os.chdir(getDownloadPath())
            youtubedl=getConfigDirPath()+"youtube-dl.exe" 
            self.checkForYoutubeDl()
        
        js='''$("#btnCmd").prop("disabled",true);$("#btnKill").prop("disabled",false);'''
        window.evaluate_js(js)
        
        js='''$("#loading").removeClass("hidden")'''
        window.evaluate_js(js)
        
        cmd="{0} {1} %s".format(youtubedl, url)
        self.proc = subprocess.Popen(cmd.split(), bufsize=-1 ,startupinfo=si,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        while self.proc.poll() is None :
            out = self.proc.stdout.readline()
            r=out.decode("utf-8").split()
            r=" ".join(r)
            r=r.replace("'","")
            r=r.replace('"','')
            js='$("#out").html("<p>%s</p>");' % r
            window.evaluate_js(js)
        
        
        if self.wasKilled==True:
            js='''$("#loading").addClass("hidden");
            $("#out").append("<p><b>Scaricamento interrotto dall\'utente.</b></p>");
            $("#btnCmd").prop("disabled",false);$("#btnKill").prop("disabled",true);
            '''
            window.evaluate_js(js)
            return
        
        
        js='''$("#loading").addClass("hidden")'''
        window.evaluate_js(js)
        
        js="$('#out').append('<p><b>Scaricamento terminato.</b></p>');"
        window.evaluate_js(js)
        js="$('#out').append('<p><b>Troverai il file in %s</b></p>');" % getDownloadPath().replace("\\","\\\\")
        window.evaluate_js(js)
        js='''$("#btnCmd").prop("disabled",false);$("#btnKill").prop("disabled",true);'''
        window.evaluate_js(js)
        
        
        if platform == "linux" or platform == "linux2":
            os.system("xdg-open %s &" % getDownloadPath())    
        elif platform == "win32":
            os.system("explorer.exe %s " % getDownloadPath())    


api = Api()
we=""

if platform == "linux" or platform == "linux2":
    we="gtk"
elif platform == "win32":
    we="edgechromium"    

def removeYoutubeDl():
    try:
        os.remove(getConfigDirPath()+"youtube-dl.exe")
        print("Youtube-dl.exe cancellato.")
        js="$('#out').html('<p class=\"ui inverted green segment\"><b>Youtube-dl.exe cancellato.</b></p>');"
        window.evaluate_js(js)
    except:
        print("Problemi nel cancellare youtube-dl.exe")
        js="$('#out').html('<p class=\"ui inverted red segment\"><b>Problemi nel cancellare youtube-dl.exe</b></p>');"
        window.evaluate_js(js)
    
    
window=webview.create_window('Scarica un video dal web', 'index.html', js_api=api, min_size=(1024, 350),width=1024, height=350)
window.expose(removeYoutubeDl)

print(dir(window.gui))
webview.start(gui=we,debug=False)



Powered by Code, a simple repository browser by Fabio Di Matteo