1
0

subscription.py 4.7 KB

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