subscription.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. from lib_stov import stov_exceptions
  18. from lib_stov import yt_noapi
  19. from lib_stov import zdf_mediathek
  20. from lib_stov import twitch
  21. from lib_stov import vidme
  22. class Sub(object):
  23. """This class constructs a object that stores all the attributes that define
  24. a subscription and performs the necessary operations on it.
  25. """
  26. def __init__(self, subscription_type, name, conf, site, search="",
  27. subscription_id=0, title="", directory="", disabled=0):
  28. self._id = subscription_id
  29. self._title = title
  30. self._type = subscription_type
  31. self._name = name
  32. self._search = search
  33. self._directory = directory
  34. self._conf = conf
  35. self.site = site
  36. self.downloaded_videos = []
  37. self.failed_videos_count = 0
  38. self.failed_videos = []
  39. self.to_delete = []
  40. self._video_list = []
  41. self._id_list = []
  42. self.parsed_response = None
  43. if int(disabled) == 0:
  44. self.disabled = False
  45. elif int(disabled) == 1:
  46. self.disabled = True
  47. if site == "youtube":
  48. self._connector = yt_noapi.Connector(self._type, self._name,
  49. self._conf, self._search)
  50. elif site == "zdf_mediathek":
  51. self._connector = zdf_mediathek.Connector(self._type, self._name,
  52. self._conf)
  53. elif site == "twitch":
  54. self._connector = twitch.Connector(self._type, self._name,
  55. self._conf, self._search)
  56. elif site == "vidme":
  57. self._connector = vidme.Connector(self._type, self._name,
  58. self._conf, self._search)
  59. else:
  60. raise stov_exceptions.SiteUnsupported()
  61. def get_title(self):
  62. """Returns the title attribute."""
  63. return self._title
  64. def get_id(self):
  65. """Returns the id attribute."""
  66. return self._id
  67. def set_id(self, subscription_id):
  68. """Sets the ID attribute."""
  69. self._id = subscription_id
  70. def check_string_match(self, video):
  71. """Checks if the subscription is enabled and the video matches the
  72. search string defined for the subscription"""
  73. if not self.disabled:
  74. if self._search != "" and self._conf.values["check_title"]\
  75. == "yes":
  76. return self._search in video.title
  77. else:
  78. return True
  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)