-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
397 lines (352 loc) · 13.9 KB
/
main.py
File metadata and controls
397 lines (352 loc) · 13.9 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
from flask import Flask, render_template, request, redirect, url_for, jsonify, session, send_file
from spotipy import Spotify
from spotipy.oauth2 import SpotifyClientCredentials
import json
import os
import io
import qrcode
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
bsurl = "http://192.168.2.113:5000/"
# Replace with your Spotify API credentials
CLIENT_ID = os.getenv('CLIENT_ID')
CLIENT_SECRET = os.getenv('CLIENT_SECRET')
# Initialize Spotipy client
sp = Spotify(auth_manager=SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET))
# Set a secret key for session management
app.secret_key = os.urandom(24)
# ... (Other routes and code)
@app.route('/')
def home():
return redirect("https://github.com/tschuerti/SoundWish")
#404 error page
@app.errorhandler(404)
def page_not_found(e):
return redirect("https://github.com/tschuerti/SoundWish")
@app.route('/<name>/overview', methods=['GET', 'POST'])
def overview(name):
submissions_file = 'json/' + name + '.json'
events = []
with open('events.json', 'r') as f:
for line in f:
event = json.loads(line)
events.append(event)
for event in events:
if event['url'] == name:
background_color = event['background_color']
rname = event['name']
vs = event['veranstalter']
url = event['url']
break
event_exists = False
for event in events:
if event['url'] == name:
event_exists = True
break
if not event_exists:
return "Event not found"
PASSWORD = event['password']
pasw = name + "_password"
if pasw not in session:
return redirect("/" + name + "/login")
if session[pasw] != PASSWORD:
session.pop(pasw, None)
return redirect("/" + name + "/login")
# Read the submitted wishes from the JSON file
wishes = []
if not os.path.exists(submissions_file):
with open(submissions_file, 'w') as f:
f.write('')
with open(submissions_file, 'r') as f:
for line in f:
wish = json.loads(line)
wishes.append(wish)
for wish in wishes:
track_id = wish['selected_song']
try:
track_info = sp.track(track_id)
wish['song_name'] = track_info['name']
wish['artist_name'] = track_info['artists'][0]['name']
if len(track_info['artists']) > 1:
for i in range(2):
wish['artist_name'] += ", " + track_info['artists'][i]['name']
wish['spotify_url'] = track_info['external_urls']['spotify']
wish['thumbnail_url'] = track_info['album']['images'][0]['url']
except Exception as e:
# Handle errors (e.g., track not found)
print(f"Error fetching track information: {str(e)}")
# only show wishes without an status
wishes = [w for w in wishes if 'status' not in w]
#reverse the list to show the newest first
wishes = wishes[::-1]
# Handle wish deletion
if request.method == 'POST':
type = request.form.get('type')
if type == "delete":
wish_id_to_delete = request.form.get('delete')
if wish_id_to_delete is not None:
# Delete the wish from the list
wishes = [w for w in wishes if w['selected_song'] == wish_id_to_delete]
# set an deleted variable to true in the json file
with open(submissions_file, 'r') as f:
lines = f.readlines()
with open(submissions_file, 'w') as f:
for line in lines:
wish = json.loads(line)
if wish['selected_song'] == wish_id_to_delete:
wish['status'] = "deleted"
json.dump(wish, f)
f.write('\n')
elif type == "played":
wish_id_to_played = request.form.get('played')
if wish_id_to_played is not None:
# Delete the wish from the list
wishes = [w for w in wishes if w['selected_song'] == wish_id_to_played]
# set an deleted variable to true in the json file
with open(submissions_file, 'r') as f:
lines = f.readlines()
with open(submissions_file, 'w') as f:
for line in lines:
wish = json.loads(line)
if wish['selected_song'] == wish_id_to_played:
wish['status'] = "played"
json.dump(wish, f)
f.write('\n')
return redirect(url_for('overview', name=name))
return render_template('overview.html', wishes=wishes, name=rname, background_color=background_color, vs=vs, url=url)
@app.route('/<name>/overview/played')
def overviewplayed(name):
submissions_file = 'json/' + name + '.json'
events = []
with open('events.json', 'r') as f:
for line in f:
event = json.loads(line)
events.append(event)
event_exists = False
for event in events:
if event['url'] == name:
event_exists = True
break
if not event_exists:
return "Event not found"
PASSWORD = event['password']
pasw = name + "_password"
if pasw not in session or session[pasw] != PASSWORD:
return redirect("/" + name + "/login")
# Read the submitted wishes from the JSON file
wishes = []
with open(submissions_file, 'r') as f:
for line in f:
wish = json.loads(line)
wishes.append(wish)
for wish in wishes:
track_id = wish['selected_song']
try:
track_info = sp.track(track_id)
wish['song_name'] = track_info['name']
wish['artist_name'] = track_info['artists'][0]['name']
wish['spotify_url'] = track_info['external_urls']['spotify']
wish['thumbnail_url'] = track_info['album']['images'][0]['url']
except Exception as e:
# Handle errors (e.g., track not found)
print(f"Error fetching track information: {str(e)}")
# only show wishes without status played
wishes = [w for w in wishes if 'status' in w and w['status'] == "played"]
#reverse the list to show the newest first
wishes = wishes[::-1]
for event in events:
if event['url'] == name:
background_color = event['background_color']
rname = event['name']
vs = event['veranstalter']
url = event['url']
break
return render_template('overview2.html', wishes=wishes, name=rname, background_color=background_color, vs=vs, url=url, type="Gespielte")
@app.route('/<name>/overview/deleted')
def overviewdeleted(name):
submissions_file = 'json/' + name + '.json'
events = []
with open('events.json', 'r') as f:
for line in f:
event = json.loads(line)
events.append(event)
event_exists = False
for event in events:
if event['url'] == name:
event_exists = True
break
if not event_exists:
return "Event not found"
PASSWORD = event['password']
pasw = name + "_password"
if pasw not in session or session[pasw] != PASSWORD:
return redirect("/" + name + "/login")
# Read the submitted wishes from the JSON file
wishes = []
with open(submissions_file, 'r') as f:
for line in f:
wish = json.loads(line)
wishes.append(wish)
for wish in wishes:
track_id = wish['selected_song']
try:
track_info = sp.track(track_id)
wish['song_name'] = track_info['name']
wish['artist_name'] = track_info['artists'][0]['name']
wish['spotify_url'] = track_info['external_urls']['spotify']
wish['thumbnail_url'] = track_info['album']['images'][0]['url']
except Exception as e:
# Handle errors (e.g., track not found)
print(f"Error fetching track information: {str(e)}")
# only show wishes without status played
wishes = [w for w in wishes if 'status' in w and w['status'] == "deleted"]
#reverse the list to show the newest first
wishes = wishes[::-1]
for event in events:
if event['url'] == name:
background_color = event['background_color']
rname = event['name']
vs = event['veranstalter']
url = event['url']
break
return render_template('overview2.html', wishes=wishes, name=rname, background_color=background_color, vs=vs, url=url, type="Gelöschte")
@app.route('/<name>/login', methods=['GET', 'POST'])
def login(name):
events = []
with open('events.json', 'r') as f:
for line in f:
event = json.loads(line)
events.append(event)
for event in events:
if event['url'] == name:
background_color = event['background_color']
rname = event['name']
vs = event['veranstalter']
vsurl = event['url']
break
event_exists = False
for event in events:
if event['url'] == name:
event_exists = True
break
if not event_exists:
return "Event not found"
PASSWORD = event['password']
if request.method == 'POST':
password_attempt = request.form.get('password')
# set the password to the one defined in the event.json file
pasw = name + "_password"
if password_attempt == PASSWORD:
session[pasw] = PASSWORD
return redirect("/" + name + "/overview")
else:
return render_template('login.html', vs = vs, rname=rname, background_color=background_color, vsurl=vsurl, error=True)
else:
pasw = name + "_password"
if pasw in session and session[pasw] == PASSWORD:
return redirect("/" + name + "/overview")
else:
return render_template('login.html', vs = vs, rname=rname, background_color=background_color, vsurl=vsurl, error=False)
@app.route('/<name>/logout')
def logout(name):
pasw = name + "_password"
session.pop(pasw, None)
return redirect("/" + name + "/login")
@app.route('/<name>', methods=['GET', 'POST'])
def index(name):
#check in the events.json file if the event exists
events = []
with open('events.json', 'r') as f:
for line in f:
event = json.loads(line)
events.append(event)
event_exists = False
for event in events:
if event['url'] == name:
event_exists = True
break
if not event_exists:
return "Event not found"
submissions_file = 'json/' + name + '.json'
if request.method == 'POST':
name = request.form['name']
selected_song = request.form['selected_song']
message = request.form['message']
submission = {
'name': name,
'selected_song': selected_song,
'message': message,
}
# Save the submission to a JSON file
with open(submissions_file, 'a') as f:
json.dump(submission, f)
f.write('\n')
return redirect(url_for('submitted', name=event['url']))
#return the index with the background_color variable from the events.json file
for event in events:
if event['url'] == name:
background_color = event['background_color']
rname = event['name']
vs = event['veranstalter']
break
return render_template('index.html', background_color=background_color, name=name, rname=rname, vs=vs)
@app.route('/search_song', methods=['POST'])
def search_song():
query = request.form['query']
if query:
results = sp.search(q=query, type='track', limit=10)
tracks = results['tracks']['items']
# Add image URL to each track
for track in tracks:
if len(track['album']['images']) > 0:
track['image_url'] = track['album']['images'][0]['url']
else:
track['image_url'] = None
else:
tracks = []
return jsonify(tracks)
@app.route('/<name>/submitted')
def submitted(name):
events = []
with open('events.json', 'r') as f:
for line in f:
event = json.loads(line)
events.append(event)
for event in events:
if event['url'] == name:
background_color = event['background_color']
rname = event['name']
vs = event['veranstalter']
vsurl = event['url']
break
return render_template('submitted.html', vs = vs, rname=rname, background_color=background_color, vsurl=vsurl)
@app.route('/<name>/qr')
def qr(name):
# if the event exists, generate a QR code
events = []
with open('events.json', 'r') as f:
for line in f:
event = json.loads(line)
events.append(event)
event_exists = False
for event in events:
if event['url'] == name:
event_exists = True
break
if not event_exists:
return "Event not found"
# Generate the QR code
qr_data = bsurl + name
img = qrcode.make(qr_data)
# Create an in-memory file
img_data = io.BytesIO()
img.save(img_data, format='PNG')
img_data.seek(0) # Reset the buffer position to the beginning
# Serve the QR code as a temporary file
return send_file(img_data, mimetype='image/png')
@app.route('/media/<file>')
def media(file):
return send_file("./media/" + file)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')