subscription.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2014.
  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. from __future__ import unicode_literals
  17. from lib_stov import youtubeAPI
  18. from lib_stov import stov_exceptions
  19. from lib_stov import noapi
  20. class sub(object):
  21. def __init__(self, type, name, conf, search="", id=0, title="",
  22. directory="", disabled=0):
  23. self.__ID = id
  24. self.__title = title
  25. self.__type = type
  26. self.__name = name
  27. self.__search = search
  28. self.__directory = directory
  29. self.__conf = conf
  30. self.DownloadedVideos = []
  31. self.FailedVideosCount = 0
  32. self.FailedVideos = []
  33. self.ToDelete = []
  34. self.__video_list = []
  35. self.__id_list = []
  36. self.parsed_response = None
  37. if int(disabled) == 0:
  38. self.disabled = False
  39. elif int(disabled) == 1:
  40. self.disabled = True
  41. if self.__conf.values["use_api"]:
  42. self._connector = youtubeAPI.Connector(self.__type, self.__name,
  43. self.__search, self.__conf)
  44. else:
  45. self._connector = noapi.Connector(self.__type, self.__name,
  46. self.__conf, self.__search)
  47. def GetTitle(self):
  48. return self.__title
  49. def GetId(self):
  50. return self.__ID
  51. def SetID(self, id):
  52. self.__ID = id
  53. def CheckStringMatch(self, video):
  54. """Checks if the subscription is enabled and the video matches the
  55. search string defined for the subscription"""
  56. if not self.disabled:
  57. if self.__search != "" and self.__conf.values["check_title"]\
  58. == "yes":
  59. if self.__search in video.title:
  60. return True
  61. else:
  62. return False
  63. else:
  64. return True
  65. else:
  66. return False
  67. def GatherVideos(self, video_list):
  68. """Gathers all videos in the subscription and saves
  69. them in an the internal list so they can be accessed by the object
  70. """
  71. self.__video_list = video_list
  72. def DownloadVideos(self, itag_value):
  73. """Uses the DownloadVideo method of the video object to download all
  74. videos contained in the subscription and adds them to the list of
  75. downloaded videos if the download succeeds.
  76. """
  77. if not self.disabled:
  78. for video in self.__video_list:
  79. if video.downloaded == 0:
  80. if video.DownloadVideo(self.__directory, itag_value) is\
  81. True:
  82. self.DownloadedVideos.append(video)
  83. else:
  84. self.FailedVideosCount += 1
  85. self.FailedVideos.append(video)
  86. else:
  87. raise stov_exceptions.SubscriptionDisabledException(self.__title)
  88. def PrintVideos(self):
  89. """Prepares a human readable list of all videos contained
  90. in the subscription.
  91. """
  92. video_list = []
  93. for i in self.__video_list:
  94. if i.downloaded == 0:
  95. video_list.append(i.title + _(" (pending)"))
  96. elif i.downloaded == 1:
  97. video_list.append(i.title + _(" (downloaded)"))
  98. elif i.downloaded == -1:
  99. video_list.append(i.title + _(" (failed)"))
  100. return video_list
  101. def AddSub(self):
  102. """Adds a new subscription to the database"""
  103. parsed_response = self._connector.ParseAPIData()
  104. self.__title = parsed_response.title
  105. self.__directory = self.__name + "_" + self.__search.replace(" ", "_")
  106. data = (self.__title, self.__type, self.__search, self.__directory,
  107. self.__name, 0)
  108. return data
  109. def CheckAndDelete(self, videos):
  110. """Checks if a video still exists in the current API response and
  111. deletes it if it doesn't
  112. """
  113. parsed_response = self._connector.ParseAPIData()
  114. self.GatherVideos(videos)
  115. for entry in parsed_response.videos:
  116. self.__id_list.append(entry.ytid)
  117. for item in self.__video_list:
  118. if item.ytid not in self.__id_list:
  119. self.ToDelete.append(item)
  120. def update_data(self):
  121. """Updates the data from the API."""
  122. self.parsed_response = self._connector.ParseAPIData(self.__video_list)