subscription.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. import sys
  18. import ssl
  19. if sys.version_info >= (3,):
  20. import urllib.request as urllib2
  21. else:
  22. import urllib2
  23. from lib_stov import youtubeAPI
  24. from lib_stov import youtube
  25. from lib_stov import stov_exceptions
  26. class sub(object):
  27. def __init__(self, type, name, conf, search="", id=0, title="",
  28. directory="", disabled=0):
  29. self.__ID = id
  30. self.__title = title
  31. self.__type = type
  32. self.__name = name
  33. self.__search = search
  34. self.__directory = directory
  35. self.__APIURL = ""
  36. self.__conf = conf
  37. self.DownloadedVideos = []
  38. self.FailedVideosCount = 0
  39. self.FailedVideos = []
  40. self.ToDelete = []
  41. self.__video_list = []
  42. self.parsed_response = None
  43. self.__id_list = []
  44. if int(disabled) == 0:
  45. self.__disabled = False
  46. elif int(disabled) == 1:
  47. self.__disabled = True
  48. self.__ConstructAPIURL()
  49. def GetTitle(self):
  50. return self.__title
  51. def GetId(self):
  52. return self.__ID
  53. def SetID(self, id):
  54. self.__ID = id
  55. def CheckStringMatch(self, video):
  56. """Checks if the subscription is enabled and the video matches the
  57. search string defined for the subscription"""
  58. if self.__disabled is False:
  59. if self.__search != "" and self.__conf.values["check_title"]\
  60. == "yes":
  61. if self.__search in video.title:
  62. return True
  63. else:
  64. return False
  65. else:
  66. return True
  67. else:
  68. return False
  69. def GatherVideos(self, video_data):
  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. for i in video_data:
  74. self.__video_list.append(youtube.video(id=i[0], title=i[1],
  75. description=i[2],
  76. ytid=i[3],
  77. downloaded=i[4],
  78. failcount=i[5],
  79. conf=self.__conf))
  80. def DownloadVideos(self, itag_value):
  81. """Uses the DownloadVideo method of the video object to download all
  82. videos contained in the subscription and adds them to the list of
  83. downloaded videos if the download succeeds.
  84. """
  85. if self.__disabled is False:
  86. for video in self.__video_list:
  87. if video.downloaded == 0:
  88. if video.DownloadVideo(self.__directory, itag_value) is\
  89. True:
  90. self.DownloadedVideos.append(video)
  91. else:
  92. self.FailedVideosCount += 1
  93. self.FailedVideos.append(video)
  94. else:
  95. raise stov_exceptions.SubscriptionDisabledException(self.__title)
  96. def PrintVideos(self):
  97. """Prepares a human readable list of all videos contained
  98. in the subscription.
  99. """
  100. video_list = []
  101. for i in self.__video_list:
  102. if i.downloaded == 0:
  103. video_list.append(i.title + _(" (pending)"))
  104. elif i.downloaded == 1:
  105. video_list.append(i.title + _(" (downloaded)"))
  106. elif i.downloaded == -1:
  107. video_list.append(i.title + _(" (failed)"))
  108. return video_list
  109. def AddSub(self):
  110. """Adds a new subscription to the database"""
  111. self.ParseAPIData()
  112. self.__title = self.parsed_response.title
  113. self.__directory = self.__name + "_" + self.__search.replace(" ", "_")
  114. data = (self.__title, self.__type, self.__search, self.__directory,
  115. self.__name, 0)
  116. return data
  117. def ParseAPIData(self):
  118. """Retrieves the youtube API data and parses them so they can be used
  119. by the object
  120. """
  121. try:
  122. ConnectAPI = urllib2.urlopen(self.__APIURL, timeout=30)
  123. APIResponse = ConnectAPI.read()
  124. ConnectAPI.close()
  125. except urllib2.HTTPError:
  126. raise stov_exceptions.NoDataFromYoutubeAPIException()
  127. except ssl.SSLError:
  128. raise stov_exceptions.YoutubeAPITimeoutException(self.__title)
  129. else:
  130. parser = youtubeAPI.Parser(APIResponse)
  131. self.parsed_response = parser.parse()
  132. def __ConstructAPIURL(self):
  133. """Constructs the API URL which is used to retrieve API data"""
  134. if self.__type == "channel":
  135. self.__APIURL = "https://gdata.youtube.com/feeds/api/users/" \
  136. + urllib2.quote(self.__name) + "/uploads/" \
  137. "?v=2&max-results=%s" \
  138. % self.__conf.values["maxvideos"]
  139. if self.__search != "":
  140. self.__APIURL = self.__APIURL + "&q=" + "%22" \
  141. + urllib2.quote(self.__search) + "%22"
  142. elif self.__type == "search":
  143. self.__APIURL = "http://gdata.youtube.com/feeds/api/videos?q=" \
  144. + urllib2.quote(self.__search) + "&v=2" \
  145. + "&max-results=%s" \
  146. % self.__conf.values["maxvideos"]
  147. elif self.__type == "playlist":
  148. self.__APIURL = "https://gdata.youtube.com/feeds/api/playlists/" \
  149. + "%20" + urllib2.quote(self.__name) + "%20?v=2" \
  150. + "&max-results=%s" \
  151. % self.__conf.values["maxvideos"]
  152. def CheckAndDelete(self, videos):
  153. """Checks if a video still exists in the current API response and
  154. deletes it if it doesn't
  155. """
  156. self.ParseAPIData()
  157. self.GatherVideos(videos)
  158. for entry in self.parsed_response.videos:
  159. self.__id_list.append(entry.ytid)
  160. for item in self.__video_list:
  161. if item.ytid not in self.__id_list:
  162. self.ToDelete.append(item)