-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
177 lines (145 loc) · 5.19 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
175
176
177
import json
import os
from dataclasses import dataclass
import spotipy
from flask import Flask, redirect, render_template, request, session
from spotipy.oauth2 import SpotifyOAuth
app = Flask(__name__)
app.secret_key = os.getenv("secret_key")
cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session)
spotify = SpotifyOAuth(
client_id=os.getenv("client_id"),
client_secret=os.getenv("client_secret"),
redirect_uri=os.getenv('redirect_uri')+"/callback",
scope="user-top-read user-read-recently-played",
cache_handler=cache_handler, show_dialog=True
)
@dataclass
class Track:
name: str
artist: str
image: str
link: str
@dataclass
class Artist:
name: str
image: str
link: str
def group_artist(artists):
artists_names = []
for artist in artists:
artists_names.append(artist.get("name", ""))
return ", ".join(artists_names)
def clean_data(data: dict, type: str = "t"):
if type == "t":
tracks = []
for item in data:
try:
ima = item["album"]["images"][0]["url"]
except IndexError:
ima = ""
tracks.append(
Track(
item.get("name"),
group_artist(item["artists"]),
ima,
item["external_urls"]["spotify"],
)
)
return tracks
if type == "r":
tracks = []
for item in data:
item = item.get("track")
tracks.append(
Track(
item.get("name"),
group_artist(item["artists"]),
item["album"]["images"][0]["url"],
item["external_urls"]["spotify"],
)
)
return tracks
else:
arts = []
for item in data:
arts.append(
Artist(
item["name"],
item["images"][0]["url"],
item["external_urls"]["spotify"],
)
)
return arts
def from_cache(file):
with open(file, "r") as file:
a = json.load(file)
return a["items"]
@app.route("/")
def index():
if not session.get("spotify_token"):
return render_template("home.html", logged_in=False)
return render_template("home.html", username=session.get('username'), profile_pic=session.get('profile_pic'))
@app.route("/top/tracks")
def tracks():
if not session.get("spotify_token"):
return redirect("/")
sp = spotipy.Spotify(auth=session.get("spotify_token"))
long_term_tracks = sp.current_user_top_tracks(time_range="long_term", limit=50)["items"]
middle_term_tracks = sp.current_user_top_tracks(time_range="medium_term", limit=50)["items"]
short_term_tracks = sp.current_user_top_tracks(time_range="short_term", limit=50)["items"]
# long_term_tracks = from_cache('a.json')
# short_term_tracks = from_cache('a.json')
# middle_term_tracks = from_cache('a.json')
return render_template(
"list.html",
lt=clean_data(long_term_tracks),
mt=clean_data(middle_term_tracks),
st=clean_data(short_term_tracks),
username=session.get('username'), profile_pic=session.get('profile_pic')
)
@app.route("/top/artists")
def artists():
if not session.get("spotify_token"):
return redirect("/")
sp = spotipy.Spotify(auth=session.get("spotify_token"))
long_term_artists = sp.current_user_top_artists(time_range="long_term", limit=50)["items"]
middle_term_artists = sp.current_user_top_artists(time_range="medium_term", limit=50)["items"]
short_term_artists = sp.current_user_top_artists(time_range="short_term", limit=50)["items"]
# long_term_artists = from_cache('t.json')
# short_term_artists = from_cache('t.json')
# middle_term_artists = from_cache('t.json')
return render_template(
"list.html",
lt=clean_data(long_term_artists, type="a"),
mt=clean_data(middle_term_artists, type="a"),
st=clean_data(short_term_artists, type="a"), username=session.get('username'), profile_pic=session.get('profile_pic')
)
@app.route("/recents")
def recents():
if not session.get("spotify_token"):
return redirect("/")
sp = spotipy.Spotify(auth=session.get("spotify_token"))
tracks = sp.current_user_recently_played()["items"]
# tracks = from_cache('r.json')
return render_template("recents.html", t=clean_data(tracks, type="r"), username=session.get('username'), profile_pic=session.get('profile_pic'))
@app.route("/login")
def login():
auth_url = spotify.get_authorize_url()
return redirect(auth_url)
@app.route("/logout")
def logout():
session.pop('spotify_token')
session.clear()
return redirect("/")
@app.route("/callback")
def callback():
session.clear()
code = request.args.get("code")
token_info = spotify.get_access_token(code)
session["spotify_token"] = token_info["access_token"]
sp = spotipy.Spotify(auth=session.get("spotify_token"))
user_profile = sp.current_user()
session['username'] = user_profile['display_name']
session['profile_pic'] = user_profile['images'][0]['url']
return redirect("/")