2017-10-28 5 views
0

Youtubeのビデオ用の小さなダウンローダで、trollius(async)、htmlpy、およびpafy(およびyoutube-dl)を使用していました。今、私は、ダウンロードボタンをクリックすると、UIがフリーズし、backendがビデオをダウンロードしている間に問題が発生します。htmlpy uiのブロックを停止する方法

私はdownloaderクラスを独自のスレッドにして、それをUIと一緒に実行しようとしましたが、うまくいかないようです。 UIが続行している間に、ビデオを非同期的にダウンロードするためにcoroutineを使用しようとしました。どちらもうまくいかなかった。ここで

は、いくつかのコード

class Downloader(htmlPy.Object,threading.Thread): 
dd = os.path.join(os.getenv('USERPROFILE'), 'Downloads') # dd = download director 
dd = dd + "/vindownload/" 

def __init__(self, app): 
    threading.Thread.__init__(self) 
    super(Downloader, self).__init__() 
    # Initialize the class here, if required. 
    self.app = app 
    return 

@htmlPy.Slot(str) 
def download_single(self, json_data): 
    form_data = json.loads(json_data) 
    print json_data 
    url = form_data["name"] 
    dt = form_data["dt"] # Download type is audio or video 
    if url.__contains__("https://www.youtube.com/watch?v="): 
     if dt == 'audio': 
      print "hello1" 
      loop = trollius.get_event_loop() 
      loop.run_until_complete(self._downloadVid(url, vid=False)) 
      loop.stop() 
     else: 
      print "hello1" 
      loop = trollius.get_event_loop() 
      loop.run_until_complete(self._downloadVid(url, vid=True)) 
      loop.stop() 
     self.app.evaluate_javascript("document.getElementById('form').reset()") 
    else: 
     print "Incorrect url" 
    print form_data 

@trollius.coroutine 
def _downloadVid(self, url, vid=False, order_reverse=False, vinName=None): 
    print "hello123" 
    video = pafy.new(url) 
    print video 
    name = u''.join(video.title).encode('utf8') 
    name = re.sub("[<>:\"/\\|?*]", "", name) 
    if not vid: 
     file = video.getbestaudio() 
    else: 
     file = video.getbest() 
    if (order_reverse): 
     file.download(self.dd + vinName + name + ".mp4", quiet=False,callback=self.mycb) 
    else: 
     file.download(self.dd + name + ".mp4", quiet=False,callback=self.mycb) 

def mycb(self,total, recvd, ratio, rate, eta): 
    print(recvd, ratio, eta) 

と私のinitialize.py

BASE_DIR = os.path.abspath(os.path.dirname("initilize.py")) 

app = htmlPy.AppGUI(title=u"Vin download", width=700, height=400, resizable=False) 

app.static_path = os.path.join(BASE_DIR, "static/") 
app.template_path = os.path.join(BASE_DIR, "templates/") 

app.web_app.setMaximumWidth(830) 
app.web_app.setMaximumHeight(600) 

download = Downloader(app) 
download.start() 

# Register back-end functionalities 
app.bind(download) 
app.template = ("./index.html", {"template_variable_name": "value"}) 

# Instructions for running application 
if __name__ == "__main__": 
    # The driver file will have to be imported everywhere in back-end. 
    # So, always keep app.start() in if __name__ == "__main__" conditional 
    app.start() 

は今、私の質問をされています。ダウンロード中に私がUIを解放する方法はありますか?アプリケーションがクラッシュしたように見えません。

私はPython 2.7、Trollius、Pafy、Youtube-dl、HTMLPYを使用しています。

ありがとうございます。

答えて

0

申し訳ありませんが、私は私の質問に対する答えを見つけました。

@htmlPy.Slot(str) 
def download_single(self, json_data): 
    form_data = json.loads(json_data) 
    print json_data 
    url = form_data["name"] 
    dt = form_data["dt"] # Download type is audio or video 
    videoDown = videoDownload(url, dt, self.app, self.dd) 
    videoDown.start() 
    print form_data 

と私は以前それで持っていたコードが今videoDownloadは、上記のすべてのこれらの属性を取ると呼ばれる新しいクラスに転送されます:私は次のように私のdownload_single方法を変更しました。

videoDownload.py

class videoDownload(threading.Thread): 
def __init__(self, url, dt, app, dd): 
    threading.Thread.__init__(self) 
    self.url = url 
    self.dt = dt 
    self.app = app 
    self.dd = dd 

def run(self): 
    threads.append(self) 
    if self.url.__contains__("https://www.youtube.com/watch?v="): 
     if self.dt == 'audio': 
      print "hello1" 
      self._downloadVid(self.url, vid=False) 
     else: 
      print "hello1" 
      self._downloadVid(self.url, vid=True) 
    else: 
     print "Incorrect url" 

def _downloadVid(self, url, vid=False, order_reverse=False, vinName=None): 
    print "hello123" 
    video = pafy.new(url) 
    print video 
    name = u''.join(video.title).encode('utf8') 
    name = re.sub("[<>:\"/\\|?*]", "", name) 
    if not vid: 
     file = video.getbestaudio() 
    else: 
     file = video.getbest() 
    if (order_reverse): 
     file.download(self.dd + vinName + name + ".mp4", quiet=False, callback=self.mycb) 
    else: 
     file.download(self.dd + name + ".mp4", quiet=False, callback=self.mycb) 
    threads.remove(self) 

def mycb(self, total, recvd, ratio, rate, eta): 
    pass 

これは私が、UIがブロックされているとしていた問題を解決しました。スレッドはrunメソッドから一度終了し、クラスの上に定義されているスレッド配列から削除されます。

は素晴らしい一日、すべての

〜Ellisan

を持っています
関連する問題