-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
122 lines (110 loc) · 5.06 KB
/
admin.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
from glob import escape
from flask import Flask, request, render_template
import logging
from analyzer import execute_query, execute_read_multiple_query, execute_read_query, escape_sql_string
app = Flask(__name__)
app.debug = True
log = logging.getLogger('werkzeug')
log.disabled = True
@app.route('/')
def admin_all_artists():
all_artists = get_all_artists()
return render_template('admin.html', artists=all_artists, page=0)
@app.route('/page/<page>/')
def admin_artists_range(page):
all_artists = get_all_artists(range=[int(page) * 100, (int(page) + 1) * 100])
return render_template('admin.html', artists=all_artists, page=int(page))
@app.route('/clean/')
def admin_clean_all_artists():
all_artists = get_all_artists(clean=True)
return render_template('admin.html', artists=all_artists, page=0)
@app.route('/', methods=['POST'])
def admin_change_artists():
update_artists(request.json, admin=True)
return request.json
@app.route('/artist/<artist_name>/')
def get_specific_artists(artist_name):
all_artists = get_all_artists(name=artist_name)
return render_template('admin.html', artists=all_artists, page=0)
@app.route('/artist/<artist_name>/', methods=['POST'])
def change_specific_artists(artist_name):
update_artists(request.json, admin=True)
return request.json
def get_all_artists(playlist=None, clean=False, name=None, range=None):
all_artists_dict = {}
if(playlist == None):
all_artists = []
limit = ""
if (range == None and not clean):
limit = "limit 100"
elif (not clean):
limit = f"limit {range[1] - range[0]} offset {range[0]}"
if(name == None):
all_artists = execute_read_multiple_query(f"SELECT * from artists {limit}")
else:
escaped_name = escape_sql_string(name).lower()
all_artists = execute_read_multiple_query(f"SELECT * from artists WHERE lower(name)='{escaped_name}'")
for artist in all_artists:
artist_dict = {
"id": artist[0],
"name": artist[1],
"picture": artist[2],
"popularity": artist[3],
"votes_m": artist[4],
"votes_f": artist[5],
"votes_x": artist[6],
"votes_mix": artist[7],
"consensus": artist[8],
"locked": artist[9],
}
all_artists_dict[artist[0]] = artist_dict
if(clean):
if(artist_dict['consensus'] == 'M' or artist_dict['consensus'] == 'MIX'):
execute_query(f"DELETE from recs WHERE artist_id='{artist_dict['id']}'")
return all_artists_dict
def update_artists(updates_json, admin=False):
for id in updates_json:
artist = execute_read_query(f"SELECT * from artists WHERE spotify_id='{id}'")
artist_dict = {
"id": artist[0],
"name": artist[1],
"picture": artist[2],
"popularity": artist[3],
"votes_m": artist[4],
"votes_f": artist[5],
"votes_x": artist[6],
"votes_mix": artist[7],
"consensus": artist[8],
"locked": artist[9],
}
if(not('category' in updates_json[id])):
updates_json[id]['category'] = artist_dict['consensus']
if(not('locked' in updates_json[id])):
updates_json[id]['locked'] = artist_dict['locked']
if(not(admin)):
if(artist_dict['locked'] == 0):
vote_category = ""
if(updates_json[id]['category'] == 'M'):
artist_dict['votes_m'] += 1
vote_category = 'votes_m'
elif(updates_json[id]['category'] == 'F'):
artist_dict['votes_f'] += 1
vote_category = 'votes_f'
elif(updates_json[id]['category'] == 'X'):
artist_dict['votes_x'] += 1
vote_category = 'votes_x'
elif(updates_json[id]['category'] == 'MIX'):
artist_dict['votes_mix'] += 1
vote_category = 'votes_mix'
if(artist_dict[vote_category] == max(artist_dict['votes_m'], artist_dict['votes_f'], \
artist_dict['votes_x'], artist_dict['votes_mix'])):
artist_dict['consensus'] = updates_json[id]['category']
execute_query(f"UPDATE artists SET {vote_category}={artist_dict[vote_category]}, consensus=" + \
f"'{artist_dict['consensus']}' WHERE spotify_id='{id}'")
else:
execute_query(f"UPDATE artists SET consensus='{updates_json[id]['category']}', locked={updates_json[id]['locked']} " + \
f"WHERE spotify_id='{id}'")
if(updates_json[id]['category'] == 'M' or updates_json[id]['category'] == 'MIX'):
execute_query(f"DELETE from recs WHERE artist_id='{artist_dict['id']}'")
if __name__ == '__main__':
app.run(threaded=True)