Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback #1

Open
wants to merge 7 commits into
base: feedback
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added dataregex.db
Binary file not shown.
32 changes: 32 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 16 additions & 0 deletions model/db_model.py
Original file line number Diff line number Diff line change
@@ -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
85 changes: 85 additions & 0 deletions modules/db_operations.py
Original file line number Diff line number Diff line change
@@ -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()

49 changes: 49 additions & 0 deletions modules/jsonfile_operations.py
Original file line number Diff line number Diff line change
@@ -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

29 changes: 29 additions & 0 deletions modules/regex_operations.py
Original file line number Diff line number Diff line change
@@ -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