123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #
- # 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 logging
- import subprocess
- import sys
- from lib_stov import configuration
- from lib_stov import stov_exceptions
- 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):
- """
- Downloads a video from a specified url using youtube-dl.
- :param url: URL to pass to youtube-dl
- :type url: str
- """
- conf = configuration.Conf.get_instance()
- youtube_dl_format = "%s[height=%s]+%s/%s+best" % \
- (conf.get_value("video_codec"),
- conf.get_value("video_height"),
- conf.get_value("audio_quality"),
- conf.get_value("video_codec"))
- LOGGER.debug(_("Executing command: %s -f %s %s"),
- conf.values["youtube-dl"], youtube_dl_format, url)
- stderr = open("/dev/null", "w")
- stdout = sys.stdout
- if conf.outputlevel == "verbose":
- stderr = sys.stderr
- elif conf.outputlevel == "quiet":
- stdout = open("/dev/null", "w")
- stderr = open("/dev/null", "w")
- try:
- subprocess.check_call([conf.values["youtube-dl"], "-f %s"
- % youtube_dl_format,
- "-o", "%(title)s-%(id)s.%(ext)s", url],
- stderr=stderr,
- stdout=stdout)
- except subprocess.CalledProcessError as error:
- LOGGER.debug(_("Error while calling youtube-dl: %s"), error.output)
- raise stov_exceptions.YoutubeDlCallFailed()
|