123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- # -*- coding: utf8 -*-
- import youtube
- import sys
- import gettext
- import urllib
- import feedparser
- import sqlite3
- class sub(object):
- def __init__(self, type, name, conf, search="", id=0, title="", directory=""):
- self.__ID = id
- self.__title = title
- self.__type = type
- self.__name = name
- self.__search = search
- self.__directory = directory
- self.__APIURL = ""
- self.__conf = conf
- self.DownloadedVideos = []
-
- if self.__name != "delete":
- self.__InitSub()
- def GetTitle(self):
- return self.__title
- def Delete(self):
- try:
- self.__connection = sqlite3.connect(self.__conf.dbpath)
- self.__cursor = self.__connection.cursor()
- self.__cursor.execute("DELETE FROM videos WHERE subscription_id='%i'" %self.__ID)
- self.__cursor.execute("DELETE FROM subscriptions WHERE id='%i'" %self.__ID)
- self.__connection.commit()
- except sqlite3.OperationalError:
- print >> sys.stderr, _("Could not write to database, new video was NOT added! Please check permissions and try again.")
- print _("Subscription deleted successfully!")
- self.__connection.close()
- def GetVideos(self):
- self.__videos = []
- self.__processed = []
- try:
- self.__connection = sqlite3.connect(self.__conf.dbpath)
- self.__cursor = self.__connection.cursor()
- except sqlite3.OperationalError:
- print >> sys.stderr, _("Could not write to database, new video was NOT added! Please check permissions and try again.")
- for i in self.__ParsedResponse.entries:
- self.__ytid = i.link.partition("=")
- self.__ytid = self.__ytid[2].partition("&")
- self.__ytid = self.__ytid[0]
- self.__cursor.execute("SELECT id FROM videos WHERE ytid='%s'" %self.__ytid)
- self.__tmpid = self.__cursor.fetchall()
- if self.__tmpid == []:
- self.__query = "INSERT INTO videos (title, description, ytid, subscription_id, downloaded) VALUES (?, ?, ?, ?, ?)"
- self.__data = (i.title, i.description, self.__ytid, self.__ID, 0)
- self.__cursor.execute(self.__query, self.__data)
-
- self.__cursor.execute("SELECT id, title, description, ytid, downloaded FROM videos WHERE subscription_id=%s" %self.__ID)
- self.__videodata = self.__cursor.fetchall()
- for i in self.__videodata:
- self.__videos.append(youtube.video(id = i[0], title = i[1], description = i[2], ytid = i[3], downloaded = i[4], conf=self.__conf))
- self.__connection.commit()
- self.__connection.close()
-
-
- def DownloadVideos(self):
- for video in self.__videos:
- if video.DownloadVideo(self.__directory) == True:
- self.DownloadedVideos.append(video.title)
-
- def PrintVideos(self):
- print _("Videos in subscription ") + self.__title + ":\n"
- for i in self.__videos:
- print i.title
- def AddSub(self):
- try:
- self.__connection = sqlite3.connect(self.__conf.dbpath)
- self.__cursor = self.__connection.cursor()
- self.__title = self.__ParsedResponse.feed.title
- self.__directory = self.__name + "_" + self.__search.replace(" ", "_")
- self.__query = "INSERT INTO subscriptions (title, type, searchstring, directory, name) VALUES (?, ?, ?, ?, ?)"
- self.__data = (self.__title, self.__type, self.__search, self.__directory, self.__name)
- self.__cursor.execute(self.__query, self.__data)
- self.__connection.commit()
- except sqlite3.OperationalError:
- print >> sys.stderr, _("Could not write to database, new subscription was NOT added! Please check permissions and try again.")
- self.__query = "SELECT id from subscriptions where title=?"
- self.__data = (self.__title, )
- self.__cursor.execute(self.__query, self.__data)
- self.__ID = self.__cursor.fetchone()
- self.__ID = self.__ID[0]
- self.__connection.close()
- def __ParseAPIData(self):
- try:
- self.__APIResponse = urllib.urlopen(self.__APIURL)
- except IOError:
- 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!")
- self.__ParsedResponse = feedparser.parse(self.__APIResponse)
- def __ConstructAPIURL(self):
- if self.__type == "channel":
- self.__APIURL = "https://gdata.youtube.com/feeds/api/users/" + self.__name + "/uploads/"
- if self.__search != "":
- self.__APIURL = self.__APIURL + "?q=" + "%22" + self.__search + "%22"
- elif self.__type == "search":
- self.__APIURL = "http://gdata.youtube.com/feeds/api/videos?q=%22" + self.__search + "%22&max-results=10"
- elif self.__type == "playlist":
- self.__APIURL = "https://gdata.youtube.com/feeds/api/playlists/" + self.__name
- else:
- print >> sys.stderr, _("None or invalid subscription type given, please check the type option and try again")
-
- def __InitSub(self):
- self.__ConstructAPIURL()
- self.__ParseAPIData()
|