subscription.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 youtube
  19. from lib_stov import stov_exceptions
  20. from lib_stov import noapi
  21. class sub(object):
  22. def __init__(self, type, name, conf, search="", id=0, title="",
  23. directory="", disabled=0):
  24. self.__ID = id
  25. self.__title = title
  26. self.__type = type
  27. self.__name = name
  28. self.__search = search
  29. self.__directory = directory
  30. self.__conf = conf
  31. self.DownloadedVideos = []
  32. self.FailedVideosCount = 0
  33. self.FailedVideos = []
  34. self.ToDelete = []
  35. self.__video_list = []
  36. self.__id_list = []
  37. self.parsed_response = None
  38. if int(disabled) == 0:
  39. self.__disabled = False
  40. elif int(disabled) == 1:
  41. self.__disabled = True
  42. if self.__conf.values["use_api"]:
  43. self._connector = youtubeAPI.Connector(self.__type, self.__name,
  44. self.__search, self.__conf)
  45. else:
  46. self._connector = noapi.Connector(self.__type, self.__name,
  47. self.__conf, self.__search)
  48. def GetTitle(self):
  49. return self.__title
  50. def GetId(self):
  51. return self.__ID
  52. def SetID(self, id):
  53. self.__ID = id
  54. def CheckStringMatch(self, video):
  55. """Checks if the subscription is enabled and the video matches the
  56. search string defined for the subscription"""
  57. if self.__disabled is False:
  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 GatherVideos(self, video_data):
  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. for i in video_data:
  73. self.__video_list.append(youtube.video(id=i[0], title=i[1],
  74. description=i[2],
  75. ytid=i[3],
  76. downloaded=i[4],
  77. failcount=i[5],
  78. conf=self.__conf))
  79. def DownloadVideos(self, itag_value):
  80. """Uses the DownloadVideo method of the video object to download all
  81. videos contained in the subscription and adds them to the list of
  82. downloaded videos if the download succeeds.
  83. """
  84. if self.__disabled is False:
  85. for video in self.__video_list:
  86. if video.downloaded == 0:
  87. if video.DownloadVideo(self.__directory, itag_value) is\
  88. True:
  89. self.DownloadedVideos.append(video)
  90. else:
  91. self.FailedVideosCount += 1
  92. self.FailedVideos.append(video)
  93. else:
  94. raise stov_exceptions.SubscriptionDisabledException(self.__title)
  95. def PrintVideos(self):
  96. """Prepares a human readable list of all videos contained
  97. in the subscription.
  98. """
  99. video_list = []
  100. for i in self.__video_list:
  101. if i.downloaded == 0:
  102. video_list.append(i.title + _(" (pending)"))
  103. elif i.downloaded == 1:
  104. video_list.append(i.title + _(" (downloaded)"))
  105. elif i.downloaded == -1:
  106. video_list.append(i.title + _(" (failed)"))
  107. return video_list
  108. def AddSub(self):
  109. """Adds a new subscription to the database"""
  110. parsed_response = self._connector.ParseAPIData()
  111. self.__title = parsed_response.title
  112. self.__directory = self.__name + "_" + self.__search.replace(" ", "_")
  113. data = (self.__title, self.__type, self.__search, self.__directory,
  114. self.__name, 0)
  115. return data
  116. def CheckAndDelete(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.ParseAPIData()
  121. self.GatherVideos(videos)
  122. for entry in parsed_response.videos:
  123. self.__id_list.append(entry.ytid)
  124. for item in self.__video_list:
  125. if item.ytid not in self.__id_list:
  126. self.ToDelete.append(item)
  127. def update_data(self):
  128. """Updates the data from the API."""
  129. self.parsed_response = self._connector.ParseAPIData()