subscription.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 takes care of managing subscriptions."""
  17. import logging
  18. from lib_stov import configuration
  19. from lib_stov import stov_exceptions
  20. from lib_stov import twitch
  21. from lib_stov import yt_noapi
  22. from lib_stov import zdf_mediathek
  23. LOGGER = logging.getLogger("stov")
  24. class Sub:
  25. """This class constructs a object that stores all the attributes that define
  26. a subscription and performs the necessary operations on it.
  27. """
  28. def __init__(self, subscription_type, name, site, search="",
  29. subscription_id=0, title="", directory="", disabled=0):
  30. self._id = subscription_id
  31. self._title = title
  32. self._type = subscription_type
  33. self._name = name
  34. self._search = search
  35. self._directory = directory
  36. self._conf = configuration.Conf.get_instance()
  37. self.site = site
  38. self.downloaded_videos = []
  39. self.failed_videos_count = 0
  40. self.failed_videos = []
  41. self.to_delete = []
  42. self._video_list = []
  43. self._id_list = []
  44. self.parsed_response = None
  45. if int(disabled) == 0:
  46. self.disabled = False
  47. elif int(disabled) == 1:
  48. self.disabled = True
  49. if site == "youtube":
  50. self._connector = yt_noapi.Connector(self._type, self._name,
  51. self._search)
  52. elif site == "zdf_mediathek":
  53. self._connector = zdf_mediathek.Connector(self._type, self._name,
  54. self._search)
  55. elif site == "twitch":
  56. self._connector = twitch.Connector(self._type, self._name,
  57. self._search)
  58. else:
  59. raise stov_exceptions.SiteUnsupported()
  60. def get_title(self):
  61. """Returns the title attribute."""
  62. return self._title
  63. def get_id(self):
  64. """Returns the id attribute."""
  65. return self._id
  66. def set_id(self, subscription_id):
  67. """Sets the ID attribute."""
  68. self._id = subscription_id
  69. def check_string_match(self, video):
  70. """Checks if the subscription is enabled and the video matches the
  71. search string defined for the subscription"""
  72. if not self.disabled:
  73. LOGGER.debug(_("Matching parameter %s with title %s"),
  74. self._search, video.title)
  75. if self._search != "" and self._conf.values["check_title"] \
  76. == "yes":
  77. return self._search in video.title
  78. return self._search.lower() in video.title.lower()
  79. return False
  80. def gather_videos(self, video_list):
  81. """Gathers all videos in the subscription and saves
  82. them in an the internal list so they can be accessed by the object
  83. """
  84. self._video_list = video_list
  85. def download_videos(self):
  86. """Uses the DownloadVideo method of the video object to download all
  87. videos contained in the subscription and adds them to the list of
  88. downloaded videos if the download succeeds.
  89. """
  90. if not self.disabled:
  91. for video in self._video_list:
  92. if video.downloaded == 0:
  93. video_url = self._connector.construct_video_url(
  94. video.site_id)
  95. if video.download_video(self._directory, video_url):
  96. self.downloaded_videos.append(video)
  97. else:
  98. self.failed_videos_count += 1
  99. self.failed_videos.append(video)
  100. else:
  101. raise stov_exceptions.SubscriptionDisabledException(self._title)
  102. def print_videos(self):
  103. """Prepares a human readable list of all videos contained
  104. in the subscription.
  105. """
  106. video_list = []
  107. for i in self._video_list:
  108. if i.downloaded == 0:
  109. video_list.append(i.title + _(" (pending)"))
  110. elif i.downloaded == 1:
  111. video_list.append(i.title + _(" (downloaded)"))
  112. elif i.downloaded == -1:
  113. video_list.append(i.title + _(" (failed)"))
  114. return video_list
  115. def add_sub(self):
  116. """Adds a new subscription to the database"""
  117. parsed_response = self._connector.parse_api_data([])
  118. self._title = parsed_response.title
  119. self._type = parsed_response.type
  120. if self._type == "channel" or self._type == "playlist":
  121. self._directory = self._title.replace(" ", "_")
  122. else:
  123. self._directory = self._name + "_" + \
  124. self._search.replace(" ", "_")
  125. data = (self._title, self._type, self._search, self._directory,
  126. self._name, 0, self.site)
  127. return data
  128. def check_and_delete(self, videos):
  129. """Checks if a video still exists in the current API response and
  130. deletes it if it doesn't
  131. """
  132. parsed_response = self._connector.parse_api_data([])
  133. self._type = parsed_response.type
  134. self.gather_videos(videos)
  135. for entry in parsed_response.videos:
  136. self._id_list.append(entry.video_id)
  137. for item in self._video_list:
  138. if item.site_id not in self._id_list:
  139. self.to_delete.append(item)
  140. def update_data(self):
  141. """Updates the data from the API."""
  142. self.parsed_response = self._connector.parse_api_data(self._video_list)