subscription.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #
  2. # This file is part of stov, written by Helmut Pozimski in 2012.
  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. import sys
  17. import gettext
  18. import urllib2
  19. import sqlite3
  20. import youtubeAPI
  21. import youtube
  22. from outputhelper import printf
  23. class sub(object):
  24. def __init__(self, type, name, conf, search="", id=0, title="", directory=""):
  25. self.__ID = id
  26. self.__title = title
  27. self.__type = type
  28. self.__name = name
  29. self.__search = search
  30. self.__directory = directory
  31. self.__APIURL = ""
  32. self.__conf = conf
  33. self.DownloadedVideos = []
  34. self.FailedVideos = 0
  35. if self.__name != "delete":
  36. self.__ConstructAPIURL()
  37. def GetTitle(self):
  38. return self.__title
  39. def Delete(self):
  40. """Deletes all videos from the subscription from the database and deletes
  41. the subscription itself once it's done.
  42. """
  43. try:
  44. self.__connection = sqlite3.connect(self.__conf.dbpath)
  45. except sqlite3.OperationalError:
  46. printf(_("Could not write to database, subscription "
  47. "was NOT deleted. Please check permissions and try again."),
  48. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  49. else:
  50. self.__cursor = self.__connection.cursor()
  51. self.__deletevideos = "DELETE FROM videos WHERE subscription_id=?"
  52. self.__cursor.execute(self.__deletevideos, (self.__ID,))
  53. self.__deletesubscription = "DELETE FROM subscriptions WHERE id=?"
  54. self.__cursor.execute(self.__deletesubscription, (self.__ID,))
  55. self.__connection.commit()
  56. printf(_("Subscription deleted successfully!"), outputlevel="default",
  57. level=self.__conf.outputlevel, descriptor="stdout")
  58. self.__connection.close()
  59. def UpdateVideos(self):
  60. """Retrieves the current videos from the youtube API and adds them
  61. to the database if they where not already added earlier
  62. """
  63. self.__ParseAPIData()
  64. self.__processed = []
  65. try:
  66. self.__connection = sqlite3.connect(self.__conf.dbpath)
  67. self.__cursor = self.__connection.cursor()
  68. except sqlite3.OperationalError:
  69. printf(_("Could not write to database, "
  70. "new video was NOT added! Please check permissions and try again."),
  71. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  72. for i in self.__ParsedResponse.videos:
  73. printf(_('Checking if video "%s" exists in the database' %i.title),
  74. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  75. self.__ytid = i.ytid
  76. self.__videoquery = "SELECT id FROM videos WHERE ytid=?"
  77. self.__cursor.execute(self.__videoquery, (self.__ytid,))
  78. self.__tmpid = self.__cursor.fetchall()
  79. if self.__tmpid == []:
  80. printf(_("Video %s not found in database, inserting..." % i.title),
  81. outputlevel="verbose", level=self.__conf.outputlevel,
  82. descriptor="stderr")
  83. self.__query = "INSERT INTO videos (title, description, \
  84. ytid, subscription_id, downloaded) VALUES (?, ?, ?, ?, ?)"
  85. self.__data = (i.title, i.description, self.__ytid, self.__ID, 0)
  86. self.__cursor.execute(self.__query, self.__data)
  87. self.__connection.commit()
  88. self.__connection.close()
  89. def GetVideos(self):
  90. """Retrieves all videos in the subscription from the database and saves
  91. them in an the internal list so they can be accessed by the object
  92. """
  93. self.__videos = []
  94. try:
  95. self.__connection = sqlite3.connect(self.__conf.dbpath)
  96. self.__cursor = self.__connection.cursor()
  97. except sqlite3.OperationalError:
  98. printf(_("Could not access database. "
  99. "Please check permissions and try again."),
  100. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  101. printf(_("Getting all videos for subscription %s from database"
  102. % self.__title), outputlevel="verbose", level=self.__conf.outputlevel,
  103. descriptor="stderr")
  104. self.__videoquerybysubscription = "SELECT id, title, description, \
  105. ytid, downloaded, failcnt FROM videos WHERE subscription_id=?"
  106. self.__cursor.execute(self.__videoquerybysubscription, (self.__ID,))
  107. self.__videodata = self.__cursor.fetchall()
  108. for i in self.__videodata:
  109. printf(_("Got video %s" % i[1]),
  110. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  111. self.__videos.append(youtube.video(id=i[0],
  112. title=i[1], description=i[2], ytid=i[3],
  113. downloaded=i[4], failcount=i[5], conf=self.__conf))
  114. self.__connection.close()
  115. def DownloadVideos(self, itag_value):
  116. """Uses the DownloadVideo method of the video object to download all
  117. videos contained in the subscription and adds them to the list of
  118. downloaded videos if the download succeeds.
  119. """
  120. for video in self.__videos:
  121. if video.downloaded == 0:
  122. if video.DownloadVideo(self.__directory, itag_value) is True:
  123. self.DownloadedVideos.append(video.title)
  124. else:
  125. self.FailedVideos = self.FailedVideos + 1
  126. """Prints a list of all videos contained in the subscription"""
  127. def PrintVideos(self):
  128. printf(_("Videos in subscription ") + self.__title + ":\n",
  129. outputlevel="default", level=self.__conf.outputlevel, descriptor="stdout")
  130. for i in self.__videos:
  131. if i.downloaded == 0:
  132. printf(i.title + _(" (pending)"), outputlevel="default",
  133. level=self.__conf.outputlevel, descriptor="stdout")
  134. elif i.downloaded == 1:
  135. printf(i.title + _(" (downloaded)"), outputlevel="default",
  136. level=self.__conf.outputlevel, descriptor="stdout")
  137. elif i.downloaded == -1:
  138. printf(i.title + _(" (failed)"), outputlevel="default",
  139. level=self.__conf.outputlevel, descriptor="stdout")
  140. def AddSub(self):
  141. """Adds a new subscription to the database"""
  142. self.__ParseAPIData()
  143. try:
  144. self.__connection = sqlite3.connect(self.__conf.dbpath)
  145. except sqlite3.OperationalError:
  146. printf(_("Could not write to database, new "
  147. "subscription was NOT added! Please check permissions and try again."),
  148. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  149. else:
  150. self.__cursor = self.__connection.cursor()
  151. self.__title = self.__ParsedResponse.title
  152. printf(_("Found subscription title %s" % self.__title),
  153. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  154. self.__directory = self.__name + "_" + self.__search.replace(" ", "_")
  155. printf(_("Directory: %s" % self.__directory),
  156. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  157. self.__query = "INSERT INTO subscriptions (title, type, searchstring, \
  158. directory, name) VALUES (?, ?, ?, ?, ?)"
  159. self.__data = (self.__title, self.__type, self.__search,
  160. self.__directory, self.__name)
  161. printf(_("Writing subscription info into database..."),
  162. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  163. self.__cursor.execute(self.__query, self.__data)
  164. self.__connection.commit()
  165. self.__query = "SELECT id from subscriptions where title=?"
  166. self.__data = (self.__title, )
  167. self.__cursor.execute(self.__query, self.__data)
  168. self.__ID = self.__cursor.fetchone()
  169. self.__ID = self.__ID[0]
  170. printf(_("Subscription got internal ID %s" %self.__ID),
  171. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  172. self.__connection.close()
  173. def __ParseAPIData(self):
  174. """Retrieves the youtube API data and parses them so they can be used
  175. by the object
  176. """
  177. try:
  178. printf(_("Connectiong to youtube API."),
  179. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  180. self.__ConnectAPI = urllib2.urlopen(self.__APIURL)
  181. printf(_("Connection established, reading data."),
  182. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  183. self.__APIResponse = self.__ConnectAPI.read()
  184. self.__ConnectAPI.close()
  185. except IOError:
  186. printf(_("Could not get API data, maybe the API "
  187. "is down or you have given wrong parameters"
  188. ", please check and try again!"),
  189. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  190. sys.exit(1)
  191. else:
  192. printf(_("Parsing youtube API response."),
  193. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  194. parser = youtubeAPI.Parser(self.__APIResponse)
  195. self.__ParsedResponse = parser.parse()
  196. def __ConstructAPIURL(self):
  197. """Constructs the API URL which is used to retrieve API data"""
  198. if self.__type == "channel":
  199. self.__APIURL = "https://gdata.youtube.com/feeds/api/users/"\
  200. + urllib2.quote(self.__name) + "/uploads/" + "?v=2"\
  201. + "&max-results=%s" % self.__conf.values["maxvideos"]
  202. if self.__search != "":
  203. self.__APIURL = self.__APIURL + "&q=" + "%22"\
  204. + urllib2.quote(self.__search) + "%22"
  205. elif self.__type == "search":
  206. self.__APIURL = "http://gdata.youtube.com/feeds/api/videos?q="\
  207. + urllib2.quote(self.__search) + "&v=2"\
  208. + "&max-results=%s" % self.__conf.values["maxvideos"]
  209. elif self.__type == "playlist":
  210. self.__APIURL = "https://gdata.youtube.com/feeds/api/playlists/"\
  211. + "%20" + urllib2.quote(self.__name) + "%20" + "?v=2"\
  212. + "&max-results=%s" % self.__conf.values["maxvideos"]
  213. else:
  214. printf(_("None or invalid subscription type given, "
  215. "please check the type option and try again"),
  216. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  217. printf(_("Constructed the following API URL: %s" % self.__APIURL),
  218. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  219. def CheckAndDelete(self):
  220. """Checks if a video still exists in the current API response and deletes
  221. it if it doesn't
  222. """
  223. self.__ParseAPIData()
  224. self.GetVideos()
  225. self.__id_list = []
  226. for entry in self.__ParsedResponse.videos:
  227. self.__id_list.append(entry.ytid)
  228. for item in self.__videos:
  229. if item.ytid not in self.__id_list:
  230. if item.delete() is True:
  231. printf(_("Video %s deleted from the database!" % item.title),
  232. outputlevel="verbose", level=self.__conf.outputlevel,
  233. descriptor="stdout")
  234. else:
  235. printf(_("Videos %s could not be deleted from the database"),
  236. outputlevel="verbose", level=self.__conf.outputlevel,
  237. descriptor="stderr")