youtube-dl-gui/youtube_dl_gui/updatemanager.py

97 lines
2.8 KiB
Python
Raw Permalink Normal View History

2014-05-22 08:43:36 -07:00
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
2014-03-01 09:12:37 -08:00
2015-01-24 05:34:02 -08:00
"""Youtubedlg module to update youtube-dl binary.
Attributes:
UPDATE_PUB_TOPIC (string): wxPublisher subscription topic of the
UpdateThread thread.
"""
from __future__ import unicode_literals
2014-12-06 08:02:45 -08:00
import os.path
from threading import Thread
2014-03-01 09:12:37 -08:00
from urllib2 import urlopen, URLError, HTTPError
2014-07-20 08:47:07 -07:00
from wx import CallAfter
from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub as Publisher
from .utils import (
2014-12-06 08:02:45 -08:00
YOUTUBEDL_BIN,
check_path
2014-04-30 08:34:25 -07:00
)
2014-03-26 07:28:07 -07:00
2015-01-24 05:34:02 -08:00
UPDATE_PUB_TOPIC = 'update'
2014-03-01 09:12:37 -08:00
class UpdateThread(Thread):
2014-04-30 03:38:56 -07:00
2014-12-24 02:00:42 -08:00
"""Python Thread that downloads youtube-dl binary.
2014-07-20 08:47:07 -07:00
2014-12-24 02:00:42 -08:00
Attributes:
LATEST_YOUTUBE_DL (string): URL with the latest youtube-dl binary.
DOWNLOAD_TIMEOUT (int): Download timeout in seconds.
2014-12-24 02:00:42 -08:00
Args:
download_path (string): Absolute path where UpdateThread will download
the latest youtube-dl.
2014-07-20 08:47:07 -07:00
2014-12-24 02:00:42 -08:00
quiet (boolean): If True UpdateThread won't send the finish signal
back to the caller. Finish signal can be used to make sure that
the UpdateThread has been completed in an asynchronous way.
2014-12-24 02:00:42 -08:00
"""
2014-07-20 08:47:07 -07:00
LATEST_YOUTUBE_DL = 'https://yt-dl.org/latest/'
2017-12-10 05:01:45 -08:00
DOWNLOAD_TIMEOUT = 10
def __init__(self, download_path, quiet=False):
2014-04-30 03:38:56 -07:00
super(UpdateThread, self).__init__()
2014-12-06 08:02:45 -08:00
self.download_path = download_path
2014-07-20 08:47:07 -07:00
self.quiet = quiet
2014-04-30 03:38:56 -07:00
self.start()
def run(self):
self._talk_to_gui('download')
2014-12-06 08:02:45 -08:00
source_file = self.LATEST_YOUTUBE_DL + YOUTUBEDL_BIN
destination_file = os.path.join(self.download_path, YOUTUBEDL_BIN)
check_path(self.download_path)
2014-04-30 03:38:56 -07:00
try:
2014-07-20 08:47:07 -07:00
stream = urlopen(source_file, timeout=self.DOWNLOAD_TIMEOUT)
2014-07-20 08:47:07 -07:00
with open(destination_file, 'wb') as dest_file:
dest_file.write(stream.read())
self._talk_to_gui('correct')
except (HTTPError, URLError, IOError) as error:
self._talk_to_gui('error', unicode(error))
2014-12-24 02:00:42 -08:00
if not self.quiet:
self._talk_to_gui('finish')
def _talk_to_gui(self, signal, data=None):
"""Communicate with the GUI using wxCallAfter and wxPublisher.
2014-12-24 02:00:42 -08:00
Args:
signal (string): Unique signal string that informs the GUI for the
update process.
data (string): Can be any string data to pass along with the
given signal. Default is None.
Note:
UpdateThread supports 4 signals.
1) download: The update process started
2) correct: The update process completed successfully
3) error: An error occured while downloading youtube-dl binary
4) finish: The update thread is ready to join
2014-12-24 02:00:42 -08:00
"""
2015-01-24 05:34:02 -08:00
CallAfter(Publisher.sendMessage, UPDATE_PUB_TOPIC, (signal, data))