youtubedl_wrapper.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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=""):
  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. :return: video IDs
  32. :rtype: list
  33. """
  34. videos_list = []
  35. if conf.outputlevel == "verbose":
  36. stderr = sys.stderr
  37. else:
  38. stderr = open("/dev/null", "w")
  39. if title:
  40. LOGGER.debug(_("Executing command: %s %s %s %s %s %s %s"),
  41. conf.values["youtube-dl"], "--max-downloads",
  42. conf.values["maxvideos"], "--match-title",
  43. title, "--get-id", url)
  44. try:
  45. video_ids = subprocess.check_output(
  46. [conf.values["youtube-dl"], "--max-downloads",
  47. conf.values["maxvideos"], "--match-title",
  48. title, "--get-id", url], stderr=stderr)
  49. except subprocess.CalledProcessError as error_message:
  50. video_ids = error_message.output
  51. else:
  52. LOGGER.debug(_("Executing command: %s %s %s %s %s"),
  53. conf.values["youtube-dl"], "--max-downloads",
  54. conf.values["maxvideos"], "--get-id", url)
  55. try:
  56. video_ids = subprocess.check_output(
  57. [conf.values["youtube-dl"], "--max-downloads",
  58. conf.values["maxvideos"], "--get-id", url], stderr=stderr)
  59. except subprocess.CalledProcessError as error_message:
  60. video_ids = error_message.output
  61. video_ids = video_ids.decode(sys.stdout.encoding).strip()
  62. for video in video_ids.split("\n"):
  63. videos_list.append(video)
  64. LOGGER.debug("generated list: %s", videos_list)
  65. return videos_list
  66. def get_title(conf, url):
  67. """
  68. Retrieves the title of a specified video
  69. :param conf: configuration object
  70. :type conf: lib_stov.configuration.Conf
  71. :param url: URL to pass to youtube-dl
  72. :type url: str
  73. """
  74. if conf.outputlevel == "verbose":
  75. stderr = sys.stderr
  76. else:
  77. stderr = open("/dev/null", "w")
  78. LOGGER.debug(_("Executing command: %s %s %s"),
  79. conf.values["youtube-dl"], "--get-title", url)
  80. video_title = subprocess.check_output([
  81. conf.values["youtube-dl"], "--get-title", url], stderr=stderr)
  82. video_title = video_title.decode(sys.stdout.encoding)
  83. return video_title
  84. def download_video(conf, url, itag_value, video_codec):
  85. """
  86. Downloads a video from a specified url using youtube-dl.
  87. :param conf: configuration object
  88. :type conf: lib_stov.configuration.Conf
  89. :param url: URL to pass to youtube-dl
  90. :type url: str
  91. """
  92. try:
  93. LOGGER.debug(_("Executing command: %s -f %s/%s %s"),
  94. conf.values["youtube-dl"], itag_value, video_codec, url)
  95. if conf.outputlevel == "default":
  96. subprocess.check_call([conf.values["youtube-dl"], "-f %s/%s"
  97. % (itag_value, video_codec),
  98. "-o", "%(title)s-%(id)s.%(ext)s", url],
  99. stderr=sys.stderr,
  100. stdout=open("/dev/null", "w"))
  101. elif conf.outputlevel == "verbose":
  102. subprocess.check_call([conf.values["youtube-dl"], "-f %s/%s"
  103. % (itag_value, video_codec),
  104. "-o", "%(title)s-%(id)s.%(ext)s", url],
  105. stderr=sys.stderr, stdout=sys.stdout)
  106. elif conf.outputlevel == "quiet":
  107. subprocess.check_call([conf.values["youtube-dl"], "-f %s/%s"
  108. % (itag_value, video_codec),
  109. "-o", "%(title)s-%(id)s.%(ext)s", url],
  110. stderr=open("/dev/null", "w"),
  111. stdout=open("/dev/null", "w"))
  112. except subprocess.CalledProcessError as error:
  113. LOGGER.debug(_("Error while calling youtube-dl: %s"), error.output)
  114. raise stov_exceptions.YoutubeDlCallFailed()