From a779c54dc3834d52a982eefda9251859615384b3 Mon Sep 17 00:00:00 2001 From: Qulu Date: Sun, 5 Aug 2018 22:37:07 +0400 Subject: [PATCH 1/4] =?UTF-8?q?Solve=20misbehaviours=20off=20#2605=20?= =?UTF-8?q?=F0=9F=90=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- instapy/database_engine.py | 104 +++++++++++++++++++++++-------------- instapy/instapy.py | 4 +- instapy/settings.py | 5 -- 3 files changed, 68 insertions(+), 45 deletions(-) diff --git a/instapy/database_engine.py b/instapy/database_engine.py index b77f222f4..a4f6ab726 100644 --- a/instapy/database_engine.py +++ b/instapy/database_engine.py @@ -3,11 +3,17 @@ 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 +22,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, table): + if "profiles" in table: + cursor.execute(SQL_CREATE_PROFILE_TABLE) -def create_record_activity_table(cursor): - cursor.execute(SQL_CREATE_RECORD_ACTIVITY_TABLE) + if "recordActivity" in table: + cursor.execute(SQL_CREATE_RECORD_ACTIVITY_TABLE) + if "followRestriction" in table: + 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: @@ -100,8 +116,8 @@ def update_profile_settings(name, address, logger): if profile is None: profile = insert_profile(conn, cursor, name) - - Settings.update_settings_with_profile(dict(profile)) + # 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,6 +127,15 @@ def update_profile_settings(name, address, logger): # close the open connection conn.close() + profile = dict(profile) + id = profile["id"] + + # assign the id to its child in `Settings` class + Settings.profile["id"] = id + + return id + + def insert_profile(conn, cursor, name): cursor.execute(INSERT_INTO_PROFILE, (name,)) @@ -118,12 +143,15 @@ def insert_profile(conn, cursor, name): 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 31c49f9ce..7274dec96 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 @@ -186,7 +186,7 @@ def __init__(self, # Assign logger self.logger = self.get_instapy_logger(self.show_logs) - initialize_database() + get_database(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"] From d672de6976d43e23267b60a437776c12f659df2d Mon Sep 17 00:00:00 2001 From: Qulu Date: Mon, 6 Aug 2018 09:40:02 +0400 Subject: [PATCH 2/4] insert_profile is for inserting, it should not return selected value The logic is mixed in there. Future maintainers will not get the idea. --- instapy/database_engine.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/instapy/database_engine.py b/instapy/database_engine.py index a4f6ab726..d1ec0ea4d 100644 --- a/instapy/database_engine.py +++ b/instapy/database_engine.py @@ -115,7 +115,7 @@ def get_profile(name, address, logger): profile = select_profile_by_username(cursor, name) if profile is None: - profile = insert_profile(conn, cursor, name) + add_profile(conn, cursor, name) # reselect the table after adding data to get the proper `id` profile = select_profile_by_username(cursor, name) @@ -137,14 +137,10 @@ def get_profile(name, address, logger): -def insert_profile(conn, cursor, name): +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 From b8bcc4a40aed64d1a44a477878ea52d83457e50d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gro=C3=9Fmann?= Date: Wed, 8 Aug 2018 08:50:00 +0200 Subject: [PATCH 3/4] chang table to tables --- instapy/database_engine.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/instapy/database_engine.py b/instapy/database_engine.py index d1ec0ea4d..84793879f 100644 --- a/instapy/database_engine.py +++ b/instapy/database_engine.py @@ -5,7 +5,6 @@ - SELECT_FROM_PROFILE_WHERE_NAME = "SELECT * FROM profiles WHERE name = :name" INSERT_INTO_PROFILE = "INSERT INTO profiles (name) VALUES (?)" @@ -72,14 +71,14 @@ def create_database(address, logger, name): -def create_tables(cursor, table): - if "profiles" in table: +def create_tables(cursor, tables): + if "profiles" in tables: cursor.execute(SQL_CREATE_PROFILE_TABLE) - if "recordActivity" in table: + if "recordActivity" in tables: cursor.execute(SQL_CREATE_RECORD_ACTIVITY_TABLE) - if "followRestriction" in table: + if "followRestriction" in tables: cursor.execute(SQL_CREATE_FOLLOW_RESTRICTION_TABLE) From c7d9e852992df84a5fa760da2c2a1fcddd7046db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gro=C3=9Fmann?= Date: Wed, 8 Aug 2018 08:51:31 +0200 Subject: [PATCH 4/4] add parameter name to get_database call --- instapy/instapy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instapy/instapy.py b/instapy/instapy.py index 7274dec96..942e9a8d7 100644 --- a/instapy/instapy.py +++ b/instapy/instapy.py @@ -186,7 +186,7 @@ def __init__(self, # Assign logger self.logger = self.get_instapy_logger(self.show_logs) - get_database(True) + get_database(make=True) if self.selenium_local_session == True: self.set_selenium_local_session()