# # This file is part of stov, written by Helmut Pozimski 2012-2017. # # stov is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 2 of the License. # # stov is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with stov. If not, see . # -*- coding: utf8 -*- """ Provides a wrapper around youtube-dl""" import subprocess import sys import logging from lib_stov import stov_exceptions LOGGER = logging.getLogger("stov") def get_ids(conf, url, title=""): """ Retrieves the IDs :param conf: configuration object :type conf: lib_stov.configuration.Conf :param url: URL to pass to youtube-dl :type url: str :param title: optional title to match :type title: str :return: video IDs :rtype: list """ videos_list = [] if conf.outputlevel == "verbose": stderr = sys.stderr else: stderr = open("/dev/null", "w") if title: LOGGER.debug(_("Executing command: %s %s %s %s %s %s %s"), conf.values["youtube-dl"], "--max-downloads", conf.values["maxvideos"], "--match-title", title, "--get-id", url) try: video_ids = subprocess.check_output( [conf.values["youtube-dl"], "--max-downloads", conf.values["maxvideos"], "--match-title", title, "--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"), conf.values["youtube-dl"], "--max-downloads", conf.values["maxvideos"], "--get-id", url) try: video_ids = subprocess.check_output( [conf.values["youtube-dl"], "--max-downloads", conf.values["maxvideos"], "--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() for video in video_ids.split("\n"): videos_list.append(video) LOGGER.debug("generated list: %s", videos_list) return videos_list def get_title(conf, url): """ Retrieves the title of a specified video :param conf: configuration object :type conf: lib_stov.configuration.Conf :param url: URL to pass to youtube-dl :type url: str """ if conf.outputlevel == "verbose": stderr = sys.stderr else: stderr = open("/dev/null", "w") LOGGER.debug(_("Executing command: %s %s %s"), conf.values["youtube-dl"], "--get-title", url) video_title = subprocess.check_output([ conf.values["youtube-dl"], "--get-title", url], stderr=stderr) video_title = video_title.decode(sys.stdout.encoding) return video_title def download_video(conf, url, itag_value, video_codec): """ Downloads a video from a specified url using youtube-dl. :param conf: configuration object :type conf: lib_stov.configuration.Conf :param url: URL to pass to youtube-dl :type url: str """ try: LOGGER.debug(_("Executing command: %s -f %s/%s %s"), conf.values["youtube-dl"], itag_value, video_codec, url) if conf.outputlevel == "default": subprocess.check_call([conf.values["youtube-dl"], "-f %s/%s" % (itag_value, video_codec), "-o", "%(title)s-%(id)s.%(ext)s", url], stderr=sys.stderr, stdout=open("/dev/null", "w")) elif conf.outputlevel == "verbose": subprocess.check_call([conf.values["youtube-dl"], "-f %s/%s" % (itag_value, video_codec), "-o", "%(title)s-%(id)s.%(ext)s", url], stderr=sys.stderr, stdout=sys.stdout) elif conf.outputlevel == "quiet": subprocess.check_call([conf.values["youtube-dl"], "-f %s/%s" % (itag_value, video_codec), "-o", "%(title)s-%(id)s.%(ext)s", url], stderr=open("/dev/null", "w"), stdout=open("/dev/null", "w")) except subprocess.CalledProcessError as error: LOGGER.debug(_("Error while calling youtube-dl: %s"), error.output) raise stov_exceptions.YoutubeDlCallFailed()