twitch.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2017.
  3. #
  4. # stov is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 2 of the License.
  7. #
  8. # stov is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  15. # -*- coding: utf8 -*-
  16. """ This module implements support for subscriptions from twitch.tv"""
  17. import urllib.parse
  18. import urllib.request
  19. import urllib.error
  20. import logging
  21. from lib_stov import stov_exceptions
  22. from lib_stov import yt_noapi
  23. LOGGER = logging.getLogger("stov")
  24. class Connector(yt_noapi.Connector):
  25. """ Connector class, performing calls to youtube-dl to retrieve
  26. information about videos from twitch.tv
  27. """
  28. def __init__(self, subscription_type, name, conf, search=""):
  29. """Populates the object with all necessary data."""
  30. yt_noapi.Connector.__init__(self, subscription_type, name,
  31. conf, search)
  32. def _construct_url(self):
  33. """ Constructs the URL for the subscription to retrieve
  34. data from.
  35. """
  36. if self._type == "user":
  37. self._url = "https://www.twitch.tv/%s/videos/all" \
  38. % urllib.parse.quote(self._name)
  39. elif self._type == "search":
  40. raise stov_exceptions.TypeNotSupported()
  41. elif self._type == "playlist":
  42. raise stov_exceptions.TypeNotSupported()
  43. LOGGER.debug(_("Constructed URL for subscription: %s"), self._url)
  44. def _fetch_title(self):
  45. """Sets the title of a subscription"""
  46. self._title = self._name
  47. if self._search != "":
  48. self._title += _(" search %s") % self._search
  49. @staticmethod
  50. def construct_video_url(ytid):
  51. """
  52. Resturns the URL to a specified twitch video
  53. :param ytid: Twitch ID of the video
  54. :type ytid: str
  55. :return: Video URL
  56. :rtype: str
  57. """
  58. ytid = ytid.split("v")[1]
  59. url = "https://www.twitch.tv/videos/%s" % ytid
  60. return url
  61. @staticmethod
  62. def get_quality_parameter(config):
  63. """Determines which itag value results from codec and resolution
  64. settings and returns it
  65. :param config: configuration object
  66. :type config: lib_stov.configuration.Conf
  67. :return: itag value
  68. :rtype: str
  69. """
  70. LOGGER.debug(_("Trying to determine the itag value for youtube-dl from"
  71. " your quality and codec settings."))
  72. quality_value = 0
  73. if config.values["videocodec"] == "mp4":
  74. if config.values["maxresolution"] == "360p":
  75. quality_value = "360p"
  76. elif config.values["maxresolution"] == "480p":
  77. quality_value = "480p"
  78. elif config.values["maxresolution"] == "720p":
  79. quality_value = "720p"
  80. elif config.values["maxresolution"] == "1080p":
  81. quality_value = "1080p"
  82. if quality_value:
  83. LOGGER.debug(_("Found value: %s."), quality_value)
  84. return quality_value + "/" + config.values["videocodec"]
  85. else:
  86. LOGGER.debug(_("Could not determine an itag value "
  87. "from the configuration"))
  88. return "1080p" + "/" + config.values["videocodec"]