subscription.py 11 KB

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