1
0

twitch.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2021.
  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. self._url = ""
  32. self._title = ""
  33. def _construct_url(self):
  34. """ Constructs the URL for the subscription to retrieve
  35. data from.
  36. """
  37. if self._type == "user":
  38. self._url = "https://www.twitch.tv/%s/videos/all" \
  39. % urllib.parse.quote(self._name)
  40. elif self._type == "search":
  41. raise stov_exceptions.TypeNotSupported()
  42. elif self._type == "playlist":
  43. raise stov_exceptions.TypeNotSupported()
  44. LOGGER.debug(_("Constructed URL for subscription: %s"), self._url)
  45. def _fetch_title(self):
  46. """Sets the title of a subscription"""
  47. self._title = self._name
  48. if self._search != "":
  49. self._title += _(" search %s") % self._search
  50. @staticmethod
  51. def construct_video_url(ytid):
  52. """
  53. Resturns the URL to a specified twitch video
  54. :param ytid: Twitch ID of the video
  55. :type ytid: str
  56. :return: Video URL
  57. :rtype: str
  58. """
  59. ytid = ytid.split("v")[1]
  60. url = "https://www.twitch.tv/videos/%s" % ytid
  61. return url