subscription.py 5.4 KB

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