# # 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 -*- """ This module implements support for subscriptions from twitch.tv""" import logging import urllib.error import urllib.parse import urllib.request from lib_stov import stov_exceptions from lib_stov import yt_noapi LOGGER = logging.getLogger("stov") class Connector(yt_noapi.Connector): """ Connector class, performing calls to youtube-dl to retrieve information about videos from twitch.tv """ def __init__(self, subscription_type, name, search=""): """Populates the object with all necessary data.""" yt_noapi.Connector.__init__(self, subscription_type, name, search) def _construct_url(self): """ Constructs the URL for the subscription to retrieve data from. """ if self._type == "user": self._url = "https://www.twitch.tv/%s/videos/all" \ % urllib.parse.quote(self._name) elif self._type == "search": raise stov_exceptions.TypeNotSupported() elif self._type == "playlist": raise stov_exceptions.TypeNotSupported() LOGGER.debug(_("Constructed URL for subscription: %s"), self._url) def _fetch_title(self): """Sets the title of a subscription""" self._title = self._name if self._search != "": self._title += _(" search %s") % self._search @staticmethod def construct_video_url(ytid): """ Resturns the URL to a specified twitch video :param ytid: Twitch ID of the video :type ytid: str :return: Video URL :rtype: str """ ytid = ytid.split("v")[1] url = "https://www.twitch.tv/videos/%s" % ytid return url @staticmethod def get_quality_parameter(config): """Determines which itag value results from codec and resolution settings and returns it :param config: configuration object :type config: lib_stov.configuration.Conf :return: itag value :rtype: str """ LOGGER.debug(_("Trying to determine the itag value for youtube-dl from" " your quality and codec settings.")) quality_value = 0 if config.values["videocodec"] == "mp4": if config.values["maxresolution"] == "360p": quality_value = "360p" elif config.values["maxresolution"] == "480p": quality_value = "480p" elif config.values["maxresolution"] == "720p": quality_value = "720p" elif config.values["maxresolution"] == "1080p": quality_value = "1080p" if quality_value: LOGGER.debug(_("Found value: %s."), quality_value) return quality_value + "/" + config.values["videocodec"] LOGGER.debug(_("Could not determine an itag value " "from the configuration")) return "1080p" + "/" + config.values["videocodec"]