subscription.py 6.2 KB

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