123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #
- # 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 <http://www.gnu.org/licenses/>.
- # -*- coding: utf8 -*-
- """This module takes care of managing subscriptions."""
- import logging
- from lib_stov import stov_exceptions
- from lib_stov import yt_noapi
- from lib_stov import zdf_mediathek
- from lib_stov import twitch
- LOGGER = logging.getLogger("stov")
- 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, self._search)
- elif site == "twitch":
- self._connector = twitch.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:
- LOGGER.debug(_("Matching parameter %s with title %s"),
- self._search, video.title)
- if self._search != "" and self._conf.values["check_title"]\
- == "yes":
- return self._search in video.title
- else:
- return self._search.lower() in video.title.lower()
- 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.site_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)
|