2014-05-22 08:43:36 -07:00
|
|
|
#!/usr/bin/env python2
|
2015-03-04 13:42:07 -08:00
|
|
|
# -*- 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.
|
|
|
|
|
|
|
|
"""
|
Version 0.3.3
Refactor code
Fix DASH to Audio bug
When "Clear DASH audio/video files"
option was enable. Youtube-dlG would
also remove audio output files
*.mp3, *.wav, etc..
SignalHandler.py
Add DataPack class
Add OutputHandler class
DownloadThread.py
Now OutputHandler handles the stdout
Reduce user input errors:
OptionsHandler.py
Change to int
--> startTrack, endTrack, maxDownloads, retries
YoutubeDLInterpreter.py
Remove empty string check
--> startTrack, endTrack, maxDownloads, retries
YoutubeDLGUI.py
Change wx.TextCtrl to wx.SpinCtrl
--> startTrack, endTrack, maxDownloads, retries
Add check_input function for
--> maxFileSize, minFileSize
2014-04-02 11:29:50 -07:00
|
|
|
|
2015-03-04 13:42:07 -08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-12-06 08:02:45 -08:00
|
|
|
import os.path
|
Version 0.3.3
Refactor code
Fix DASH to Audio bug
When "Clear DASH audio/video files"
option was enable. Youtube-dlG would
also remove audio output files
*.mp3, *.wav, etc..
SignalHandler.py
Add DataPack class
Add OutputHandler class
DownloadThread.py
Now OutputHandler handles the stdout
Reduce user input errors:
OptionsHandler.py
Change to int
--> startTrack, endTrack, maxDownloads, retries
YoutubeDLInterpreter.py
Remove empty string check
--> startTrack, endTrack, maxDownloads, retries
YoutubeDLGUI.py
Change wx.TextCtrl to wx.SpinCtrl
--> startTrack, endTrack, maxDownloads, retries
Add check_input function for
--> maxFileSize, minFileSize
2014-04-02 11:29:50 -07:00
|
|
|
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.
|
2015-01-10 08:34:22 -08:00
|
|
|
|
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
|
2015-01-16 09:27:57 -08:00
|
|
|
the UpdateThread has been completed in an asynchronous way.
|
2015-01-10 08:34:22 -08:00
|
|
|
|
2014-12-24 02:00:42 -08:00
|
|
|
"""
|
2014-07-20 08:47:07 -07:00
|
|
|
|
2014-05-11 08:38:59 -07:00
|
|
|
LATEST_YOUTUBE_DL = 'https://yt-dl.org/latest/'
|
2017-12-10 05:01:45 -08:00
|
|
|
DOWNLOAD_TIMEOUT = 10
|
2014-05-11 08:38:59 -07:00
|
|
|
|
2014-12-21 10:15:26 -08:00
|
|
|
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):
|
2015-01-16 09:27:57 -08:00
|
|
|
self._talk_to_gui('download')
|
2015-03-04 13:42:07 -08:00
|
|
|
|
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)
|
2014-05-11 08:38:59 -07:00
|
|
|
|
|
|
|
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-05-11 08:38:59 -07:00
|
|
|
|
2014-07-20 08:47:07 -07:00
|
|
|
with open(destination_file, 'wb') as dest_file:
|
|
|
|
dest_file.write(stream.read())
|
2014-05-11 08:38:59 -07:00
|
|
|
|
2015-01-16 09:27:57 -08:00
|
|
|
self._talk_to_gui('correct')
|
2015-01-10 08:34:22 -08:00
|
|
|
except (HTTPError, URLError, IOError) as error:
|
2015-03-04 13:42:07 -08:00
|
|
|
self._talk_to_gui('error', unicode(error))
|
2014-05-11 08:38:59 -07:00
|
|
|
|
2014-12-24 02:00:42 -08:00
|
|
|
if not self.quiet:
|
|
|
|
self._talk_to_gui('finish')
|
|
|
|
|
2015-01-16 09:27:57 -08:00
|
|
|
def _talk_to_gui(self, signal, data=None):
|
|
|
|
"""Communicate with the GUI using wxCallAfter and wxPublisher.
|
2015-01-10 08:34:22 -08:00
|
|
|
|
2014-12-24 02:00:42 -08:00
|
|
|
Args:
|
2015-01-16 09:27:57 -08:00
|
|
|
signal (string): Unique signal string that informs the GUI for the
|
|
|
|
update process.
|
2015-03-04 13:42:07 -08:00
|
|
|
|
2015-01-16 09:27:57 -08:00
|
|
|
data (string): Can be any string data to pass along with the
|
|
|
|
given signal. Default is None.
|
2015-03-04 13:42:07 -08:00
|
|
|
|
2015-01-16 09:27:57 -08:00
|
|
|
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
|
2015-01-10 08:34:22 -08:00
|
|
|
|
2014-12-24 02:00:42 -08:00
|
|
|
"""
|
2015-01-24 05:34:02 -08:00
|
|
|
CallAfter(Publisher.sendMessage, UPDATE_PUB_TOPIC, (signal, data))
|