123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #
- # This file is part of stov, written by Helmut Pozimski 2012-2018.
- #
- # 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 <http://www.gnu.org/licenses/>.
- # -*- coding: utf8 -*-
- """ Provides a wrapper around youtube-dl"""
- import subprocess
- import sys
- import logging
- from lib_stov import stov_exceptions
- from lib_stov import configuration
- LOGGER = logging.getLogger("stov")
- def get_ids(url, reverse=False):
- """
- Retrieves the IDs
- :param url: URL to pass to youtube-dl
- :type url: str
- :param reverse: look up a playlist in reverse order to get \
- the recent videos first
- :type reverse: bool
- :return: video IDs
- :rtype: list
- """
- conf = configuration.Conf.get_instance()
- videos_list = []
- if conf.outputlevel == "verbose":
- stderr = sys.stderr
- else:
- stderr = open("/dev/null", "w")
- LOGGER.debug(_("Executing command: %s %s %s %s %s %s"),
- conf.values["youtube-dl"], "--max-downloads",
- conf.values["maxvideos"], "-i", "--get-id", url)
- try:
- if reverse:
- video_ids = subprocess.check_output(
- [conf.values["youtube-dl"], "--max-downloads",
- conf.values["maxvideos"], "-i", "--playlist-reverse",
- "--get-id", url], stderr=stderr)
- else:
- video_ids = subprocess.check_output(
- [conf.values["youtube-dl"], "--max-downloads",
- conf.values["maxvideos"],
- "-i", "--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"):
- if video:
- videos_list.append(video)
- LOGGER.debug("generated list: %s", videos_list)
- return videos_list
- def get_title(url):
- """
- Retrieves the title of a specified video
- :param url: URL to pass to youtube-dl
- :type url: str
- """
- conf = configuration.Conf.get_instance()
- 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(url, itag_value):
- """
- Downloads a video from a specified url using youtube-dl.
- :param itag_value: video and audio parameter
- :type itag_value: str
- :param url: URL to pass to youtube-dl
- :type url: str
- """
- conf = configuration.Conf.get_instance()
- try:
- LOGGER.debug(_("Executing command: %s -f %s %s"),
- conf.values["youtube-dl"], itag_value, url)
- if conf.outputlevel == "default":
- subprocess.check_call([conf.values["youtube-dl"], "-f %s"
- % itag_value,
- "-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"
- % itag_value,
- "-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,
- "-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()
|