subscription.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf8 -*-
  2. import youtube
  3. import sys
  4. import gettext
  5. import urllib
  6. import feedparser
  7. import sqlite3
  8. class sub(object):
  9. def __init__(self, type, name, conf, search="", id=0, title="", directory=""):
  10. self.__ID = id
  11. self.__title = title
  12. self.__type = type
  13. self.__name = name
  14. self.__search = search
  15. self.__directory = directory
  16. self.__APIURL = ""
  17. self.__conf = conf
  18. self.DownloadedVideos = []
  19. if self.__name != "delete":
  20. self.__InitSub()
  21. def GetTitle(self):
  22. return self.__title
  23. def Delete(self):
  24. try:
  25. self.__connection = sqlite3.connect(self.__conf.dbpath)
  26. self.__cursor = self.__connection.cursor()
  27. self.__cursor.execute("DELETE FROM videos WHERE subscription_id='%i'" %self.__ID)
  28. self.__cursor.execute("DELETE FROM subscriptions WHERE id='%i'" %self.__ID)
  29. self.__connection.commit()
  30. except sqlite3.OperationalError:
  31. print >> sys.stderr, _("Could not write to database, new video was NOT added! Please check permissions and try again.")
  32. print _("Subscription deleted successfully!")
  33. self.__connection.close()
  34. def GetVideos(self):
  35. self.__videos = []
  36. self.__processed = []
  37. try:
  38. self.__connection = sqlite3.connect(self.__conf.dbpath)
  39. self.__cursor = self.__connection.cursor()
  40. except sqlite3.OperationalError:
  41. print >> sys.stderr, _("Could not write to database, new video was NOT added! Please check permissions and try again.")
  42. for i in self.__ParsedResponse.entries:
  43. self.__ytid = i.link.partition("=")
  44. self.__ytid = self.__ytid[2].partition("&")
  45. self.__ytid = self.__ytid[0]
  46. self.__cursor.execute("SELECT id FROM videos WHERE ytid='%s'" %self.__ytid)
  47. self.__tmpid = self.__cursor.fetchall()
  48. if self.__tmpid == []:
  49. self.__query = "INSERT INTO videos (title, description, ytid, subscription_id, downloaded) VALUES (?, ?, ?, ?, ?)"
  50. self.__data = (i.title, i.description, self.__ytid, self.__ID, 0)
  51. self.__cursor.execute(self.__query, self.__data)
  52. self.__cursor.execute("SELECT id, title, description, ytid, downloaded FROM videos WHERE subscription_id=%s" %self.__ID)
  53. self.__videodata = self.__cursor.fetchall()
  54. for i in self.__videodata:
  55. self.__videos.append(youtube.video(id = i[0], title = i[1], description = i[2], ytid = i[3], downloaded = i[4], conf=self.__conf))
  56. self.__connection.commit()
  57. self.__connection.close()
  58. def DownloadVideos(self):
  59. for video in self.__videos:
  60. if video.DownloadVideo(self.__directory) == True:
  61. self.DownloadedVideos.append(video.title)
  62. def PrintVideos(self):
  63. print _("Videos in subscription ") + self.__title + ":\n"
  64. for i in self.__videos:
  65. print i.title
  66. def AddSub(self):
  67. try:
  68. self.__connection = sqlite3.connect(self.__conf.dbpath)
  69. self.__cursor = self.__connection.cursor()
  70. self.__title = self.__ParsedResponse.feed.title
  71. self.__directory = self.__name + "_" + self.__search.replace(" ", "_")
  72. self.__query = "INSERT INTO subscriptions (title, type, searchstring, directory, name) VALUES (?, ?, ?, ?, ?)"
  73. self.__data = (self.__title, self.__type, self.__search, self.__directory, self.__name)
  74. self.__cursor.execute(self.__query, self.__data)
  75. self.__connection.commit()
  76. except sqlite3.OperationalError:
  77. print >> sys.stderr, _("Could not write to database, new subscription was NOT added! Please check permissions and try again.")
  78. self.__query = "SELECT id from subscriptions where title=?"
  79. self.__data = (self.__title, )
  80. self.__cursor.execute(self.__query, self.__data)
  81. self.__ID = self.__cursor.fetchone()
  82. self.__ID = self.__ID[0]
  83. self.__connection.close()
  84. def __ParseAPIData(self):
  85. try:
  86. self.__APIResponse = urllib.urlopen(self.__APIURL)
  87. except IOError:
  88. print >> sys.stderr, _("Could not get API data, maybe the API is down or you have a problem with your network, please check and try again!")
  89. self.__ParsedResponse = feedparser.parse(self.__APIResponse)
  90. def __ConstructAPIURL(self):
  91. if self.__type == "channel":
  92. self.__APIURL = "https://gdata.youtube.com/feeds/api/users/" + self.__name + "/uploads/"
  93. if self.__search != "":
  94. self.__APIURL = self.__APIURL + "?q=" + "%22" + self.__search + "%22"
  95. elif self.__type == "search":
  96. self.__APIURL = "http://gdata.youtube.com/feeds/api/videos?q=%22" + self.__search + "%22&max-results=10"
  97. elif self.__type == "playlist":
  98. self.__APIURL = "https://gdata.youtube.com/feeds/api/playlists/" + self.__name
  99. else:
  100. print >> sys.stderr, _("None or invalid subscription type given, please check the type option and try again")
  101. def __InitSub(self):
  102. self.__ConstructAPIURL()
  103. self.__ParseAPIData()