Skip to content

Commit

Permalink
Find and use system youtube-dl binary
Browse files Browse the repository at this point in the history
Fallback to downloading it from the internet if a system one is not available
  • Loading branch information
mikhailnov committed Jul 12, 2019
1 parent c5c18e5 commit 9d7d88f
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 6 deletions.
130 changes: 130 additions & 0 deletions 0001-Find-and-use-system-youtube-dl-binary.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
From 8b8d7dd2f828b9c45192d1ce736c34020b9ca928 Mon Sep 17 00:00:00 2001
From: Mikhail Novosyolov <m.novosyolov@rosalinux.ru>
Date: Fri, 12 Jul 2019 14:51:05 +0300
Subject: [PATCH] Find and use system youtube-dl binary

Fallback to downloading it from the internet if a system one is not available
---
youtube_dl_gui/__init__.py | 10 ++++++++--
youtube_dl_gui/downloaders.py | 11 +++++++++--
youtube_dl_gui/downloadmanager.py | 8 ++++++--
youtube_dl_gui/utils.py | 6 ++++++
4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/youtube_dl_gui/__init__.py b/youtube_dl_gui/__init__.py
index d8c0fe2..4988922 100644
--- a/youtube_dl_gui/__init__.py
+++ b/youtube_dl_gui/__init__.py
@@ -51,7 +51,8 @@ from .utils import (
get_config_path,
get_locale_file,
os_path_exists,
- YOUTUBEDL_BIN
+ YOUTUBEDL_BIN,
+ system_youtube_dl,
)


@@ -81,7 +82,12 @@ from .mainframe import MainFrame

def main():
"""The real main. Creates and calls the main app windows. """
- youtubedl_path = os.path.join(opt_manager.options["youtubedl_path"], YOUTUBEDL_BIN)
+
+ if system_youtube_dl is not None:
+ youtubedl_path = system_youtube_dl
+ print("Found and using system youtube-dl")
+ else:
+ youtubedl_path = os.path.join(opt_manager.options["youtubedl_path"], YOUTUBEDL_BIN)

app = wx.App()
frame = MainFrame(opt_manager, log_manager)
diff --git a/youtube_dl_gui/downloaders.py b/youtube_dl_gui/downloaders.py
index 159ed6c..68e0363 100644
--- a/youtube_dl_gui/downloaders.py
+++ b/youtube_dl_gui/downloaders.py
@@ -21,7 +21,10 @@ from time import sleep
from Queue import Queue
from threading import Thread

-from .utils import convert_item
+from .utils import (
+ convert_item,
+ system_youtube_dl,
+)


class PipeReader(Thread):
@@ -314,7 +317,11 @@ class YoutubeDLDownloader(object):
if os.name == 'nt':
cmd = [self.youtubedl_path] + options + [url]
else:
- cmd = ['python', self.youtubedl_path] + options + [url]
+ if (system_youtube_dl is None):
+ cmd = ['python2', self.youtubedl_path] + options + [url]
+ else:
+ cmd = [system_youtube_dl] + options + [url]
+ #print("Executing cmd: ", cmd)

return cmd

diff --git a/youtube_dl_gui/downloadmanager.py b/youtube_dl_gui/downloadmanager.py
index a344829..0bfab71 100644
--- a/youtube_dl_gui/downloadmanager.py
+++ b/youtube_dl_gui/downloadmanager.py
@@ -40,6 +40,7 @@ from .downloaders import YoutubeDLDownloader

