|
@@ -1,4 +1,4 @@
|
|
|
-# This file is part of stov, written by Helmut Pozimski 2012-2015.
|
|
|
+# 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
|
|
@@ -18,11 +18,14 @@
|
|
|
"""This module is responsible for all database related operations."""
|
|
|
|
|
|
import sqlite3
|
|
|
+import logging
|
|
|
|
|
|
from lib_stov import stov_exceptions
|
|
|
from lib_stov import youtube
|
|
|
from lib_stov import subscription
|
|
|
|
|
|
+LOGGER = logging.getLogger("stov")
|
|
|
+
|
|
|
|
|
|
class Db(object):
|
|
|
"""This class is used to cosntruct the module which will take care of all
|
|
@@ -53,11 +56,12 @@ class Db(object):
|
|
|
"""Executes a statement, works as a wrapper around cursor execute."""
|
|
|
|
|
|
try:
|
|
|
- if argument is None:
|
|
|
+ if not argument:
|
|
|
result = self.__cursor.execute(statement)
|
|
|
else:
|
|
|
result = self.__cursor.execute(statement, argument)
|
|
|
- except sqlite3.OperationalError:
|
|
|
+ except sqlite3.OperationalError as error:
|
|
|
+ LOGGER.debug(error)
|
|
|
raise stov_exceptions.DBWriteAccessFailedException()
|
|
|
else:
|
|
|
self.__connection.commit()
|
|
@@ -73,7 +77,8 @@ class Db(object):
|
|
|
type TEXT,
|
|
|
searchstring TEXT,
|
|
|
directory TEXT,
|
|
|
- disabled INTEGER DEFAULT 0
|
|
|
+ disabled INTEGER DEFAULT 0,
|
|
|
+ site INTEGER NOT NULL
|
|
|
);""")
|
|
|
self._execute_statement("""CREATE TABLE videos (
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
@@ -83,6 +88,9 @@ class Db(object):
|
|
|
downloaded INT,
|
|
|
failcnt INTEGER DEFAULT 0
|
|
|
);""")
|
|
|
+ self._execute_statement("""CREATE TABLE sites(
|
|
|
+ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
|
+ title TEXT);""")
|
|
|
|
|
|
def update(self):
|
|
|
"""Updates the database structure to match the current version"""
|
|
@@ -99,10 +107,8 @@ class Db(object):
|
|
|
self._execute_statement("UPDATE subscriptions SET disabled=0;")
|
|
|
self.__version = 3
|
|
|
if int(self.__version) == 3:
|
|
|
- """
|
|
|
- Pseudo version without changes to the database structure,
|
|
|
- converts existing channel subscriptions into user ones.
|
|
|
- """
|
|
|
+ # Pseudo version without changes to the database structure,
|
|
|
+ # converts existing channel subscriptions into user ones.
|
|
|
self._execute_statement("UPDATE subscriptions SET type='user' "
|
|
|
"WHERE type='channel'")
|
|
|
self.__version = 4
|
|
@@ -120,8 +126,35 @@ class Db(object):
|
|
|
" id, title, ytid, subscription_id, "
|
|
|
"downloaded, failcnt FROM videos;")
|
|
|
self._execute_statement("DROP TABLE videos;")
|
|
|
- self._execute_statement("ALTER TABLE videos_backup RENAME TO videos;")
|
|
|
+ self._execute_statement("ALTER TABLE videos_backup RENAME TO "
|
|
|
+ "videos;")
|
|
|
self.__version = 5
|
|
|
+ if int(self.__version) == 5:
|
|
|
+ self._execute_statement("""CREATE TABLE sites(
|
|
|
+ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
|
+ title TEXT);""")
|
|
|
+ self._execute_statement("""ALTER TABLE subscriptions ADD
|
|
|
+ COLUMN site INTEGER;""")
|
|
|
+ self.add_site("youtube")
|
|
|
+ self._execute_statement("""UPDATE subscriptions SET site=1;""")
|
|
|
+ self._execute_statement("""CREATE TABLE subscriptions_backup (
|
|
|
+ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
|
+ title TEXT,
|
|
|
+ name TEXT,
|
|
|
+ type TEXT,
|
|
|
+ searchstring TEXT,
|
|
|
+ directory TEXT,
|
|
|
+ disabled INTEGER DEFAULT 0,
|
|
|
+ site INTEGER NOT NULL
|
|
|
+ );""")
|
|
|
+ self._execute_statement("INSERT INTO subscriptions_backup SELECT"
|
|
|
+ " id, title, name, type, "
|
|
|
+ "searchstring, directory, disabled, "
|
|
|
+ "site FROM subscriptions;")
|
|
|
+ self._execute_statement("DROP TABLE subscriptions;")
|
|
|
+ self._execute_statement("ALTER TABLE subscriptions_backup "
|
|
|
+ "RENAME TO subscriptions;")
|
|
|
+ self.__version = 6
|
|
|
|
|
|
def get_version(self):
|
|
|
"""Simple getter method, returns the DB version"""
|
|
@@ -186,9 +219,9 @@ class Db(object):
|
|
|
|
|
|
def insert_subscription(self, data):
|
|
|
"""Inserts a subscription with the given data into the database"""
|
|
|
- subscription_insert = "INSERT INTO subscriptions (title, type, " \
|
|
|
- "searchstring, directory, name, disabled) " \
|
|
|
- "VALUES (?, ?, ?, ?, ?, ?)"
|
|
|
+ subscription_insert = """INSERT INTO subscriptions (title, type,
|
|
|
+ searchstring, directory, name, disabled, site)
|
|
|
+ VALUES (?, ?, ?, ?, ?, ?, ?)"""
|
|
|
self._execute_statement(subscription_insert, data)
|
|
|
subscription_getid = "SELECT id from subscriptions where title=?"
|
|
|
query_cursor = self._execute_statement(subscription_getid, (data[0],))
|
|
@@ -217,8 +250,10 @@ class Db(object):
|
|
|
|
|
|
def get_subscription(self, sub_id):
|
|
|
"""Retrieves a specific subscription from the database"""
|
|
|
- sub_query = "SELECT id,title,type,name,searchstring, directory," \
|
|
|
- "disabled FROM subscriptions where id=?"
|
|
|
+ sub_query = """SELECT subscriptions.id, subscriptions.title,
|
|
|
+ type, name,searchstring, directory,disabled, sites.title FROM
|
|
|
+ subscriptions INNER JOIN sites ON subscriptions.site=sites.id WHERE
|
|
|
+ subscriptions.id=?"""
|
|
|
result_cursor = self._execute_statement(sub_query, (sub_id,))
|
|
|
result = result_cursor.fetchall()
|
|
|
return result
|
|
@@ -242,15 +277,16 @@ class Db(object):
|
|
|
def get_subscriptions(self, conf):
|
|
|
"""Retrieves all subscriptions from the database"""
|
|
|
subscriptions_list = []
|
|
|
- subscriptions_query = "SELECT id,title,type,name,searchstring," \
|
|
|
- "directory,disabled FROM subscriptions"
|
|
|
+ subscriptions_query = """SELECT subscriptions.id, subscriptions.title,
|
|
|
+ type, name,searchstring, directory,disabled, sites.title FROM
|
|
|
+ subscriptions INNER JOIN sites ON subscriptions.site=sites.id;"""
|
|
|
result_cursor = self._execute_statement(subscriptions_query)
|
|
|
result = result_cursor.fetchall()
|
|
|
for sub in result:
|
|
|
subscriptions_list.append(subscription.Sub(
|
|
|
subscription_id=sub[0], title=sub[1], subscription_type=sub[2],
|
|
|
name=sub[3], search=sub[4], directory=sub[5], disabled=sub[6],
|
|
|
- conf=conf))
|
|
|
+ site=sub[7], conf=conf))
|
|
|
return subscriptions_list
|
|
|
|
|
|
def vacuum(self):
|
|
@@ -264,3 +300,39 @@ class Db(object):
|
|
|
"""
|
|
|
update_statement = "UPDATE subscriptions SET disabled=? WHERE id=?"
|
|
|
self._execute_statement(update_statement, (state, sub_id))
|
|
|
+
|
|
|
+ def add_site(self, name):
|
|
|
+ """
|
|
|
+ Adds a site with the specified name to the database.
|
|
|
+
|
|
|
+ :param name: name of the new site
|
|
|
+ :type site: str
|
|
|
+ """
|
|
|
+ insert_statement = "INSERT INTO sites (title) VALUES (?)"
|
|
|
+ self._execute_statement(insert_statement, (name,))
|
|
|
+
|
|
|
+ def get_site_id(self, name):
|
|
|
+ """
|
|
|
+ Get the ID of a specific site
|
|
|
+
|
|
|
+ :param name: name of the new site
|
|
|
+ :type site: str
|
|
|
+ :return: the site ID
|
|
|
+ :rtype: int
|
|
|
+ """
|
|
|
+ query = "SELECT id FROM sites WHERE title=?"
|
|
|
+ cursor = self._execute_statement(query, (name,))
|
|
|
+ result = cursor.fetchone()[0]
|
|
|
+ return result
|
|
|
+
|
|
|
+ def get_sites(self):
|
|
|
+ """
|
|
|
+ Retrieves all sites from the database.
|
|
|
+
|
|
|
+ :return: list of sites with their respective IDs
|
|
|
+ :rtype: tuple
|
|
|
+ """
|
|
|
+ query = "SELECT id,title FROM sites"
|
|
|
+ cursor = self._execute_statement(query)
|
|
|
+ result = cursor.fetchall()
|
|
|
+ return result
|