Browse Source

ignore errors in youtube-dl while gettings ids and reverse the playlist order to get the most recent videos of a youtube playlist

Helmut Pozimski 6 years ago
parent
commit
c230e3dbe3
2 changed files with 20 additions and 7 deletions
  1. 17 7
      lib_stov/youtubedl_wrapper.py
  2. 3 0
      lib_stov/yt_noapi.py

+ 17 - 7
lib_stov/youtubedl_wrapper.py

@@ -27,7 +27,7 @@ from lib_stov import stov_exceptions
 LOGGER = logging.getLogger("stov")
 
 
-def get_ids(conf, url, title=""):
+def get_ids(conf, url, title="", reverse=False):
     """
     Retrieves the IDs
     :param conf: configuration object
@@ -36,6 +36,9 @@ def get_ids(conf, url, title=""):
     :type url: str
     :param title: optional title to match
     :type title: str
+    :param reverse: look up a playlist in reverse order to get \
+    the recent videos first
+    :type reverse: bool
     :return: video IDs
     :rtype: list
     """
@@ -53,17 +56,24 @@ def get_ids(conf, url, title=""):
             video_ids = subprocess.check_output(
                 [conf.values["youtube-dl"], "--max-downloads",
                  conf.values["maxvideos"], "--match-title",
-                 title, "--get-id", url], stderr=stderr)
+                 title, "-i", "--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"),
+        LOGGER.debug(_("Executing command: %s %s %s %s %s %s"),
                      conf.values["youtube-dl"], "--max-downloads",
-                     conf.values["maxvideos"], "--get-id", url)
+                     conf.values["maxvideos"], "-i", "--get-id", url)
         try:
-            video_ids = subprocess.check_output(
-                [conf.values["youtube-dl"], "--max-downloads",
-                 conf.values["maxvideos"], "--get-id", url], stderr=stderr)
+            if reverse:
+                video_ids = subprocess.check_output(
+                    [conf.values["youtube-dl"], "--max-downloads",
+                     conf.values["maxvideos"], "-i", "--playlist-reverse",
+                     "--get-id", url], stderr=stderr)
+            else:
+                video_ids = subprocess.check_output(
+                    [conf.values["youtube-dl"], "--max-downloads",
+                     conf.values["maxvideos"],
+                     "-i", "--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()

+ 3 - 0
lib_stov/yt_noapi.py

@@ -123,6 +123,9 @@ class Connector(object):
         if self._type == "user" and self._search:
             video_ids = youtubedl_wrapper.get_ids(self._conf, self._url,
                                                   self._search)
+        elif self._type == "playlist":
+            video_ids = youtubedl_wrapper.get_ids(self._conf, self._url,
+                                                  reverse=True)
         else:
             video_ids = youtubedl_wrapper.get_ids(self._conf, self._url)
         LOGGER.debug("Got video IDs: %s", video_ids)