twitch.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 logging
  18. import urllib.error
  19. import urllib.parse
  20. import urllib.request
  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, search=""):
  29. """Populates the object with all necessary data."""
  30. yt_noapi.Connector.__init__(self, subscription_type, name, search)
  31. def _construct_url(self):
  32. """ Constructs the URL for the subscription to retrieve
  33. data from.
  34. """
  35. if self._type == "user":
  36. self._url = "https://www.twitch.tv/%s/videos/all" \
  37. % urllib.parse.quote(self._name)
  38. elif self._type == "search":
  39. raise stov_exceptions.TypeNotSupported()
  40. elif self._type == "playlist":
  41. raise stov_exceptions.TypeNotSupported()
  42. LOGGER.debug(_("Constructed URL for subscription: %s"), self._url)
  43. def _fetch_title(self):
  44. """Sets the title of a subscription"""
  45. self._title = self._name
  46. if self._search != "":
  47. self._title += _(" search %s") % self._search
  48. @staticmethod
  49. def construct_video_url(ytid):
  50. """
  51. Resturns the URL to a specified twitch video
  52. :param ytid: Twitch ID of the video
  53. :type ytid: str
  54. :return: Video URL
  55. :rtype: str
  56. """
  57. ytid = ytid.split("v")[1]
  58. url = "https://www.twitch.tv/videos/%s" % ytid
  59. return url
  60. @staticmethod
  61. def get_quality_parameter(config):
  62. """Determines which itag value results from codec and resolution
  63. settings and returns it
  64. :param config: configuration object
  65. :type config: lib_stov.configuration.Conf
  66. :return: itag value
  67. :rtype: str
  68. """
  69. LOGGER.debug(_("Trying to determine the itag value for youtube-dl from"
  70. " your quality and codec settings."))
  71. quality_value = 0
  72. if config.values["videocodec"] == "mp4":
  73. if config.values["maxresolution"] == "360p":
  74. quality_value = "360p"
  75. elif config.values["maxresolution"] == "480p":
  76. quality_value = "480p"
  77. elif config.values["maxresolution"] == "720p":
  78. quality_value = "720p"
  79. elif config.values["maxresolution"] == "1080p":
  80. quality_value = "1080p"
  81. if quality_value:
  82. LOGGER.debug(_("Found value: %s."), quality_value)
  83. return quality_value + "/" + config.values["videocodec"]
  84. LOGGER.debug(_("Could not determine an itag value "
  85. "from the configuration"))
  86. return "1080p" + "/" + config.values["videocodec"]