-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheventism.py
More file actions
193 lines (186 loc) · 7.12 KB
/
eventism.py
File metadata and controls
193 lines (186 loc) · 7.12 KB
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
import mysql.connector
import random
import json
from datetime import datetime, timedelta
from config_template import db_config
# Step 1: Connect to the MySQL database
def connect_to_database():
return db_config
# Step 2: Fetch the data from the database
def fetch_data_from_database(connection):
cursor = connection.cursor(dictionary=True)
cursor.execute("SELECT * FROM songs")
return cursor.fetchall()
# Step 3: Generate random events
def generate_random_events(num_events):
events = []
event_types = {"streamed song": 65,
"added to playlist": 10,
"removed from playlist":5,
"liked song": 15,
"disliked song": 5
}
age_distribution = {
"18-24 years old": 30,
"25-34 years old": 32,
"35-44 years old": 17,
"45-54 years old": 13,
"55-64 years old": 8
}
gender_distribution = {
"Male": 55,
"Female": 40,
"Non-binary": 5
}
main_genre_distribution = {
"Hip-Hop/R&B": 26.6,
"Rock": 16.2,
"Pop": 12.6,
"Country": 11.6,
"Latin": 5.8,
"Electronic/Dance": 4.9,
"Classical": 3.1,
"Jazz": 2.3,
"Indie/Alternative": 2.1,
"Other": 14.8 # Includes various other genres
}
paid_user_probability = 0.578
platform_distribution = {
"Spotify": 36,
"Apple Music": 30.7,
"Amazon Music": 23.8,
"YouTube Music": 6.8,
"Pandora Premium": 1.9,
"Tidal": 0.5,
"SoundCloud": 0.3
}
state_distribution = {
"Alabama": 1,
"Alaska": 1,
"Arizona": 2,
"Arkansas": 1,
"California": 14,
"Colorado": 2,
"Connecticut": 1,
"Delaware": 1,
"Florida": 7,
"Georgia": 4,
"Hawaii": 1,
"Idaho": 1,
"Illinois": 5,
"Indiana": 2,
"Iowa": 1,
"Kansas": 1,
"Kentucky": 1,
"Louisiana": 1,
"Maine": 1,
"Maryland": 2,
"Massachusetts": 2,
"Michigan": 3,
"Minnesota": 2,
"Mississippi": 1,
"Missouri": 2,
"Montana": 1,
"Nebraska": 1,
"Nevada": 1,
"New Hampshire": 1,
"New Jersey": 3,
"New Mexico": 1,
"New York": 8,
"North Carolina": 3,
"North Dakota": 1,
"Ohio": 4,
"Oklahoma": 1,
"Oregon": 1,
"Pennsylvania": 5,
"Rhode Island": 1,
"South Carolina": 1,
"South Dakota": 1,
"Tennessee": 2,
"Texas": 10,
"Utah": 1,
"Vermont": 1,
"Virginia": 2,
"Washington": 2,
"Washington D.C.": 1,
"West Virginia": 1,
"Wisconsin": 2,
"Wyoming": 1
}
system_distribution = {
"Android": 28.28,
"iOS": 70.92,
"Windows": 0.41,
"macOS": 0.20,
"Other": 0.19
}
page_distribution = {
"Home/Discover Page": 35,
"Now Playing Page": 25,
"Library/My Music Page": 15,
"Search Page": 10,
"Playlist Page": 5,
"Artist Page": 3,
"Album Page": 2,
"Trending/Charts Page": 2,
"Settings Page": 1,
"Profile Page": 1,
"Genre/Category Page": 1
}
for _ in range(num_events):
event = {
"event_type": random.choices(list(event_types.keys()), weights=event_types.values())[0],
"timestamp": (datetime.now() - timedelta(days=random.randint(0, 365))).isoformat(),
"user": {
"id": random.randint(1, 1000), # Associate each event with a user ID from 1 to 1000
"name": f"User{random.randint(1, 1000)}",
"age": random.choices(list(age_distribution.keys()), weights=age_distribution.values())[0],
"gender": random.choices(list(gender_distribution.keys()), weights=gender_distribution.values())[0],
"main_genre": random.choices(list(main_genre_distribution.keys()), weights=main_genre_distribution.values())[0],
"subscription_plan": random.choices(['Paid', 'UnPaid'], weights=[paid_user_probability, 1 - paid_user_probability])[0],
"platform": random.choices(list(platform_distribution.keys()), weights=platform_distribution.values())[0],
"state": random.choices(list(state_distribution.keys()), weights=state_distribution.values())[0],
"OS": random.choices(list(system_distribution.keys()), weights=system_distribution.values()),
"current_page": random.choices(list(page_distribution.keys()), weights=page_distribution.values())[0]
},
}
events.append(event)
return events
# Step 4: Combine the data and events
def combine_data_and_events(data, events):
combined = []
for record in data:
record_events = random.sample(events, random.randint(1, len(events)))
combined.append({
"record": record,
"events": record_events
})
return combined
# Step 5: Write the combined data to an SQL file
def write_to_sql_file(data, filename):
with open(filename, 'w') as sql_file:
for entry in data:
record = entry["record"]
events = entry["events"]
# Write SQL INSERT statement for the record
sql_file.write(f"INSERT INTO songs (track_id, title, song_id, release, artist_id, artist_mbid, artist_name, duration, artist_familiarity, artist_hotttnesss, year, track_7digitalid) VALUES ('{record['track_id']}', '{record['title']}', '{record['song_id']}', '{record['release']}', '{record['artist_id']}', '{record['artist_mbid']}', '{record['artist_name']}', {record['duration']}, {record['artist_familiarity']}, {record['artist_hotttnesss']}, {record['year']}, {record['track_7digitalid']});\n")
# Write SQL INSERT statements for the events
for event in events:
user = event["user"]
sql_file.write(f"INSERT INTO events (event_type, timestamp, user_id, user_name, user_age, user_gender, user_main_genre, user_subscription_plan, user_platform, user_state, user_os, user_current_page) VALUES ('{event['event_type']}', '{event['timestamp']}', {user['id']}, '{user['name']}', '{user['age']}', '{user['gender']}', '{user['main_genre']}', '{user['subscription_plan']}', '{user['platform']}', '{user['state']}', '{user['OS'][0]}', '{user['current_page']}');\n")
# Write SQL INSERT statements for the events
for event in events:
user = event["user"]
sql_file.write(f"INSERT INTO events (event_type, timestamp, user_id, user_name, user_age, user_gender, user_main_genre, user_subscription_plan, user_platform, user_state, user_os, user_current_page) VALUES ('{event['event_type']}', '{event['timestamp']}', {user['id']}, '{user['name']}', '{user['age']}', '{user['gender']}', '{user['main_genre']}', '{user['subscription_plan']}', '{user['platform']}', '{user['state']}', '{user['OS'][0]}', '{user['current_page']}');\n")
def main():
connection = connect_to_database()
data = fetch_data_from_database(connection)
events = generate_random_events(10) # Generate 10 random events
# Create a filename with a timestamp
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
filename = f"output_{timestamp}.sql"
combined_data = combine_data_and_events(data, events)
write_to_sql_file(combined_data, filename)
connection.close()
if __name__ == "__main__":
main()