|
@@ -0,0 +1,100 @@
|
|
|
+# 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 contains the functions that make up the core of the
|
|
|
+application.
|
|
|
+"""
|
|
|
+
|
|
|
+import logging
|
|
|
+import sys
|
|
|
+
|
|
|
+from lib_stov import subscription
|
|
|
+from lib_stov import stov_exceptions
|
|
|
+
|
|
|
+LOGGER = logging.getLogger("stov")
|
|
|
+
|
|
|
+
|
|
|
+def add_subscription(conf, database, channel="", search="", playlist=""):
|
|
|
+ """
|
|
|
+ Takes care of adding a new subscription to the database.
|
|
|
+
|
|
|
+ :param conf: configuration object
|
|
|
+ :type conf: lib_stov.configuration.Conf
|
|
|
+ :param database: database object
|
|
|
+ :type database: lib_stov.database.Db
|
|
|
+ :param channel: optional channel name
|
|
|
+ :type channel: str
|
|
|
+ :param search: optional search string
|
|
|
+ :type search: str
|
|
|
+ :param playlist: optional playlist ID
|
|
|
+ :type playlist: str
|
|
|
+ """
|
|
|
+ if channel and not search:
|
|
|
+ new_subscription = subscription.Sub(subscription_type="user",
|
|
|
+ name=channel, conf=conf)
|
|
|
+ elif channel and search:
|
|
|
+ new_subscription = subscription.Sub(subscription_type="user",
|
|
|
+ name=channel,
|
|
|
+ search=search,
|
|
|
+ conf=conf)
|
|
|
+ elif not channel and search:
|
|
|
+ new_subscription = subscription.Sub(subscription_type="search",
|
|
|
+ name=_("Search_"),
|
|
|
+ search=search,
|
|
|
+ conf=conf)
|
|
|
+ elif playlist:
|
|
|
+ if search:
|
|
|
+ LOGGER.error(_("Playlists do not support searching, the search "
|
|
|
+ "option will be ignored!"))
|
|
|
+ new_subscription = subscription.Sub(subscription_type="playlist",
|
|
|
+ name=playlist,
|
|
|
+ conf=conf)
|
|
|
+ else:
|
|
|
+ LOGGER.error(_("None or invalid subscription type given, please check "
|
|
|
+ "the type option and try again."))
|
|
|
+ sys.exit(1)
|
|
|
+ try:
|
|
|
+ subscription_id = database.insert_subscription(
|
|
|
+ new_subscription.add_sub())
|
|
|
+ new_subscription.set_id(subscription_id)
|
|
|
+ except stov_exceptions.DBWriteAccessFailedException as error:
|
|
|
+ LOGGER.error(error)
|
|
|
+ sys.exit(1)
|
|
|
+ except stov_exceptions.ChannelNotFound as error:
|
|
|
+ LOGGER.error(error)
|
|
|
+ sys.exit(1)
|
|
|
+ else:
|
|
|
+ LOGGER.debug(_("Subscription sucessfully inserted into database."))
|
|
|
+ try:
|
|
|
+ new_subscription.update_data()
|
|
|
+ except stov_exceptions.YoutubeAPITimeoutException as error:
|
|
|
+ LOGGER.error(error)
|
|
|
+ except stov_exceptions.NoDataFromYoutubeAPIException as error:
|
|
|
+ LOGGER.error(error)
|
|
|
+ for video in new_subscription.parsed_response.videos:
|
|
|
+ if not database.video_in_database(video.ytid):
|
|
|
+ if new_subscription.check_string_match(video):
|
|
|
+ try:
|
|
|
+ database.insert_video(video, new_subscription.get_id())
|
|
|
+ except stov_exceptions.DBWriteAccessFailedException as error:
|
|
|
+ LOGGER.error(error)
|
|
|
+ sys.exit(1)
|
|
|
+ else:
|
|
|
+ LOGGER.debug(_("Video %s successfully inserted into "
|
|
|
+ "database."), video.title)
|
|
|
+ LOGGER.info(_("New subscription ") + new_subscription.get_title() +
|
|
|
+ _(" successfully added"))
|