subscription.py 5.1 KB

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