Browse Source

move updating of subscriptions to program

Helmut Pozimski 6 years ago
parent
commit
bccb663e99
2 changed files with 34 additions and 23 deletions
  1. 33 0
      lib_stov/program.py
  2. 1 23
      stov

+ 33 - 0
lib_stov/program.py

@@ -143,3 +143,36 @@ def delete_subscription(database, sub_id):
         sys.exit(1)
     else:
         LOGGER.info(_("Subscription deleted successfully!"))
+
+
+def update_subscriptions(database, conf):
+    """
+    Updates data about videos in a subscription.
+
+    :param conf: configuration object
+    :type conf: lib_stov.configuration.Conf
+    :param database: database object
+    :type database: lib_stov.database.Db
+    """
+    subscriptions_list = database.get_subscriptions(conf)
+    for element in subscriptions_list:
+        videos = database.get_videos(element.get_id(), conf)
+        element.gather_videos(videos)
+        try:
+            element.update_data()
+        except stov_exceptions.YoutubeAPITimeoutException as error:
+            LOGGER.error(error)
+        except stov_exceptions.NoDataFromYoutubeAPIException as error:
+            LOGGER.error(error)
+        for video in element.parsed_response.videos:
+            if not database.video_in_database(video.ytid):
+                if element.check_string_match(video):
+                    try:
+                        database.insert_video(video, element.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)

+ 1 - 23
stov

@@ -64,29 +64,7 @@ elif ARGUMENTS.list:
 elif ARGUMENTS.deleteid:
     program.delete_subscription(DB, ARGUMENTS.deleteid)
 elif ARGUMENTS.update is True:
-    SUBSCRIPTIONS_LIST = DB.get_subscriptions(CONF)
-    for element in SUBSCRIPTIONS_LIST:
-        VIDEOS = DB.get_videos(element.get_id(), CONF)
-        element.gather_videos(VIDEOS)
-        try:
-            element.update_data()
-        except stov_exceptions.YoutubeAPITimeoutException as error:
-            LOGGER.error(error)
-        except stov_exceptions.NoDataFromYoutubeAPIException as error:
-            LOGGER.error(error)
-        for video in element.parsed_response.videos:
-            if not DB.video_in_database(video.ytid):
-                if element.check_string_match(video):
-                    try:
-                        DB.insert_video(video, element.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)
-
+    program.update_subscriptions(CONF, DB)
 elif ARGUMENTS.download is True:
     SUBSCRIPTIONS_LIST = DB.get_subscriptions(CONF)
     LOGGER.debug(_("Trying to determine the itag value for youtube-dl from"