-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdataset_shuffler_Spotify.py
60 lines (53 loc) · 2.44 KB
/
dataset_shuffler_Spotify.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
# !/bin/python
from __future__ import print_function
import datetime
import psycopg2
from time import time
from time import sleep
import sys
def main():
shuffles = ['1', '2', '3']
# Open connection
conn = psycopg2.connect(host="/tmp/", database="mettas", user="mettas", port="1997")
cur = conn.cursor()
# If only one argument, clean is provided, drop and remake tables, then exit
if len(sys.argv) == 2 and sys.argv[1] == "clean":
createTables(shuffles, conn)
exit()
def makeCopyString(scf, table):
print("Time of data reload start: " + str(datetime.datetime.now()))
# Ensure only entries with a non-null brand are selected and copied
query = "INSERT INTO {}{} SELECT * FROM {} ORDER BY RANDOM();".format(table, scf, table)
return query
def createTables(shuffles, conn):
cur = conn.cursor()
try:
for scf in shuffles:
print("Recreating shuffle: {}".format(scf))
cur.execute("DROP TABLE IF EXISTS artists" + scf + ";")
cur.execute("DROP TABLE IF EXISTS spotify_tracks" + scf + ";")
cur.execute("CREATE TABLE artists" + scf + """ (
artist_id TEXT,
artist_name TEXT
);""")
cur.execute("CREATE TABLE spotify_tracks" + scf + """ (
id TEXT,
name TEXT,
album TEXT,
album_id TEXT,
artists TEXT,
explicit BOOLEAN,
year INT
);""")
# Execute and commit data copy for wdc1 and wdc2
cur.execute(makeCopyString(scf, "artists"))
cur.execute(makeCopyString(scf, "spotify_tracks"))
conn.commit()
print("Data loaded successfully for shuffle {}.".format(scf))
except Exception as e:
print("An error occurred: {}".format(e))
conn.rollback() # Rollback in case of error
raise # Optionally re-raise the error after handling
print('Spotify done')
if __name__ == '__main__':
main()