-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·297 lines (202 loc) · 8.42 KB
/
server.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
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
"""Playlist creator."""
import os
import requests
from jinja2 import StrictUndefined
from flask import (Flask, render_template, redirect, request, flash, session, jsonify)
from flask_debugtoolbar import DebugToolbarExtension
from flask_session import Session
from model import User, Artist, Song, Playlist, PlaylistSong, delete_user_playlist, connect_to_db, db
# clear_data
# ///////// SPOTIPY SAMPLE CODE
import spotipy
import uuid
SPOTIFY_SCOPE = 'playlist-modify-private playlist-modify-public user-read-private user-read-currently-playing streaming user-read-email user-library-read user-modify-playback-state user-read-playback-state'
# ///////// SPOTIPY SAMPLE CODE
import setlist_api
import spotify_api
app = Flask(__name__)
# ///////// SPOTIPY SAMPLE CODE
app.config['SECRET_KEY'] = os.urandom(64)
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_FILE_DIR'] = './.flask_session/'
Session(app)
# ///////// SPOTIPY SAMPLE CODE
# Required to use Flask sessions and the debug toolbar
app.secret_key = "ABC"
app.jinja_env.undefined = StrictUndefined
# ///////// SPOTIPY SAMPLE CODE
caches_folder = './.spotify_caches/'
if not os.path.exists(caches_folder):
os.makedirs(caches_folder)
def session_cache_path():
return caches_folder + session.get('uuid')
# ///////// SPOTIPY SAMPLE CODE
@app.route('/')
def index():
"""Homepage."""
return render_template("homepage.html")
@app.route('/spotify-login')
def spotify_login():
""" Spotify Authorization Page """
spotify_auth_url = spotify_api.generate_auth_url()
print('\n\n\n\n')
print(spotify_auth_url)
if not session.get('uuid'):
# Step 1. Visitor is unknown, give random ID
session['uuid'] = str(uuid.uuid4())
auth_manager = spotipy.oauth2.SpotifyOAuth(scope=SPOTIFY_SCOPE,
cache_path=session_cache_path(),
show_dialog=True)
if request.args.get("code"):
# Step 3. Being redirected from Spotify auth page
auth_manager.get_access_token(request.args.get("code"))
return redirect('/')
if not auth_manager.get_cached_token():
# Step 2. Display sign in link when no token
auth_url = auth_manager.get_authorize_url()
return f'<h2><a href="{auth_url}">Sign in</a></h2>'
# Step 4. Signed in, display data
spotify = spotipy.Spotify(auth_manager=auth_manager)
session['access_token'] = auth_manager.get_access_token()
print(session['access_token'])
#return f'<h2>Hi {spotify.me()["display_name"]}, we are currently under maintenance! Check back again soon!'
return redirect('/')
#************************************************
@app.route('/display-playlists')
def display_playlists():
"""Show playlists on page."""
user_id = session.get("user_id")
user = User.query.get(user_id)
return render_template("display-playlists.html", playlists=user.playlists)
@app.route('/add-to-playlist', methods=["GET"])
def show_add_to_playlist_form():
"""Shows page to add to playlists."""
return render_template("add-to-playlist.html")
@app.route('/add-to-playlist', methods=["POST"])
def db_add_to_playlist():
"""Adds to playlist in db."""
playlist_title = request.form.get('playlist_title')
artist_name = request.form.get('artist_name')
user_id = session.get("user_id")
setlist_api.db_create_playlist(artist_name = artist_name, playlist_title = playlist_title, user_id = user_id)
flash(f'Songs added successfully to {playlist_title} playlist.')
return redirect(f'/user-dashboard/{user_id}')
#*********************************
@app.route('/add-to-spotify-playlist', methods=["POST"])
def add_to_spotify_playlist():
"""Adds playlist to spotify."""
user_id = session.get("user_id")
playlist_title = request.form.get('playlist_title')
spotify_username = session.get('spotify username')
#check if token in session?
token = session.get('access_token')
spotify_api.create_spotify_playlist_from_db(user_id, playlist_title, token)
#need to get spotify user_id, public or private?
flash(f'Songs added successfully to {playlist_title} playlist on Spotify.')
return redirect(f'/user-dashboard/{user_id}')
#************************************
@app.route('/clear-playlists', methods=["GET"])
def show_clear_playlist_form():
"""Shows show_clear_playlist_form playlist page."""
return render_template("clear-playlists.html")
@app.route('/clear-playlists', methods=["POST"])
def clear_playlist():
"""Deletes playlists from db."""
clear_data()
flash('All playlists deleted.')
return redirect('/')
@app.route('/delete-playlist', methods=["POST"])
def delete_playlist():
"""Deletes playlist from db."""
playlist_title = request.form.get('playlist_title')
user_id = session.get('user_id')
delete_user_playlist(playlist_title, user_id)
flash(f'Playlist {playlist_title} deleted.')
return redirect(f'/user-dashboard/{user_id}')
@app.route("/log-in", methods=["GET"])
def show_login_form():
"""Show login form or registration button for users."""
user_id = session.get("user_id")
if user_id:
return redirect(f"/user-dashboard/{user_id}")
return render_template("user-login.html")
@app.route("/log-in", methods=["POST"])
def handle_login():
"""Log-in a user."""
email = request.form.get("email")
password = request.form.get("password")
user = User.query.filter_by(email=email).first()
if not user:
flash(f"No account with {email}.")
return redirect("/log-in")
if not user.check_password(password):
flash("Incorrect password.")
return redirect("/log-in")
session["user_id"] = user.id
flash("Login successful.")
return redirect(f"/user-dashboard/{user.id}")
@app.route("/register", methods=["GET"])
def show_registration_form():
"""Show registration form for users."""
return render_template("user-register.html")
@app.route("/register", methods=["POST"])
def process_user_registration():
"""Process user registration."""
username = request.form.get("name")
email = request.form.get("email")
password = request.form.get("password")
if User.query.filter_by(email=email).first():
flash("An account with this email already exists.")
return redirect("/register")
new_user = User(name=username, email=email)
new_user.set_password(password)
db.session.add(new_user)
db.session.commit()
# Log in new user
session["user_id"] = new_user.id
flash(f"Successfully registered {username}.")
return redirect(f"/user-dashboard/{new_user.id}")
@app.route("/logout")
def logout():
"""Log out of a user account."""
session.clear()
flash("Logout successful.")
return redirect("/")
@app.route("/user-dashboard/<int:user_id>")
def show_user_dashboard(user_id):
"""Show a user's dashboard where they can view and play playlists."""
if check_authorization(user_id):
user = User.query.get(user_id)
playlists = user.playlists
return render_template("user-dashboard.html",
user=user,
playlists=playlists)
return render_template("unauthorized.html")
def check_authorization(user_id):
"""Check to see if the logged in user is authorized to view page."""
# Get the current user's id.
session_user_id = session.get("user_id")
# Return if correct user is logged in
return session_user_id == user_id
@app.route("/play-music")
def play_music():
"""web player"""
user_id = session.get("user_id")
playlist_id = request.args.get('playlist_id')
playlist = Playlist.query.get(playlist_id)
token = session.get('access_token')
spotify_api.play_playlist_on_web_player(user_id, playlist.playlist_title, token)
return f'Playing playlist {playlist.playlist_title} on Spotify.'
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the
# point that we invoke the DebugToolbarExtension
app.debug = True
# app.debug = False
# make sure templates, etc. are not cached in debug mode
app.jinja_env.auto_reload = app.debug
connect_to_db(app)
# Use the DebugToolbar
DebugToolbarExtension(app)
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False #for error: AttributeError: 'Request' object has no attribute 'is_xhr'
app.run(port=5000, host='0.0.0.0')
# app.run() deployment