123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #
- # This file is part of stov, written by Helmut Pozimski 2012-2015.
- #
- # 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 <http://www.gnu.org/licenses/>.
- # -*- coding: utf8 -*-
- """This module takes care of managing subscriptions."""
- from lib_stov import stov_exceptions
- from lib_stov import noapi
- 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, 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.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
- self._connector = noapi.Connector(self._type, self._name,
- self._conf, self._search)
- 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":
- if self._search in video.title:
- return True
- else:
- return False
- 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, 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.download_video(self._directory, itag_value,
- self._conf.values["videocodec"]):
- 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._directory = self._name + "_" + self._search.replace(" ", "_")
- data = (self._title, self._type, self._search, self._directory,
- self._name, 0)
- 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.gather_videos(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.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)
|