-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
110 lines (95 loc) · 4.12 KB
/
app.py
File metadata and controls
110 lines (95 loc) · 4.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
import os
from flask import Flask
from flask import send_file
from main import Playlist
app = Flask(__name__)
def init_playlist(playlist_id):
playlist = Playlist(playlist_id)
if len(next(os.walk("cache/1qGIIWnowv8o1WcG6qwaBJ/"))[-1]) == 0:
playlist.save_qr_codes()
playlist.save_text()
return playlist
@app.route("/")
def index():
playlists = []
for d in next(os.walk("cache"))[1]:
try:
playlists.append(Playlist(d))
except Exception:
print(f"ERR: unable to load playlist '{d}'")
playlist_html = [
f"<li><a href='/playlist/{playlist.playlist_id}'>{playlist.name}</a></li>"
for playlist in playlists
]
return f"""
<html>
<body>
<h1>Play a Game</h1>
<ul>
{"".join(playlist_html)}
</ul>
<div><input id="url" placeholder="spotify playlist url" /><button onclick="window.location.href='/playlist/'+document.getElementById('url').value.replaceAll('https://open.spotify.com/playlist/','')">go</button></div>
</body>
</html>
"""
@app.route("/playlist/<string:playlist_id>")
def play_game(playlist_id):
playlist = init_playlist(playlist_id)
tracks_html = ",".join(f"'{track}'" for track in playlist.tracks)
track_url_html = ",".join(f"'{track.href}'" for track in playlist.tracks.values())
return f"""
<html>
<script>
let playlist = '{playlist_id}';
let tracks = [{tracks_html}];
let urls = [{track_url_html}];
let current_song = null;
function refresh() {{
let qr = document.getElementById('qr_code')
let card = document.getElementById('card')
let url = document.getElementById('url')
let playlist_url = document.getElementById('list-url')
current_song = tracks[Math.floor(Math.random() * tracks.length)]
const href = urls[tracks.indexOf(current_song)]
qr.style.display = 'block'
card.style.display = 'none'
card.src = '/card/' + playlist + '/' + current_song
qr.src = '/qr_code/' + playlist + '/' + current_song
url.href = href
playlist_url.href = "https://open.spotify.com/playlist/" + playlist
}}
function flip() {{
let qr = document.getElementById('qr_code')
let card = document.getElementById('card')
if (card.style.display == 'none') {{
qr.style.display = 'none'
card.style.display = 'block'
}} else {{
qr.style.display = 'block'
card.style.display = 'none'
}}
}}
</script>
<body onload="refresh()">
<h1>Guess this Song from "{playlist.name}"</h1>
<img id="qr_code" src="" style="width: 300px;" />
<img id="card" src="" style="display: none; width: 300px;" />
<br />
<button onclick="window.location.href = '/';">Zurück</button>
<button onclick="flip()">Show Card</button>
<button onclick="refresh();">Next Song</button>
<br />
<a id="url" target="_blank">Link to this Song (from QR code)</a>
<br />
<a id="list-url" target="_blank">List of all songs in playlist</a>
</body>
</html>
"""
@app.route("/qr_code/<string:playlist_id>/<string:song_id>")
def send_qr_code(playlist_id, song_id):
return send_file(f"cache/{playlist_id}/{song_id}_qr.svg", mimetype="image/svg+xml")
@app.route("/card/<string:playlist_id>/<string:song_id>")
def send_card(playlist_id, song_id):
return send_file(f"cache/{playlist_id}/{song_id}_text.png", mimetype="image/png")
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")