-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathretrieve_sim.py
More file actions
66 lines (55 loc) · 1.89 KB
/
retrieve_sim.py
File metadata and controls
66 lines (55 loc) · 1.89 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
import mysql.connector
import pandas as pd
import time
import os
from config import db_config # Import the configuration
def clear_console():
if os.name == 'nt':
_ = os.system('clear')
# Function to get random data from MySQL
def get_random_data():
try:
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor(dictionary=True)
query = """
SELECT `track_id`, `title`, `song_id`, `release`, `artist_id`, `artist_mbid`,
`artist_name`, `duration`, `artist_familiarity`, `artist_hotttnesss`,
`year`, `track_7digitalid`
FROM songs
ORDER BY RAND()
LIMIT 5
"""
cursor.execute(query)
results = cursor.fetchall()
df = pd.DataFrame(results)
return df
except mysql.connector.Error as err:
print(f"Error: {err}")
return pd.DataFrame()
finally:
if conn.is_connected():
cursor.close()
conn.close()
# Function to simulate a listen event
def simulate_listen_event(track):
print(f"Simulated listen event for track: {track['title']} by {track['artist_name']}")
# Main simulation loop
def run_simulation():
for i in range(5): # 5 iterations = 10 minutes
clear_console()
print(f"Iteration {i+1} - Time: {time.strftime('%Y-%m-%d %H:%M:%S')}")
df = get_random_data()
if not df.empty:
print(df)
# Simulate a listen event for each track
for _, track in df.iterrows():
simulate_listen_event(track)
else:
print("No data retrieved")
# Wait for 2 minutes before the next iteration
if i < 4: # Don't wait after the last iteration
print("\nWaiting for 2 minutes...")
time.sleep(120)
# Run the simulation
if __name__ == "__main__":
run_simulation()