diff --git a/instapy/database_engine.py b/instapy/database_engine.py index b77f222f4..84793879f 100644 --- a/instapy/database_engine.py +++ b/instapy/database_engine.py @@ -3,11 +3,16 @@ from .settings import Settings + + SELECT_FROM_PROFILE_WHERE_NAME = "SELECT * FROM profiles WHERE name = :name" + INSERT_INTO_PROFILE = "INSERT INTO profiles (name) VALUES (?)" + SQL_CREATE_PROFILE_TABLE = """CREATE TABLE IF NOT EXISTS `profiles` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT NOT NULL);""" + SQL_CREATE_RECORD_ACTIVITY_TABLE = """CREATE TABLE IF NOT EXISTS `recordActivity` ( `profile_id` INTEGER REFERENCES `profiles` (id), `likes` SMALLINT UNSIGNED NOT NULL, @@ -16,80 +21,90 @@ `unfollows` SMALLINT UNSIGNED NOT NULL, `server_calls` INT UNSIGNED NOT NULL, `created` DATETIME NOT NULL);""" + SQL_CREATE_FOLLOW_RESTRICTION_TABLE = """CREATE TABLE IF NOT EXISTS `followRestriction` ( `profile_id` INTEGER REFERENCES `profiles` (id), `username` TEXT NOT NULL, `times` TINYINT UNSIGNED NOT NULL);""" -def get_database(): - _id = Settings.profile["id"] + +def get_database(make=False): address = Settings.database_location - return address, _id + logger = Settings.logger + credentials = Settings.profile + id, name = credentials["id"], credentials['name'] + address = validate_database_address() + + if not os.path.isfile(address) or make: + create_database(address, logger, name) + + id = get_profile(name, address, logger) if id is None or make else id + + return address, id -def initialize_database(): - logger = Settings.logger - name = Settings.profile['name'] - address = validate_database_file_address() - create_database_directories(address) - create_database(address, logger, name) - update_profile_settings(name, address, logger) def create_database(address, logger, name): - if not os.path.isfile(address): - try: - connection = sqlite3.connect(address) - with connection: - connection.row_factory = sqlite3.Row - cursor = connection.cursor() + try: + connection = sqlite3.connect(address) + with connection: + connection.row_factory = sqlite3.Row + cursor = connection.cursor() - create_profiles_table(cursor) - create_record_activity_table(cursor) - create_follow_restriction_table(cursor) + create_tables(cursor, ["profiles", + "recordActivity", + "followRestriction" + ]) - connection.commit() + connection.commit() - except Exception as exc: - logger.warning( - "Wah! Error occured while getting a DB for '{}':\n\t{}".format(name, str(exc).encode("utf-8"))) + except Exception as exc: + logger.warning( + "Wah! Error occured while getting a DB for '{}':\n\t{}".format(name, str(exc).encode("utf-8"))) - finally: - if connection: - # close the open connection - connection.close() + finally: + if connection: + # close the open connection + connection.close() -def create_follow_restriction_table(cursor): - cursor.execute(SQL_CREATE_FOLLOW_RESTRICTION_TABLE) +def create_tables(cursor, tables): + if "profiles" in tables: + cursor.execute(SQL_CREATE_PROFILE_TABLE) -def create_record_activity_table(cursor): - cursor.execute(SQL_CREATE_RECORD_ACTIVITY_TABLE) + if "recordActivity" in tables: + cursor.execute(SQL_CREATE_RECORD_ACTIVITY_TABLE) + if "followRestriction" in tables: + cursor.execute(SQL_CREATE_FOLLOW_RESTRICTION_TABLE) -def create_profiles_table(cursor): - cursor.execute(SQL_CREATE_PROFILE_TABLE) -def create_database_directories(address): +def verify_database_directories(address): db_dir = os.path.dirname(address) if not os.path.exists(db_dir): os.makedirs(db_dir) -def validate_database_file_address(): + +def validate_database_address(): address = Settings.database_location if not address.endswith(".db"): slash = "\\" if "\\" in address else "/" address = address if address.endswith(slash) else address + slash address += "instapy.db" Settings.database_location = address + + verify_database_directories(address) + return address -def update_profile_settings(name, address, logger): + +def get_profile(name, address, logger): try: conn = sqlite3.connect(address) with conn: @@ -99,9 +114,9 @@ def update_profile_settings(name, address, logger): profile = select_profile_by_username(cursor, name) if profile is None: - profile = insert_profile(conn, cursor, name) - - Settings.update_settings_with_profile(dict(profile)) + add_profile(conn, cursor, name) + # reselect the table after adding data to get the proper `id` + profile = select_profile_by_username(cursor, name) except Exception as exc: logger.warning("Heeh! Error occured while getting a DB profile for '{}':\n\t{}".format(name, str(exc).encode("utf-8"))) @@ -111,19 +126,27 @@ def update_profile_settings(name, address, logger): # close the open connection conn.close() + profile = dict(profile) + id = profile["id"] -def insert_profile(conn, cursor, name): + # assign the id to its child in `Settings` class + Settings.profile["id"] = id + + return id + + + +def add_profile(conn, cursor, name): cursor.execute(INSERT_INTO_PROFILE, (name,)) # commit the latest changes conn.commit() - # reselect the table after adding data to get the proper `id` - profile = select_profile_by_username(cursor, name) - return profile + def select_profile_by_username(cursor, name): cursor.execute(SELECT_FROM_PROFILE_WHERE_NAME, {"name": name}) profile = cursor.fetchone() + return profile diff --git a/instapy/instapy.py b/instapy/instapy.py index 955d81339..ae9fe0384 100644 --- a/instapy/instapy.py +++ b/instapy/instapy.py @@ -59,7 +59,7 @@ from .relationship_tools import get_nonfollowers from .relationship_tools import get_fans from .relationship_tools import get_mutual_following -from .database_engine import initialize_database +from .database_engine import get_database @@ -187,7 +187,7 @@ def __init__(self, # Assign logger self.logger = self.get_instapy_logger(self.show_logs) - initialize_database() + get_database(make=True) if self.selenium_local_session == True: self.set_selenium_local_session() diff --git a/instapy/settings.py b/instapy/settings.py index 5923ff2c6..00db89045 100644 --- a/instapy/settings.py +++ b/instapy/settings.py @@ -14,8 +14,3 @@ class Settings: logger = None # Set current profile credentials for DB operations profile = {"id":None, "name":None} - - @staticmethod - def update_settings_with_profile(profile): - Settings.profile["id"] = profile["id"] - Settings.profile["name"] = profile["name"]