-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
174 lines (131 loc) · 5.09 KB
/
app.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
from flask import Flask, render_template, request, url_for, session, redirect
from dotenv import load_dotenv
from spotipy.oauth2 import SpotifyOAuth
import spotipy, time, downloadMP3, os
load_dotenv()
app = Flask(__name__)
app.secret_key = "hkb34hvhj3vbjh4IEVIv4ivb"
app.config['SESSION_COOKIE_NAME'] = 'TrackDown Cookie'
TOKEN_INFO = "token_info"
clientID = os.getenv("CLIENT_ID")
clientSecret = os.getenv("CLIENT_SECRET")
@app.route('/')
def login():
oauth = create_spotify_oauth()
auth_url = oauth.get_authorize_url()
return render_template("login.html", url=auth_url)
@app.route('/redirect')
def redirectPage():
oauth = create_spotify_oauth()
session.clear()
code = request.args.get('code')
token_info = oauth.get_access_token(code)
session[TOKEN_INFO] = token_info
return redirect(url_for('home', _external=True))
@app.route('/home')
def home():
try:
token_info = get_token()
except:
print("User not logged in")
return redirect(url_for('login', _external=True))
sp = spotipy.Spotify(auth=token_info['access_token'])
userData = sp.current_user()
userName = userData["display_name"]
userFollowers = userData["followers"]["total"]
userDP = userData["images"][0]["url"] if len(userData["images"]) != 0 else None
userFollowing = len(sp.current_user_followed_artists(limit=50)['artists']['items'])
userNumofPlaylists = len(sp.current_user_playlists(limit=50)['items'])
return render_template("home.html", name=userName, followers=userFollowers, following=userFollowing, dp=userDP, playlists=userNumofPlaylists+1)
@app.route('/playlists')
def playlists():
try:
token_info = get_token()
except:
print("User not logged in")
return redirect(url_for('login', _external=True))
sp = spotipy.Spotify(auth=token_info['access_token'])
data = sp.current_user_playlists(limit=50)['items']
playlists = []
if data:
for playlist in data:
info = {'name':playlist['name'], 'id':playlist['id'], 'total':playlist['tracks']['total']}
if playlist['images'] != []:
info['cover'] = playlist['images'][0]['url']
else:
info['cover'] = None
playlists.append(info)
return render_template("playlists.html", playlists=playlists)
@app.route('/playlists/<id>', methods=["POST", "GET"])
def chosenPlaylist(id):
try:
token_info = get_token()
except:
print("User not logged in")
return redirect(url_for('login', _external=True))
sp = spotipy.Spotify(auth=token_info['access_token'])
all_songs = []
iteration = 0
while True:
if (id == "saved-songs"):
current_songs = sp.current_user_saved_tracks(limit=50, offset=iteration*50)['items']
else:
current_songs = sp.playlist_items(playlist_id=id, limit=50, offset=iteration*50)['items']
iteration += 1
for item in current_songs:
if 'track' in item:
track = item['track']
else:
track = item
try:
track_name = track['name']
track_artist = track['artists'][0]['name']
value = f"{track_name} - {track_artist}"
all_songs.append(value)
except KeyError:
print('Skipping track')
if len(current_songs) < 50:
break
if id == "saved-songs":
playlist_cover = url_for('static', filename='photos/saved_songs.png')
else:
playlist_cover = sp.playlist_cover_image(playlist_id=id)[0]['url']
if playlist_cover == None:
playlist_cover = url_for('static', filename='photos/blank_cover.png')
if id == "saved-songs":
name = "Liked Songs"
else:
data = sp.current_user_playlists(limit=50)['items']
for playlist in data:
if playlist['id'] == id:
name = playlist['name']
break
else:
name = 'Playlist'
if request.method == "POST":
downloadMP3.ListToMP3(playlist_name=name, playlist_songs=all_songs)
return render_template("chosenPlaylist.html", all_songs=enumerate(all_songs), name=name, cover=playlist_cover)
@app.route('/logout')
def logout():
for key in list(session.keys()):
session.pop(key)
return redirect(url_for('login', _external=True))
def get_token():
token_info = session.get(TOKEN_INFO, None)
if not token_info:
raise "exception"
now = int(time.time())
is_expired = token_info['expires_at'] - now < 60
if is_expired:
oauth = create_spotify_oauth()
token_info = oauth.refresh_access_token(token_info['refresh_token'])
return token_info
def create_spotify_oauth():
return SpotifyOAuth(
client_id=clientID,
client_secret=clientSecret,
redirect_uri=url_for('redirectPage', _external=True),
scope="user-library-read user-read-private user-follow-read playlist-read-private playlist-read-collaborative"
)
if __name__ == '__main__':
app.run(debug=True)