from .utils import (
YOUTUBEDL_BIN,
+ system_youtube_dl,
os_path_exists,
format_bytes,
to_string,
@@ -506,7 +507,7 @@ class DownloadManager(Thread):

def _check_youtubedl(self):
"""Check if youtube-dl binary exists. If not try to download it. """
- if not os_path_exists(self._youtubedl_path()) and self.parent.update_thread is None:
+ if (system_youtube_dl is None) and not os_path_exists(self._youtubedl_path()) and self.parent.update_thread is None:
self.parent.update_thread = UpdateThread(self.opt_manager.options['youtubedl_path'], True)
self.parent.update_thread.join()
self.parent.update_thread = None
@@ -529,7 +530,10 @@ class DownloadManager(Thread):
def _youtubedl_path(self):
"""Returns the path to youtube-dl binary. """
path = self.opt_manager.options['youtubedl_path']
- path = os.path.join(path, YOUTUBEDL_BIN)
+ if system_youtube_dl is not None:
+ path = system_youtube_dl
+ else:
+ path = os.path.join(self.opt_manager.options["youtubedl_path"], YOUTUBEDL_BIN)
return path


diff --git a/youtube_dl_gui/utils.py b/youtube_dl_gui/utils.py
index 729c007..a9a49be 100644
--- a/youtube_dl_gui/utils.py
+++ b/youtube_dl_gui/utils.py
@@ -19,6 +19,9 @@ import math
import locale
import subprocess

+import distutils
+from distutils.spawn import find_executable
+
try:
from twodict import TwoWayOrderedDict
except ImportError as error:
@@ -36,6 +39,9 @@ YOUTUBEDL_BIN = 'youtube-dl'
if os.name == 'nt':
YOUTUBEDL_BIN += '.exe'

+# Prefer system youtube-dl; if it does not exist, fallback to downloading
+# it in downloadmanager.py/_check_youtubedl
+system_youtube_dl = find_executable(YOUTUBEDL_BIN)

FILESIZE_METRICS = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]

--
2.20.1

10 changes: 8 additions & 2 deletions youtube_dl_gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
get_config_path,
get_locale_file,
os_path_exists,
YOUTUBEDL_BIN
YOUTUBEDL_BIN,
system_youtube_dl,
)


Expand Down Expand Up @@ -81,7 +82,12 @@

def main():
"""The real main. Creates and calls the main app windows. """
youtubedl_path = os.path.join(opt_manager.options["youtubedl_path"], YOUTUBEDL_BIN)

if system_youtube_dl is not None:
youtubedl_path = system_youtube_dl
print("Found and using system youtube-dl")
else:
youtubedl_path = os.path.join(opt_manager.options["youtubedl_path"], YOUTUBEDL_BIN)

app = wx.App()
frame = MainFrame(opt_manager, log_manager)
Expand Down
11 changes: 9 additions & 2 deletions youtube_dl_gui/downloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
from Queue import Queue
from threading import Thread

from .utils import convert_item
from .utils import (
convert_item,
system_youtube_dl,
)


class PipeReader(Thread):
Expand Down Expand Up @@ -314,7 +317,11 @@ def _get_cmd(self, url, options):
if os.name == 'nt':
cmd = [self.youtubedl_path] + options + [url]
else:
cmd = ['python', self.youtubedl_path] + options + [url]
if (system_youtube_dl is None):
cmd = ['python', self.youtubedl_path] + options + [url]
else:
cmd = [system_youtube_dl] + options + [url]
#print("Executing cmd: ", cmd)

return cmd

Expand Down
8 changes: 6 additions & 2 deletions youtube_dl_gui/downloadmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

from .utils import (
YOUTUBEDL_BIN,
system_youtube_dl,
os_path_exists,
format_bytes,
to_string,
Expand Down Expand Up @@ -506,7 +507,7 @@ def _talk_to_gui(self, data):

def _check_youtubedl(self):
"""Check if youtube-dl binary exists. If not try to download it. """
if not os_path_exists(self._youtubedl_path()) and self.parent.update_thread is None:
if (system_youtube_dl is None) and not os_path_exists(self._youtubedl_path()) and self.parent.update_thread is None:
self.parent.update_thread = UpdateThread(self.opt_manager.options['youtubedl_path'], True)
self.parent.update_thread.join()
self.parent.update_thread = None
Expand All @@ -529,7 +530,10 @@ def _jobs_done(self):
def _youtubedl_path(self):
"""Returns the path to youtube-dl binary. """
path = self.opt_manager.options['youtubedl_path']
path = os.path.join(path, YOUTUBEDL_BIN)
if system_youtube_dl is not None:
path = system_youtube_dl
else:
path = os.path.join(self.opt_manager.options["youtubedl_path"], YOUTUBEDL_BIN)
return path


Expand Down
6 changes: 6 additions & 0 deletions youtube_dl_gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import locale
import subprocess

import distutils
from distutils.spawn import find_executable

try:
from twodict import TwoWayOrderedDict
except ImportError as error:
Expand All @@ -36,6 +39,9 @@
if os.name == 'nt':
YOUTUBEDL_BIN += '.exe'

# Prefer system youtube-dl; if it does not exist, fallback to downloading
# it in downloadmanager.py/_check_youtubedl
system_youtube_dl = find_executable(YOUTUBEDL_BIN)

FILESIZE_METRICS = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]

Expand Down

0 comments on commit 9d7d88f

Please sign in to comment.