Browse Source

create program module, move subscription creation to program

Helmut Pozimski 6 years ago
parent
commit
6dce4a50de
2 changed files with 105 additions and 55 deletions
  1. 100 0
      lib_stov/program.py
  2. 5 55
      stov

+ 100 - 0
lib_stov/program.py

@@ -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"))

+ 5 - 55
stov

@@ -35,6 +35,7 @@ from email.mime.text import MIMEText
 from lib_stov import subscription
 from lib_stov import stov_exceptions
 from lib_stov import helpers
+from lib_stov import program
 
 
 helpers.initialize_gettext()
@@ -54,61 +55,10 @@ MAIL_CONTENT = []
 run the corresponding code
 
 """
-if ARGUMENTS.add is True:
-    if ARGUMENTS.channel is not None and ARGUMENTS.searchparameter is None:
-        NEW_SUBSCRIPTION = subscription.Sub(subscription_type="user",
-                                            name=ARGUMENTS.channel, conf=CONF)
-    elif ARGUMENTS.channel is not None and ARGUMENTS.searchparameter is not None:
-        NEW_SUBSCRIPTION = subscription.Sub(subscription_type="user",
-                                            name=ARGUMENTS.channel,
-                                            search=ARGUMENTS.searchparameter,
-                                            conf=CONF)
-    elif ARGUMENTS.channel is None and ARGUMENTS.searchparameter is not None:
-        NEW_SUBSCRIPTION = subscription.Sub(subscription_type="search",
-                                            name=_("Search_"),
-                                            search=ARGUMENTS.searchparameter,
-                                            conf=CONF)
-    elif ARGUMENTS.playlist is not None:
-        if ARGUMENTS.searchparameter is not None:
-            LOGGER.error(_("Playlists do not support searching, the search "
-                           "option will be ignored!"))
-        NEW_SUBSCRIPTION = subscription.Sub(subscription_type="playlist",
-                                            name=ARGUMENTS.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 = DB.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 DB.video_in_database(video.ytid):
-            if NEW_SUBSCRIPTION.check_string_match(video):
-                try:
-                    DB.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"))
+if ARGUMENTS.add:
+    program.add_subscription(CONF, DB, ARGUMENTS.channel,
+                             ARGUMENTS.searchparameter,
+                             ARGUMENTS.playlist)
 elif ARGUMENTS.list is True:
     LIST_OF_SUBSCRIPTIONS = DB.get_subscriptions(CONF)
     SUB_STATE = None