youtubedl_wrapper.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2017.
  3. #
  4. # stov is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 2 of the License.
  7. #
  8. # stov is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  15. # -*- coding: utf8 -*-
  16. """ Provides a wrapper around youtube-dl"""
  17. import subprocess
  18. import sys
  19. import logging
  20. from lib_stov import stov_exceptions
  21. LOGGER = logging.getLogger("stov")
  22. def get_ids(conf, url, title="", reverse=False):
  23. """
  24. Retrieves the IDs
  25. :param conf: configuration object
  26. :type conf: lib_stov.configuration.Conf
  27. :param url: URL to pass to youtube-dl
  28. :type url: str
  29. :param title: optional title to match
  30. :type title: str
  31. :param reverse: look up a playlist in reverse order to get \
  32. the recent videos first
  33. :type reverse: bool
  34. :return: video IDs
  35. :rtype: list
  36. """
  37. videos_list = []
  38. if conf.outputlevel == "verbose":
  39. stderr = sys.stderr
  40. else:
  41. stderr = open("/dev/null", "w")
  42. if title:
  43. LOGGER.debug(_("Executing command: %s %s %s %s %s %s %s"),
  44. conf.values["youtube-dl"], "--max-downloads",
  45. conf.values["maxvideos"], "--match-title",
  46. title, "--get-id", url)
  47. try:
  48. video_ids = subprocess.check_output(
  49. [conf.values["youtube-dl"], "--max-downloads",
  50. conf.values["maxvideos"], "--match-title",
  51. title, "-i", "--get-id", url], stderr=stderr)
  52. except subprocess.CalledProcessError as error_message:
  53. video_ids = error_message.output
  54. else:
  55. LOGGER.debug(_("Executing command: %s %s %s %s %s %s"),
  56. conf.values["youtube-dl"], "--max-downloads",
  57. conf.values["maxvideos"], "-i", "--get-id", url)
  58. try:
  59. if reverse:
  60. video_ids = subprocess.check_output(
  61. [conf.values["youtube-dl"], "--max-downloads",
  62. conf.values["maxvideos"], "-i", "--playlist-reverse",
  63. "--get-id", url], stderr=stderr)
  64. else:
  65. video_ids = subprocess.check_output(
  66. [conf.values["youtube-dl"], "--max-downloads",
  67. conf.values["maxvideos"],
  68. "-i", "--get-id", url], stderr=stderr)
  69. except subprocess.CalledProcessError as error_message:
  70. video_ids = error_message.output
  71. video_ids = video_ids.decode(sys.stdout.encoding).strip()
  72. for video in video_ids.split("\n"):
  73. videos_list.append(video)
  74. LOGGER.debug("generated list: %s", videos_list)
  75. return videos_list
  76. def get_title(conf, url):
  77. """
  78. Retrieves the title of a specified video
  79. :param conf: configuration object
  80. :type conf: lib_stov.configuration.Conf
  81. :param url: URL to pass to youtube-dl
  82. :type url: str
  83. """
  84. if conf.outputlevel == "verbose":
  85. stderr = sys.stderr
  86. else:
  87. stderr = open("/dev/null", "w")
  88. LOGGER.debug(_("Executing command: %s %s %s"),
  89. conf.values["youtube-dl"], "--get-title", url)
  90. video_title = subprocess.check_output([
  91. conf.values["youtube-dl"], "--get-title", url], stderr=stderr)
  92. video_title = video_title.decode(sys.stdout.encoding)
  93. return video_title
  94. def download_video(conf, url, itag_value):
  95. """
  96. Downloads a video from a specified url using youtube-dl.
  97. :param conf: configuration object
  98. :type conf: lib_stov.configuration.Conf
  99. :param itag_value: video and audio parameter
  100. :type itag_value: str
  101. :param url: URL to pass to youtube-dl
  102. :type url: str
  103. """
  104. try:
  105. LOGGER.debug(_("Executing command: %s -f %s %s"),
  106. conf.values["youtube-dl"], itag_value, url)
  107. if conf.outputlevel == "default":
  108. subprocess.check_call([conf.values["youtube-dl"], "-f %s"
  109. % itag_value,
  110. "-o", "%(title)s-%(id)s.%(ext)s", url],
  111. stderr=sys.stderr,
  112. stdout=open("/dev/null", "w"))
  113. elif conf.outputlevel == "verbose":
  114. subprocess.check_call([conf.values["youtube-dl"], "-f %s"
  115. % itag_value,
  116. "-o", "%(title)s-%(id)s.%(ext)s", url],
  117. stderr=sys.stderr, stdout=sys.stdout)
  118. elif conf.outputlevel == "quiet":
  119. subprocess.check_call([conf.values["youtube-dl"], "-f %s/%s"
  120. % itag_value,
  121. "-o", "%(title)s-%(id)s.%(ext)s", url],
  122. stderr=open("/dev/null", "w"),
  123. stdout=open("/dev/null", "w"))
  124. except subprocess.CalledProcessError as error:
  125. LOGGER.debug(_("Error while calling youtube-dl: %s"), error.output)
  126. raise stov_exceptions.YoutubeDlCallFailed()