# # This file is part of stov, written by Helmut Pozimski 2012-2014. # # 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 from lib_stov import youtubeAPI from lib_stov import stov_exceptions from lib_stov import noapi class sub(object): def __init__(self, type, name, conf, search="", id=0, title="", directory="", disabled=0): self.__ID = id self.__title = title self.__type = type self.__name = name self.__search = search self.__directory = directory self.__conf = conf self.DownloadedVideos = [] self.FailedVideosCount = 0 self.FailedVideos = [] self.ToDelete = [] self.__video_list = [] self.__id_list = [] self.parsed_response = None if int(disabled) == 0: self.disabled = False elif int(disabled) == 1: self.disabled = True if self.__conf.values["use_api"]: self._connector = youtubeAPI.Connector(self.__type, self.__name, self.__search, self.__conf) else: self._connector = noapi.Connector(self.__type, self.__name, self.__conf, self.__search) def GetTitle(self): return self.__title def GetId(self): return self.__ID def SetID(self, id): self.__ID = id def CheckStringMatch(self, video): """Checks if the subscription is enabled and the video matches the search string defined for the subscription""" if not self.disabled: if self.__search != "" and self.__conf.values["check_title"]\ == "yes": if self.__search in video.title: return True else: return False else: return True else: return False def GatherVideos(self, video_list): """Gathers all videos in the subscription and saves them in an the internal list so they can be accessed by the object """ self.__video_list = video_list 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. """ if not self.disabled: for video in self.__video_list: if video.downloaded == 0: if video.DownloadVideo(self.__directory, itag_value) is\ True: self.DownloadedVideos.append(video) else: self.FailedVideosCount += 1 self.FailedVideos.append(video) else: raise stov_exceptions.SubscriptionDisabledException(self.__title) def PrintVideos(self): """Prepares a human readable list of all videos contained in the subscription. """ video_list = [] for i in self.__video_list: if i.downloaded == 0: video_list.append(i.title + _(" (pending)")) elif i.downloaded == 1: video_list.append(i.title + _(" (downloaded)")) elif i.downloaded == -1: video_list.append(i.title + _(" (failed)")) return video_list def AddSub(self): """Adds a new subscription to the database""" parsed_response = self._connector.ParseAPIData() self.__title = parsed_response.title self.__directory = self.__name + "_" + self.__search.replace(" ", "_") data = (self.__title, self.__type, self.__search, self.__directory, self.__name, 0) return data def CheckAndDelete(self, videos): """Checks if a video still exists in the current API response and deletes it if it doesn't """ parsed_response = self._connector.ParseAPIData() self.GatherVideos(videos) for entry in parsed_response.videos: self.__id_list.append(entry.ytid) for item in self.__video_list: if item.ytid not in self.__id_list: self.ToDelete.append(item) def update_data(self): """Updates the data from the API.""" self.parsed_response = self._connector.ParseAPIData(self.__video_list)