# # This file is part of stov, written by Helmut Pozimski 2012-2017. # # 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 -*- """This module takes care of managing subscriptions.""" from lib_stov import stov_exceptions from lib_stov import yt_noapi from lib_stov import zdf_mediathek from lib_stov import twitch from lib_stov import vidme class Sub(object): """This class constructs a object that stores all the attributes that define a subscription and performs the necessary operations on it. """ def __init__(self, subscription_type, name, conf, site, search="", subscription_id=0, title="", directory="", disabled=0): self._id = subscription_id self._title = title self._type = subscription_type self._name = name self._search = search self._directory = directory self._conf = conf self.site = site self.downloaded_videos = [] self.failed_videos_count = 0 self.failed_videos = [] self.to_delete = [] 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 site == "youtube": self._connector = yt_noapi.Connector(self._type, self._name, self._conf, self._search) elif site == "zdf_mediathek": self._connector = zdf_mediathek.Connector(self._type, self._name, self._conf) elif site == "twitch": self._connector = twitch.Connector(self._type, self._name, self._conf, self._search) elif site == "vidme": self._connector = vidme.Connector(self._type, self._name, self._conf, self._search) else: raise stov_exceptions.SiteUnsupported() def get_title(self): """Returns the title attribute.""" return self._title def get_id(self): """Returns the id attribute.""" return self._id def set_id(self, subscription_id): """Sets the ID attribute.""" self._id = subscription_id def check_string_match(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": return self._search in video.title else: return True else: return False def gather_videos(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 download_videos(self): """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: itag_value = self._connector.get_quality_parameter(self._conf) for video in self._video_list: if video.downloaded == 0: video_url = self._connector.construct_video_url( video.site_id) if video.download_video(self._directory, itag_value, video_url): self.downloaded_videos.append(video) else: self.failed_videos_count += 1 self.failed_videos.append(video) else: raise stov_exceptions.SubscriptionDisabledException(self._title) def print_videos(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 add_sub(self): """Adds a new subscription to the database""" parsed_response = self._connector.parse_api_data([]) self._title = parsed_response.title self._type = parsed_response.type if self._type == "channel" or self._type == "playlist": self._directory = self._title.replace(" ", "_") else: self._directory = self._name + "_" + \ self._search.replace(" ", "_") data = (self._title, self._type, self._search, self._directory, self._name, 0, self.site) return data def check_and_delete(self, videos): """Checks if a video still exists in the current API response and deletes it if it doesn't """ parsed_response = self._connector.parse_api_data([]) self._type = parsed_response.type self.gather_videos(videos) for entry in parsed_response.videos: self._id_list.append(entry.video_id) for item in self._video_list: if item.video_id not in self._id_list: self.to_delete.append(item) def update_data(self): """Updates the data from the API.""" self.parsed_response = self._connector.parse_api_data(self._video_list)