-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsptfy.py
179 lines (160 loc) · 6.23 KB
/
sptfy.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
from flask import Flask, request, redirect, render_template_string
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
import spotipy
from datetime import datetime
# Load environment variables
load_dotenv()
app = Flask(__name__)
SPOTIPY_CLIENT_ID = '6087621be5394ddf882a4d2bc72e50d6'
SPOTIPY_CLIENT_SECRET = '3e919d84ba064c7883dd2107568932b8'
SPOTIPY_REDIRECT_URI = 'http://localhost:8888/callback'
sp_oauth = SpotifyOAuth(
client_id=SPOTIPY_CLIENT_ID,
client_secret=SPOTIPY_CLIENT_SECRET,
redirect_uri=SPOTIPY_REDIRECT_URI,
scope="user-library-read user-read-recently-played"
)
# Your existing token from the callback
access_token = 'BQCrd5iYK5I3V2zabGrU4zp-HcSujEeCTjeOHrO9UPG8ZCxcLL2dBTfu4q810StLK3EJsXt0ncrODbpBy9QhKbogvv-Ix8TmneZdzPjlOerbITObLIbe4_UPiN9Y0aUDhNM34nrpqMAGA2gBwWXiHsmiLorv0Qb8ZPTFDbIsZ8q2qRiRxmf58DJdlIm4SjL_aqxwfMlUHprc9dr0SL_FFyRVvQsgmv7Tcy6ljPw14ilBTIZ0lCofT9xqYNgF'
sp = spotipy.Spotify(auth=access_token)
@app.route('/')
def home():
# Redirect to Spotify's authorization page
auth_url = sp_oauth.get_authorize_url()
return redirect(auth_url)
@app.route('/callback')
def callback():
code = request.args.get('code')
if code:
# Get access token
token_info = sp_oauth.get_access_token(code)
access_token = token_info['access_token']
print(f"Access token: {access_token}") # Log the token
# Now use the access token to fetch the recently played tracks
sp = spotipy.Spotify(auth=access_token)
# Fetch the last 10 tracks
recently_played = sp.current_user_recently_played(limit=10)
# Collect the track information
tracks_info = []
for item in recently_played['items']:
track = item['track']
played_at = item['played_at']
try:
# Convert the timestamp to a readable format, adjusting for milliseconds and Z (UTC)
formatted_time = datetime.strptime(played_at, "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%B %d, %Y, %I:%M %p")
except ValueError:
# In case the format does not match, handle gracefully
formatted_time = "Unknown time format"
tracks_info.append({
'name': track['name'],
'artist': ', '.join(artist['name'] for artist in track['artists']),
'album': track['album']['name'],
'url': track['external_urls']['spotify'],
'image': track['album']['images'][0]['url'] if track['album']['images'] else '',
'played_at': formatted_time
})
# Render the HTML with minimal Spotify-like layout
html_content = """
<html>
<head>
<title>Recently Played Tracks</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #121212;
color: white;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
max-width: 350px;
overflow-y: auto;
padding: 10px;
box-sizing: border-box;
}
.track {
display: flex;
align-items: center;
background-color: #282828;
border-radius: 8px;
margin: 8px;
padding: 8px;
width: 100%;
box-sizing: border-box;
}
.track img {
width: 40px;
height: 40px;
border-radius: 5px;
margin-right: 10px;
}
.track-info {
display: flex;
flex-direction: column;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.track-name {
font-size: 14px;
font-weight: bold;
margin-bottom: 4px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.track-artist {
font-size: 12px;
margin-bottom: 4px;
color: #b3b3b3;
}
.track-album {
font-size: 10px;
color: #b3b3b3;
}
.track-time {
font-size: 10px;
color: #b3b3b3;
margin-top: 4px;
}
a {
font-size: 10px;
color: #1db954;
text-decoration: none;
margin-top: 4px;
}
</style>
</head>
<body>
<div class="container">
<h1>Recently Played Tracks</h1>
{% for track in tracks %}
<div class="track">
<img src="{{ track.image }}" alt="{{ track.name }}">
<div class="track-info">
<div class="track-name">{{ track.name }}</div>
<div class="track-artist">{{ track.artist }}</div>
<div class="track-album">{{ track.album }}</div>
<div class="track-time">Played on: {{ track.played_at }}</div>
<a href="{{ track.url }}" target="_blank">Listen on Spotify</a>
</div>
</div>
{% endfor %}
</div>
</body>
</html>
"""
return render_template_string(html_content, tracks=tracks_info)
else:
return "Authorization failed."
if __name__ == '__main__':
app.run(debug=True, port=8888)