youtubedl_wrapper.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2018.
  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. from lib_stov import configuration
  22. LOGGER = logging.getLogger("stov")
  23. def get_ids(url, reverse=False):
  24. """
  25. Retrieves the IDs
  26. :param url: URL to pass to youtube-dl
  27. :type url: str
  28. :param reverse: look up a playlist in reverse order to get \
  29. the recent videos first
  30. :type reverse: bool
  31. :return: video IDs
  32. :rtype: list
  33. """
  34. conf = configuration.Conf.get_instance()
  35. videos_list = []
  36. if conf.outputlevel == "verbose":
  37. stderr = sys.stderr
  38. else:
  39. stderr = open("/dev/null", "w")
  40. LOGGER.debug(_("Executing command: %s %s %s %s %s %s"),
  41. conf.values["youtube-dl"], "--max-downloads",
  42. conf.values["maxvideos"], "-i", "--get-id", url)
  43. try:
  44. if reverse:
  45. video_ids = subprocess.check_output(
  46. [conf.values["youtube-dl"], "--max-downloads",
  47. conf.values["maxvideos"], "-i", "--playlist-reverse",
  48. "--get-id", url], stderr=stderr)
  49. else:
  50. video_ids = subprocess.check_output(
  51. [conf.values["youtube-dl"], "--max-downloads",
  52. conf.values["maxvideos"],
  53. "-i", "--get-id", url], stderr=stderr)
  54. except subprocess.CalledProcessError as error_message:
  55. video_ids = error_message.output
  56. video_ids = video_ids.decode(sys.stdout.encoding).strip()
  57. for video in video_ids.split("\n"):
  58. if video:
  59. videos_list.append(video)
  60. LOGGER.debug("generated list: %s", videos_list)
  61. return videos_list
  62. def get_title(url):
  63. """
  64. Retrieves the title of a specified video
  65. :param url: URL to pass to youtube-dl
  66. :type url: str
  67. """
  68. conf = configuration.Conf.get_instance()
  69. if conf.outputlevel == "verbose":
  70. stderr = sys.stderr
  71. else:
  72. stderr = open("/dev/null", "w")
  73. LOGGER.debug(_("Executing command: %s %s %s"),
  74. conf.values["youtube-dl"], "--get-title", url)
  75. video_title = subprocess.check_output([
  76. conf.values["youtube-dl"], "--get-title", url], stderr=stderr)
  77. video_title = video_title.decode(sys.stdout.encoding)
  78. return video_title
  79. def download_video(url, itag_value):
  80. """
  81. Downloads a video from a specified url using youtube-dl.
  82. :param itag_value: video and audio parameter
  83. :type itag_value: str
  84. :param url: URL to pass to youtube-dl
  85. :type url: str
  86. """
  87. conf = configuration.Conf.get_instance()
  88. try:
  89. LOGGER.debug(_("Executing command: %s -f %s %s"),
  90. conf.values["youtube-dl"], itag_value, url)
  91. if conf.outputlevel == "default":
  92. subprocess.check_call([conf.values["youtube-dl"], "-f %s"
  93. % itag_value,
  94. "-o", "%(title)s-%(id)s.%(ext)s", url],
  95. stderr=sys.stderr,
  96. stdout=open("/dev/null", "w"))
  97. elif conf.outputlevel == "verbose":
  98. subprocess.check_call([conf.values["youtube-dl"], "-f %s"
  99. % itag_value,
  100. "-o", "%(title)s-%(id)s.%(ext)s", url],
  101. stderr=sys.stderr, stdout=sys.stdout)
  102. elif conf.outputlevel == "quiet":
  103. subprocess.check_call([conf.values["youtube-dl"],
  104. "-f %s/%s" % itag_value,
  105. "-o", "%(title)s-%(id)s.%(ext)s", url],
  106. stderr=open("/dev/null", "w"),
  107. stdout=open("/dev/null", "w"))
  108. except subprocess.CalledProcessError as error:
  109. LOGGER.debug(_("Error while calling youtube-dl: %s"), error.output)
  110. raise stov_exceptions.YoutubeDlCallFailed()