youtubedl_wrapper.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. LOGGER = logging.getLogger("stov")
  21. def get_ids(conf, url, title=""):
  22. """
  23. Retrieves the IDs
  24. :param conf: configuration object
  25. :type conf: lib_stov.configuration.Conf
  26. :param url: URL to pass to youtube-dl
  27. :type url: str
  28. :param title: optional title to match
  29. :type title: str
  30. :return: video IDs
  31. :rtype: list
  32. """
  33. videos_list = []
  34. if conf.outputlevel == "verbose":
  35. stderr = sys.stderr
  36. else:
  37. stderr = open("/dev/null", "w")
  38. if title:
  39. LOGGER.debug("Executing command: %s %s %s %s %s %s %s",
  40. conf.values["youtube-dl"], "--max-downloads",
  41. conf.values["maxvideos"], "--match-title",
  42. title, "--get-id", url)
  43. try:
  44. video_ids = subprocess.check_output(
  45. [conf.values["youtube-dl"], "--max-downloads",
  46. conf.values["maxvideos"], "--match-title",
  47. title, "--get-id", url], stderr=stderr)
  48. except subprocess.CalledProcessError as error_message:
  49. video_ids = error_message.output
  50. else:
  51. LOGGER.debug("Executing command: %s %s %s %s %s",
  52. conf.values["youtube-dl"], "--max-downloads",
  53. conf.values["maxvideos"], "--get-id", url)
  54. try:
  55. video_ids = subprocess.check_output(
  56. [conf.values["youtube-dl"], "--max-downloads",
  57. conf.values["maxvideos"], "--get-id", url], stderr=stderr)
  58. except subprocess.CalledProcessError as error_message:
  59. video_ids = error_message.output
  60. video_ids = video_ids.decode(sys.stdout.encoding).strip()
  61. for video in video_ids.split("\n"):
  62. videos_list.append(video)
  63. LOGGER.debug("generated list: %s", videos_list)
  64. return videos_list
  65. def get_title(conf, url):
  66. """
  67. Retrieves the title of a specified video
  68. :param conf: configuration object
  69. :type conf: lib_stov.configuration.Conf
  70. :param url: URL to pass to youtube-dl
  71. :type url: str
  72. """
  73. if conf.outputlevel == "verbose":
  74. stderr = sys.stderr
  75. else:
  76. stderr = open("/dev/null", "w")
  77. LOGGER.debug("Executing command: %s %s %s",
  78. conf.values["youtube-dl"], "--get-title", url)
  79. video_title = subprocess.check_output([
  80. conf.values["youtube-dl"], "--get-title", url], stderr=stderr)
  81. video_title = video_title.decode(sys.stdout.encoding)
  82. return video_title