-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_DB.py
134 lines (114 loc) · 3.65 KB
/
import_DB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from multiprocessing import freeze_support
from colorama import init, Fore
import MySQLdb
import requests
import itdbloader
import pandas as pd
from tqdm import tqdm
from utils import yes_or_no
PATCH_URL="http://www.vloom.co.kr:3000/dev/music/interval"
def connect_db():
print("Loading configuration...", end=" ")
try:
config = itdbloader.get_config()
print(Fore.GREEN + "OK")
except Exception as e:
print("Please check if .itdb.config exists")
return
print("Checking MySQL connections...", end=" ")
try:
cnx = itdbloader.db_connect(config)
print(Fore.GREEN + "OK")
except MySQLdb.Error as e:
print(Fore.RED + "failed")
print(e)
print("Please check if MySQL is running, or check the configuration file")
return
c = cnx.cursor()
db_name = config.get("client", "database")
return c, db_name
def init_db(c, db_name):
"""Create itdb database
requires: root username and pw in mysql_default.json
:param c: MySQLdb cursor
:type c: MySQLdb cursor
:param db_name: name of the database
:type db_name: String
:return: True if database created successfully
:rtype: bool
"""
print("Creating database...")
result = c.execute(f"SHOW DATABASES LIKE '{db_name}';")
# if db exists
if result == 1:
ans = yes_or_no("DB already exists. Do you want to overwrite?")
if ans is True:
ans2 = yes_or_no("Are you sure you want to overwrite?")
if ans2 is True:
c.execute(f"DROP DATABASE {db_name};")
else:
return
else:
return
# create database
try:
c.execute(f"CREATE DATABASE {db_name}")
except MySQLdb.Error as e:
print(f"{Fore.RED} Failed to create database: {e} {Fore.RESET}")
return
print(f"Database created: {db_name}")
return True
def create_table(c, db_name):
"""Create tracks and playlists table in database
:param c: MySQLdb cursor
:type c: MySQLdb cursor
:param db_name: name of the database
:type db_name: String
:return: True if successful
:rtype: bool
"""
# create table
print("Creating table...", end=" ")
c.execute(f"USE {db_name};")
try:
query = open("./itdb.sql").read()
c.execute(query)
print(Fore.GREEN + "OK")
except MySQLdb.Error as e:
print(Fore.RED + "failed")
print(e)
return False
return True
def send_patch(url, json_data):
"""send HTTP PATCH with given data
:param data: csv data
:return: status code, status message
"""
r=requests.patch(url, json=json_data)
# print(str(idx), str(value), r.json())
return r.status_code, r.json()["status"]
def send_pitch(url, pitch_path):
df_pitch=pd.read_csv(pitch_path, index_col=[0])
df_pitch=df_pitch.fillna(df_pitch.mean())
print(df_pitch.head())
error_list = []
for index, row in tqdm(df_pitch.iterrows(), total=df_pitch.shape[0]):
data={"itunes_id": index, "frequency": round(row["mean"], 2)}
status_code, success=send_patch(url, data)
if not success:
error_list.append(index)
print(str(index), "Error code:"+ str(status_code), str(success))
def main():
init(autoreset=True)
c, db_name = connect_db()
init_db(c, db_name)
flag = create_table(c, db_name)
c.close() # close connection to open new one in itdbloader
if flag is False: # if create_table failed
return
# import to DB
config = itdbloader.get_config()
db_loader = itdbloader.load_itdb(config)
if __name__ == "__main__":
freeze_support()
main()