# # This file is part of stov, written by Helmut Pozimski in 2012. # # stov is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 2 of the License. # # stov is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with stov. If not, see . # -*- coding: utf8 -*- from __future__ import unicode_literals import sys import gettext import sqlite3 if sys.version_info >= (3,): import urllib.request as urllib2 else: import urllib2 import youtubeAPI import youtube from outputhelper import printf 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 = [] self.FailedVideos = 0 if self.__name != "delete": self.__ConstructAPIURL() def GetTitle(self): return self.__title def Delete(self): """Deletes all videos from the subscription from the database and deletes the subscription itself once it's done. """ try: self.__connection = sqlite3.connect(self.__conf.dbpath) except sqlite3.OperationalError: printf(_("Could not write to database, subscription " "was NOT deleted. Please check permissions and try again."), outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr") else: self.__cursor = self.__connection.cursor() self.__deletevideos = "DELETE FROM videos WHERE subscription_id=?" self.__cursor.execute(self.__deletevideos, (self.__ID,)) self.__deletesubscription = "DELETE FROM subscriptions WHERE id=?" self.__cursor.execute(self.__deletesubscription, (self.__ID,)) self.__connection.commit() printf(_("Subscription deleted successfully!"), outputlevel="default", level=self.__conf.outputlevel, descriptor="stdout") self.__connection.close() def UpdateVideos(self): """Retrieves the current videos from the youtube API and adds them to the database if they where not already added earlier """ self.__ParseAPIData() self.__processed = [] try: self.__connection = sqlite3.connect(self.__conf.dbpath) self.__cursor = self.__connection.cursor() except sqlite3.OperationalError: printf(_("Could not write to database, " "new video was NOT added! Please check permissions and try again."), outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr") for i in self.__ParsedResponse.videos: printf(_('Checking if video "%s" exists in the database') % i.title, outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") self.__ytid = i.ytid if self.__search != "" and self.__conf.values["check_title"] == "yes": if self.__search not in i.title: printf(_("You have requested title checking and the title of the video " "does not match with the search string, ignoring it"), outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") continue self.__videoquery = "SELECT id FROM videos WHERE ytid=?" self.__cursor.execute(self.__videoquery, (self.__ytid,)) self.__tmpid = self.__cursor.fetchall() if self.__tmpid == []: printf(_("Video %s not found in database, inserting...") % i.title, outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") 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.__connection.commit() self.__connection.close() def GetVideos(self): """Retrieves all videos in the subscription from the database and saves them in an the internal list so they can be accessed by the object """ self.__videos = [] try: self.__connection = sqlite3.connect(self.__conf.dbpath) self.__cursor = self.__connection.cursor() except sqlite3.OperationalError: printf(_("Could not access database. " "Please check permissions and try again."), outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr") printf(_("Getting all videos for subscription %s from database") % self.__title, outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") self.__videoquerybysubscription = "SELECT id, title, description, \ ytid, downloaded, failcnt FROM videos WHERE subscription_id=?" self.__cursor.execute(self.__videoquerybysubscription, (self.__ID,)) self.__videodata = self.__cursor.fetchall() for i in self.__videodata: printf(_("Got video %s") % i[1], outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") self.__videos.append(youtube.video(id=i[0], title=i[1], description=i[2], ytid=i[3], downloaded=i[4], failcount=i[5], conf=self.__conf)) self.__connection.close() def DownloadVideos(self, itag_value): """Uses the DownloadVideo method of the video object to download all videos contained in the subscription and adds them to the list of downloaded videos if the download succeeds. """ for video in self.__videos: if video.downloaded == 0: if video.DownloadVideo(self.__directory, itag_value) is True: self.DownloadedVideos.append(video.title) else: self.FailedVideos = self.FailedVideos + 1 """Prints a list of all videos contained in the subscription""" def PrintVideos(self): printf(_("Videos in subscription ") + self.__title + ":\n", outputlevel="default", level=self.__conf.outputlevel, descriptor="stdout") for i in self.__videos: if i.downloaded == 0: printf(i.title + _(" (pending)"), outputlevel="default", level=self.__conf.outputlevel, descriptor="stdout") elif i.downloaded == 1: printf(i.title + _(" (downloaded)"), outputlevel="default", level=self.__conf.outputlevel, descriptor="stdout") elif i.downloaded == -1: printf(i.title + _(" (failed)"), outputlevel="default", level=self.__conf.outputlevel, descriptor="stdout") def AddSub(self): """Adds a new subscription to the database""" self.__ParseAPIData() try: self.__connection = sqlite3.connect(self.__conf.dbpath) except sqlite3.OperationalError: printf(_("Could not write to database, new " "subscription was NOT added! Please check permissions and try again."), outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr") else: self.__cursor = self.__connection.cursor() self.__title = self.__ParsedResponse.title printf(_("Found subscription title %s") % self.__title, outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") self.__directory = self.__name + "_" + self.__search.replace(" ", "_") printf(_("Directory: %s") % self.__directory, outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") self.__query = "INSERT INTO subscriptions (title, type, searchstring, \ directory, name) VALUES (?, ?, ?, ?, ?)" self.__data = (self.__title, self.__type, self.__search, self.__directory, self.__name) printf(_("Writing subscription info into database..."), outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") self.__cursor.execute(self.__query, self.__data) self.__connection.commit() 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] printf(_("Subscription got internal ID %s") % self.__ID, outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") self.__connection.close() def __ParseAPIData(self): """Retrieves the youtube API data and parses them so they can be used by the object """ try: printf(_("Connectiong to youtube API."), outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") self.__ConnectAPI = urllib2.urlopen(self.__APIURL) printf(_("Connection established, reading data."), outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") self.__APIResponse = self.__ConnectAPI.read() self.__ConnectAPI.close() except IOError: printf(_("Could not get API data, maybe the API " "is down or you have given wrong parameters" ", please check and try again!"), outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr") sys.exit(1) else: printf(_("Parsing youtube API response."), outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") parser = youtubeAPI.Parser(self.__APIResponse) self.__ParsedResponse = parser.parse() def __ConstructAPIURL(self): """Constructs the API URL which is used to retrieve API data""" if self.__type == "channel": self.__APIURL = "https://gdata.youtube.com/feeds/api/users/"\ + urllib2.quote(self.__name) + "/uploads/" + "?v=2"\ + "&max-results=%s" % self.__conf.values["maxvideos"] if self.__search != "": self.__APIURL = self.__APIURL + "&q=" + "%22"\ + urllib2.quote(self.__search) + "%22" elif self.__type == "search": self.__APIURL = "http://gdata.youtube.com/feeds/api/videos?q="\ + urllib2.quote(self.__search) + "&v=2"\ + "&max-results=%s" % self.__conf.values["maxvideos"] elif self.__type == "playlist": self.__APIURL = "https://gdata.youtube.com/feeds/api/playlists/"\ + "%20" + urllib2.quote(self.__name) + "%20" + "?v=2"\ + "&max-results=%s" % self.__conf.values["maxvideos"] else: printf(_("None or invalid subscription type given, " "please check the type option and try again"), outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr") printf(_("Constructed the following API URL: %s") % self.__APIURL, outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr") def CheckAndDelete(self): """Checks if a video still exists in the current API response and deletes it if it doesn't """ self.__ParseAPIData() self.GetVideos() self.__id_list = [] for entry in self.__ParsedResponse.videos: self.__id_list.append(entry.ytid) for item in self.__videos: if item.ytid not in self.__id_list: if item.delete() is True: printf(_("Video %s deleted from the database!") % item.title, outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stdout") else: printf(_("Videos %s could not be deleted from the database"), outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")