subscription.py 6.4 KB

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