Browse Source

move database cleanup to program

Helmut Pozimski 6 years ago
parent
commit
b98ab85ba8
2 changed files with 31 additions and 18 deletions
  1. 29 0
      lib_stov/program.py
  2. 2 18
      stov

+ 29 - 0
lib_stov/program.py

@@ -365,3 +365,32 @@ def catchup(database, sub_id):
         else:
             LOGGER.error(_("The subscription could not be updated, "
                            "please check if the ID given is correct."))
+
+
+def clean_database(database, conf):
+    """
+    Initiates a database cleanup, deleting all videos that are no longer
+    in the scope of the query and vacuuming the database to free up space.
+
+    :param database: database object
+    :type database: lib_stov.database.Db
+    :param conf: configuration object
+    :type conf: lib_stov.configuration.Conf
+    """
+    subscription_list = database.get_subscriptions(conf)
+    for element in subscription_list:
+        videos = database.get_videos(element.get_id(), conf)
+        element.check_and_delete(videos)
+        for delete_video in element.to_delete:
+            LOGGER.debug(_("Deleting video %s from "
+                           "database"), delete_video.title)
+            try:
+                database.delete_video(delete_video.get_id())
+            except stov_exceptions.DBWriteAccessFailedException as error:
+                LOGGER.error(error)
+                sys.exit(1)
+    try:
+        database.vacuum()
+    except stov_exceptions.DBWriteAccessFailedException as error:
+        LOGGER.error(error)
+        sys.exit(1)

+ 2 - 18
stov

@@ -83,24 +83,8 @@ elif ARGUMENTS.subscriptionid:
     program.list_videos(DB, CONF, ARGUMENTS.subscriptionid)
 elif ARGUMENTS.catchup:
     program.catchup(DB, ARGUMENTS.catchup)
-elif ARGUMENTS.cleanup is True:
-    SUBSCRIPTION_LIST = DB.get_subscriptions(CONF)
-    for element in SUBSCRIPTION_LIST:
-        VIDEOS = DB.get_videos(element.get_id(), CONF)
-        element.check_and_delete(VIDEOS)
-        for delete_video in element.to_delete:
-            LOGGER.debug(_("Deleting video %s from "
-                           "database"), delete_video.title)
-            try:
-                DB.delete_video(delete_video.get_id())
-            except stov_exceptions.DBWriteAccessFailedException as error:
-                LOGGER.error(error)
-                sys.exit(1)
-    try:
-        DB.vacuum()
-    except stov_exceptions.DBWriteAccessFailedException as error:
-        LOGGER.error(error)
-        sys.exit(1)
+elif ARGUMENTS.cleanup:
+    program.clean_database(DB, CONF)
 elif ARGUMENTS.enableid is not None:
     SUBSCRIPTION_STATE = DB.get_subscription(ARGUMENTS.enableid)
     try: