Browse Source

implement removal of no longer supported sites

Helmut Pozimski 6 years ago
parent
commit
49e0d3c63d
3 changed files with 36 additions and 2 deletions
  1. 31 0
      lib_stov/database.py
  2. 1 1
      lib_stov/main.py
  3. 4 1
      lib_stov/program.py

+ 31 - 0
lib_stov/database.py

@@ -312,6 +312,23 @@ class Db(object):
         insert_statement = "INSERT INTO sites (title) VALUES (?)"
         self._execute_statement(insert_statement, (name,))
 
+    def remove_site(self, name):
+        """
+        Removes a site with the specified name to the database.
+
+        :param name: name of the new site
+        :type site: str
+        """
+        site_id = self.get_site_id(name)
+        subscriptions = self._get_subscriptions_by_site_id(site_id)
+        delete_videos = "DELETE FROM videos WHERE subscription_id = ?"
+        delete_subscription = "DELETE FROM SUBSCRIPTIONS WHERE id=?"
+        for sub in subscriptions:
+            self._execute_statement(delete_videos, (sub,))
+            self._execute_statement(delete_subscription, (sub,))
+        delete_site = "DELETE FROM sites WHERE id=?"
+        self._execute_statement(delete_site, (site_id,))
+
     def get_site_id(self, name):
         """
         Get the ID of a specific site
@@ -337,3 +354,17 @@ class Db(object):
         cursor = self._execute_statement(query)
         result = cursor.fetchall()
         return result
+
+    def _get_subscriptions_by_site_id(self, site_id):
+        """
+        Retrieves all subscriptions associated with the specified site_id
+        from the database.
+
+        :param site_id: ID of the site
+        :type site_id: int
+        :return: list of subscriptions associated with the site_id
+        :rtype: tuple
+        """
+        query = "SELECT id FROM subscriptions WHERE site=?"
+        cursor = self._execute_statement(query, (site_id,))
+        return cursor.fetchall()

+ 1 - 1
lib_stov/main.py

@@ -71,7 +71,7 @@ def main():
     elif arguments.license:
         program.print_license()
     elif arguments.version:
-        logger.info("1.1.1")
+        logger.info("1.2")
     else:
         parser.print_help()
     helpers.remove_lock()

+ 4 - 1
lib_stov/program.py

@@ -509,7 +509,7 @@ def initialize_sites(database):
     :param database: database object
     :type database: lib_stov.database.Db
     """
-    supported_sites = ["youtube", "zdf_mediathek", "twitch", "vidme"]
+    supported_sites = ["youtube", "zdf_mediathek", "twitch"]
     sites = database.get_sites()
     for site in supported_sites:
         site_found = False
@@ -518,6 +518,9 @@ def initialize_sites(database):
                 site_found = True
         if not site_found:
             database.add_site(site)
+    for site in sites:
+        if site[1] not in supported_sites:
+            database.remove_site(site[1])
 
 
 def list_sites(database):