Browse Source

rename noapi to yt_noapi, add youtubedl_wrapper module to wrap calls to youtube-dl

Helmut Pozimski 6 years ago
parent
commit
40cd996f3b
3 changed files with 111 additions and 40 deletions
  1. 4 7
      lib_stov/subscription.py
  2. 92 0
      lib_stov/youtubedl_wrapper.py
  3. 15 33
      lib_stov/yt_noapi.py

+ 4 - 7
lib_stov/subscription.py

@@ -19,7 +19,7 @@
 """This module takes care of managing subscriptions."""
 
 from lib_stov import stov_exceptions
-from lib_stov import noapi
+from lib_stov import yt_noapi
 
 
 class Sub(object):
@@ -48,8 +48,8 @@ class Sub(object):
         elif int(disabled) == 1:
             self.disabled = True
 
-        self._connector = noapi.Connector(self._type, self._name,
-                                          self._conf, self._search)
+        self._connector = yt_noapi.Connector(self._type, self._name,
+                                             self._conf, self._search)
 
     def get_title(self):
         """Returns the title attribute."""
@@ -69,10 +69,7 @@ class Sub(object):
         if not self.disabled:
             if self._search != "" and self._conf.values["check_title"]\
                     == "yes":
-                if self._search in video.title:
-                    return True
-                else:
-                    return False
+                return self._search in video.title
             else:
                 return True
         else:

+ 92 - 0
lib_stov/youtubedl_wrapper.py

@@ -0,0 +1,92 @@
+#
+#        This file is part of stov, written by Helmut Pozimski 2012-2017.
+#
+#       stov is free software: you can redistribute it and/or modify
+#       it under the terms of the GNU General Public License as published by
+#       the Free Software Foundation, version 2 of the License.
+#
+#       stov is distributed in the hope that it will be useful,
+#       but WITHOUT ANY WARRANTY; without even the implied warranty of
+#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#       GNU General Public License for more details.
+#
+#       You should have received a copy of the GNU General Public License
+#       along with stov.  If not, see <http://www.gnu.org/licenses/>.
+
+
+# -*- coding: utf8 -*-
+
+""" Provides a wrapper around youtube-dl"""
+
+import subprocess
+import sys
+import logging
+
+LOGGER = logging.getLogger("stov")
+
+
+def get_ids(conf, url, title=""):
+    """
+    Retrieves the IDs
+    :param conf: configuration object
+    :type conf: lib_stov.configuration.Conf
+    :param url: URL to pass to youtube-dl
+    :type url: str
+    :param title: optional title to match
+    :type title: str
+    :return: video IDs
+    :rtype: list
+    """
+    videos_list = []
+    if conf.outputlevel == "verbose":
+        stderr = sys.stderr
+    else:
+        stderr = open("/dev/null", "w")
+    if title:
+        LOGGER.debug("Executing command: %s %s %s %s %s %s %s",
+                     conf.values["youtube-dl"], "--max-downloads",
+                     conf.values["maxvideos"], "--match-title",
+                     title, "--get-id", url)
+        try:
+            video_ids = subprocess.check_output(
+                [conf.values["youtube-dl"], "--max-downloads",
+                 conf.values["maxvideos"], "--match-title",
+                 title, "--get-id", url], stderr=stderr)
+        except subprocess.CalledProcessError as error_message:
+            video_ids = error_message.output
+    else:
+        LOGGER.debug("Executing command: %s %s %s %s %s",
+                     conf.values["youtube-dl"], "--max-downloads",
+                     conf.values["maxvideos"], "--get-id", url)
+        try:
+            video_ids = subprocess.check_output(
+                [conf.values["youtube-dl"], "--max-downloads",
+                 conf.values["maxvideos"], "--get-id", url], stderr=stderr)
+        except subprocess.CalledProcessError as error_message:
+            video_ids = error_message.output
+    video_ids = video_ids.decode(sys.stdout.encoding).strip()
+    for video in video_ids.split("\n"):
+        videos_list.append(video)
+    LOGGER.debug("generated list: %s", videos_list)
+    return videos_list
+
+
+def get_title(conf, url):
+    """
+    Retrieves the title of a specified video
+
+    :param conf: configuration object
+    :type conf: lib_stov.configuration.Conf
+    :param url: URL to pass to youtube-dl
+    :type url: str
+    """
+    if conf.outputlevel == "verbose":
+        stderr = sys.stderr
+    else:
+        stderr = open("/dev/null", "w")
+    LOGGER.debug("Executing command: %s %s %s",
+                 conf.values["youtube-dl"], "--get-title", url)
+    video_title = subprocess.check_output([
+        conf.values["youtube-dl"], "--get-title", url], stderr=stderr)
+    video_title = video_title.decode(sys.stdout.encoding)
+    return video_title

+ 15 - 33
lib_stov/noapi.py → lib_stov/yt_noapi.py

@@ -1,5 +1,5 @@
 #
-#        This file is part of stov, written by Helmut Pozimski 2012-2015.
+#        This file is part of stov, written by Helmut Pozimski 2012-2017.
 #
 #       stov is free software: you can redistribute it and/or modify
 #       it under the terms of the GNU General Public License as published by
@@ -20,13 +20,16 @@
 youtubeAPI module earlier."""
 
 import subprocess
-import sys
 import urllib.parse
 import urllib.request
 import urllib.error
+import logging
 
 import lxml.html
 from lib_stov import stov_exceptions
+from lib_stov import youtubedl_wrapper
+
+LOGGER = logging.getLogger("stov")
 
 
 class YtChannel(object):
@@ -112,33 +115,14 @@ class Connector(object):
     def _fetch_videos(self, existing_videos):
         """Retrieves all the relevant videos in a subscription."""
         videos_list = []
-        if self._conf.outputlevel == "verbose":
-            stderr = sys.stderr
-        else:
-            stderr = open("/dev/null", "w")
-        if self._type == "user" and self._search != "":
-            try:
-                video_ids = subprocess.check_output([
-                    self._conf.values["youtube-dl"],
-                    "--max-downloads",
-                    self._conf.values["maxvideos"],
-                    "--match-title",
-                    self._search,
-                    "--get-id",
-                    self._url], stderr=stderr)
-            except subprocess.CalledProcessError as error_message:
-                video_ids = error_message.output
+        if self._type == "user" and self._search:
+            video_ids = youtubedl_wrapper.get_ids(self._conf, self._url,
+                                                  self._search)
         else:
-            try:
-                video_ids = subprocess.check_output([
-                    self._conf.values["youtube-dl"], "--max-downloads",
-                    self._conf.values["maxvideos"], "--get-id",
-                    self._url], stderr=stderr).strip()
-            except subprocess.CalledProcessError as error_message:
-                video_ids = error_message.output
-        video_ids = video_ids.decode(sys.stdout.encoding).strip()
-        if len(video_ids) >= 1:
-            for video_id in video_ids.split("\n"):
+            video_ids = youtubedl_wrapper.get_ids(self._conf, self._url)
+        LOGGER.debug("Got video IDs: %s", video_ids)
+        if video_ids:
+            for video_id in video_ids:
                 video_exists = False
                 if existing_videos:
                     for existing_video in existing_videos:
@@ -147,14 +131,12 @@ class Connector(object):
                             break
                 if not video_exists:
                     try:
-                        video_title = subprocess.check_output([
-                            self._conf.values["youtube-dl"], "--get-title",
-                            "https://www.youtube.com/watch?v=%s"
-                            % video_id], stderr=stderr)
+                        video_title = youtubedl_wrapper.get_title(
+                            self._conf, "https://www.youtube.com/watch?v=%s"
+                                        % video_id)
                     except subprocess.CalledProcessError:
                         raise stov_exceptions.YoutubeDlCallFailed()
                     else:
-                        video_title = video_title.decode(sys.stdout.encoding)
                         videos_list.append(YtVideo(
                             video_title,
                             video_id))