diff --git a/dataregex.db b/dataregex.db new file mode 100644 index 0000000..65831f4 Binary files /dev/null and b/dataregex.db differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..d332f0b --- /dev/null +++ b/main.py @@ -0,0 +1,32 @@ +from model.db_model import Users +from modules.regex_operations import Regex +from modules.jsonfile_operations import Json +from modules.db_operations import Database +import argparse + +def main(): + + parser = argparse.ArgumentParser() # Create the parser + parser.add_argument('-f', '--file', required=True, help='Enter file name') # Add --file argument + parser.add_argument('-db', '--db', required=True, help='Enter database name') # Add --db argument + args = parser.parse_args() # Parse the argument + + json_path = args.jp # Set file as argument + database_path = args.dp # Set db as argument + + try: + read_json = Json(json_path) # Calls "dataregex.json" + database = Database(database_path) # Calls "dataregex.db" + user_list = read_json.create_user + database.create_table() # Create table + database.bulk_insert(user_list) + + except: + print("Transaction failed.") + + +if __name__ == '__main__': + main() + +# HELPER: You can run the following command from terminal. +# python3 main.py --jp "dataregex.json" --dp "dataregex.db" diff --git a/model/db_model.py b/model/db_model.py new file mode 100644 index 0000000..5f19372 --- /dev/null +++ b/model/db_model.py @@ -0,0 +1,16 @@ + +class Users: + + def __init__(self, email="None", username="None", name_surname="None", emailuserlk="0", + usernamelk="0", year_of_birth="0000", month_of_birth="00", day_of_birth="00", + country="None", ap="1"): + self.email = email + self.username = username + self.name_surname = name_surname + self.emailuserlk = emailuserlk + self.usernamelk = usernamelk + self.year_of_birth = year_of_birth + self.month_of_birth = month_of_birth + self.day_of_birth = day_of_birth + self.country = country + self.ap = ap diff --git a/modules/db_operations.py b/modules/db_operations.py new file mode 100644 index 0000000..2ddaa26 --- /dev/null +++ b/modules/db_operations.py @@ -0,0 +1,85 @@ +from datetime import datetime +import sqlite3 + + +class Database(): + + def __init__(self, dbName: str): + self.dbName = dbName + + def get_connection(self): + """ + This function establishes connection with database. + """ + try: + db = sqlite3.connect(self.dbName) + return db + except: + print("Connection could not be established.") + + def create_table_name(self): + """ + This function creates the names of the tables in the database. + """ + tableName = "data_" + str(datetime.today().strftime('%Y%m%d')) + return tableName + + def create_table(self): + """ + This function creates the database table. + """ + tableSql = f''' + CREATE TABLE IF NOT EXISTS "{self.tableName()}" ( + "email" text, + "username" text, + "name_surname" text, + "emailuserlk" integer, + "usernamelk" integer, + "year_of_birth" integer, + "month_of_birth" integer, + "day_of_birth" integer, + "country" text, + "ap" integer + ); + ''' + self.get_connection().execute(tableSql) + + def bulk_insert(self,user_list): + """ + This function adds the information in the user_list in bulk + """ + for user in user_list: + self.insert_data(user) + + def insert_data(self, user): + """ + This function created to insert user to database + """ + dbConnection = self.get_connection() # Try to connect to the requested database + try: + insertSql = f''' + INSERT INTO {self.tableName()}( + email, + username, + name_surname, + emailuserlk, + usernamelk, + year_of_birth, + month_of_birth, + day_of_birth, + country, + ap) + VALUES (?,?,?,?,?,?,?,?,?,?)''' + + cur = dbConnection.cursor() + cur.execute(insertSql, (user.email, user.username, user.name_surname, user.emailuserlk, user.usernamelk, + user.year_of_birth, user.month_of_birth, user.day_of_birth, user.country, + user.ap)) # Execute the given insert query + dbConnection.commit() # Commit ends a transaction within database + + except: + print("Transaction failed.") + + finally: + dbConnection.close() + diff --git a/modules/jsonfile_operations.py b/modules/jsonfile_operations.py new file mode 100644 index 0000000..8b39156 --- /dev/null +++ b/modules/jsonfile_operations.py @@ -0,0 +1,49 @@ +from modules.regex_operations import Regex +from model.db_model import Users +import json + +class Json(): + + def __init__(self, filename): + self.filename = filename + + def get_json(self): + """ + This functions reads json file + """ + with open(self.filename, "r") as json_file: + data = json.load(json_file) + + return data + + @property + def create_user(self): + """ + This functions assigns the information in the json file read to the user. + """ + + json_data = self.get_json() # Calls data from json file + rgx = Regex() # Calls regex methods + data_list = list() + + for data in json_data: + user = Users() + user.email = data["email"] + user.username = data["username"] + user.name_surname = data["profile"]["name"] + user.emailuserlk = rgx.email_validate(user.email, + user.username) + user.usernamelk = rgx.username_validate(data["username"], data["profile"][ + "name"]) + dob_raw = data["profile"]["dob"] # dob read data read as row + dob_raw = dob_raw.split("-") # line is separated by '-' + user.year_of_birth = dob_raw[0] + user.month_of_birth = dob_raw[1] + user.day_of_birth = dob_raw[2] + user.country = data.get("profile").get("address").split()[-1] # Getting city information from address + # information + + data_list.append(user) # Append created user to the data_list + + return data_list + diff --git a/modules/regex_operations.py b/modules/regex_operations.py new file mode 100644 index 0000000..8018d69 --- /dev/null +++ b/modules/regex_operations.py @@ -0,0 +1,29 @@ + + +class Regex(): + + def email_validate(self, email, username): + """ + This functions checking whether the e-mail contains the username or part of it (at least 3 letters). + """ + username = username.lower() + email = email.lower() + username = username.replace(" ", "") + for i in range(0, len(username) - 2): + substring = username[i:i + 3] + if substring in email: + return 1 + return 0 + + def username_validate(self, username, name_surname): + """ + This functions it is checked whether it contains a part of the username and the user's name or surname. + """ + name_surname = name_surname.lower() + username = username.lower() + for i in range(0, len(name_surname) - 2): + substring = name_surname[i:i + 3] + if substring in username: + return 1 + return 0 +