# # 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 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