-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_data.py
203 lines (175 loc) · 5.43 KB
/
insert_data.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import pandas as pd
import mysql.connector
import random
import string
my_connection = mysql.connector.connect(
user="root", password="harshanand31", host="localhost", database="MusicTest"
)
cursor_object = my_connection.cursor()
df = pd.read_csv("./data.csv")
def generate_random_dob(start="1950-01-01", end="2000-01-01"):
return pd.to_datetime(random.choice(pd.date_range(start=start, end=end)))
def generate_random_release(start="2000-01-01", end="2020-01-01"):
return pd.to_datetime(random.choice(pd.date_range(start=start, end=end)))
def generate_random_phone_number():
area_code = random.randint(100, 999)
central_office_code = random.randint(100, 999)
line_number = random.randint(1000, 9999)
return f"({area_code}) {central_office_code}-{line_number}"
addresses = [
"1234 Elm Street, Springfield, IL 62704",
"5678 Maple Avenue, Metropolis, NY 10001",
"9101 Oak Lane, Gotham, NJ 07030",
"1122 Pine Drive, Smallville, KS 66002",
"1314 Birch Boulevard, Star City, CA 90210",
"1516 Cedar Circle, Central City, CO 80022",
"1718 Redwood Road, Coast City, OR 97330",
"1920 Willow Way, Keystone, FL 33556",
"2122 Aspen Avenue, Blüdhaven, TX 75001",
"2324 Spruce Street, Fawcett City, WA 98001",
]
music_streaming_platforms = [
"Spotify",
"Apple Music",
"Amazon Music",
"YouTube Music",
"Tidal",
"Pandora",
"Deezer",
"SoundCloud",
"iHeartRadio",
"Napster",
"Google Play Music",
"Qobuz",
"Bandcamp",
"Audiomack",
"Anghami",
"JioSaavn",
"Wynk Music",
"Gaana",
"Tencent Music",
"KKBOX",
]
# Artists
artist_ids = {}
i = 1
for _, artist in df[["Artist Name(s)"]].drop_duplicates().iterrows():
artist_name = str(artist.values[0]).split(",")[0]
artist_ids[artist_name] = i
dob = generate_random_dob()
country = random.choice(["USA", "UK", "France", "Germany", "Brazil"])
query = "insert into Artists values (%s, %s, %s, %s)"
values = [i, artist_name, dob, country]
# cursor_object.execute(query, values)
i += 1
# Albums
album_ids = {}
i = 1
for _, row in (
df[
[
"Album Name",
"Album Artist Name(s)",
]
]
.drop_duplicates()
.iterrows()
):
album_name = row["Album Name"]
artist_name = str(row["Album Artist Name(s)"]).split(",")[0]
release_date = generate_random_release()
album_ids[album_name] = i
artist_id = artist_ids.get(artist_name)
if not artist_id:
continue
query = "insert into Albums values (%s, %s, %s, %s)"
values = [i, artist_id, album_name, release_date]
# try:
# cursor_object.execute(query, values)
# except:
# # skip
# pass
i += 1
# Genres
genre_ids = {}
i = 1
for _, row in df[["Artist Genres"]].drop_duplicates().dropna().iterrows():
genre = str(row["Artist Genres"]).split(",")[0]
if genre in genre_ids or not genre or genre == "":
continue
genre_ids[genre] = i
country = random.choice(["USA", "UK", "France", "Germany", "Brazil"])
query = "insert into Genres values (%s, %s, %s)"
values = [i, genre, country]
# cursor_object.execute(query, values)
i += 1
# RecordLabel
label_ids = {}
i = 1
for _, row in df[["Label"]].drop_duplicates().dropna().iterrows():
label = str(row["Label"]).split(",")[0]
label_ids[label] = i
address = random.choice(addresses)
phone = generate_random_phone_number()
query = "insert into RecordLabels values (%s, %s, %s, %s)"
values = [i, label, phone, address]
# cursor_object.execute(query, values)
i += 1
# Streaming platform
platform_ids = {}
i = 1
for platform in music_streaming_platforms:
platform_ids[platform] = i
subs = random.randint(1000000, 100000000)
sub_fee = round(random.uniform(5.99, 14.99), 2)
query = "insert into StreamingPlatforms values (%s, %s, %s, %s)"
values = [i, platform, subs, sub_fee]
# cursor_object.execute(query, values)
i += 1
# Songs
song_ids = {}
i = 1
for _, row in (
df[
[
"Track Duration (ms)",
"Track Name",
"Album Name",
"Artist Name(s)",
"Artist Genres",
"Label",
]
]
.drop_duplicates()
.dropna()
.iterrows()
):
duration = round(float(row["Track Duration (ms)"]) / 1000)
name = row["Track Name"]
album_id = album_ids.get(row["Album Name"])
artist_id = artist_ids.get(str(row["Artist Name(s)"]).split(",")[0])
genre_id = genre_ids.get(str(row["Artist Genres"]).split(",")[0])
label_id = label_ids.get(row["Label"])
if not album_id or not artist_id or not genre_id or not label_id:
continue
song_ids[name] = i
query = "insert into Songs values (%s, %s, %s, %s, %s, %s, %s)"
values = [i, duration, name, genre_id, label_id, album_id, artist_id]
# cursor_object.execute(query, values)
i += 1
# Streams
for song_id in song_ids.values():
for platform_id in platform_ids.values():
streams = random.randint(100000, 10000000)
query = "insert into Streams values (%s, %s, %s)"
values = [platform_id, song_id, streams]
# cursor_object.execute(query, values)
# Compose
query = "SELECT TrackID, ArtistID FROM Songs;"
cursor_object.execute(query)
mapping = cursor_object.fetchall()
for vals in mapping:
query = "insert into Compose values (%s, %s)"
values = [vals[1], vals[0]]
cursor_object.execute(query, values)
my_connection.commit()