-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify.go
More file actions
154 lines (119 loc) · 3.75 KB
/
spotify.go
File metadata and controls
154 lines (119 loc) · 3.75 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
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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/patrickmn/go-cache"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
"golang.org/x/oauth2"
)
type FullTrack struct {
*spotify.FullTrack
spotifyStatus *SpotifyStatus
}
const (
// SessionKey is the key used for retrieving session state
SessionKey = "spotify-state"
)
func (ss *SpotifyStatus) authenticator() *spotifyauth.Authenticator {
return spotifyauth.New(spotifyauth.WithRedirectURL(fmt.Sprintf("%s/callback", ss.redirectURI)), spotifyauth.WithScopes(spotifyauth.ScopeUserReadCurrentlyPlaying))
}
func (ss *SpotifyStatus) spotifyAuthLoginHandler(w http.ResponseWriter, r *http.Request) {
state := uuid.NewString()
auth := ss.authenticator()
redirect := auth.AuthURL(state)
session, _ := ss.sessionStore.Get(r, SessionKey)
session.AddFlash(state)
session.Save(r, w)
http.Redirect(w, r, redirect, http.StatusTemporaryRedirect)
}
func (ss *SpotifyStatus) spotifyAuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
session, _ := ss.sessionStore.Get(r, SessionKey)
if session.IsNew {
http.Redirect(w, r, fmt.Sprintf("%s/login", ss.redirectURI), http.StatusTemporaryRedirect)
return
}
flashes := session.Flashes()
session.Save(r, w)
if len(flashes) != 1 {
http.Redirect(w, r, fmt.Sprintf("%s/login", ss.redirectURI), http.StatusTemporaryRedirect)
return
}
state := flashes[0].(string)
auth := ss.authenticator()
token, err := auth.Token(r.Context(), state, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
client := spotify.New(auth.Client(r.Context(), token))
user, err := client.CurrentUser(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tokenBytes, _ := json.Marshal(&token)
tokenStr := base64.StdEncoding.EncodeToString(tokenBytes)
ss.cache.Set(user.ID, tokenStr, cache.NoExpiration)
http.Redirect(w, r, fmt.Sprintf("%s/%s", ss.redirectURI, user.ID), http.StatusTemporaryRedirect)
}
func (ss *SpotifyStatus) getTrack(w http.ResponseWriter, r *http.Request) (*FullTrack, bool) {
tokenEntry, exists := ss.cache.Get(mux.Vars(r)["id"])
if !exists {
http.Error(w, "not found...", http.StatusNotFound)
return nil, false
}
auth := ss.authenticator()
token := &oauth2.Token{}
tokenBytes, _ := base64.StdEncoding.DecodeString(tokenEntry.(string))
json.Unmarshal(tokenBytes, &token)
client := spotify.New(auth.Client(r.Context(), token))
playing, err := client.PlayerCurrentlyPlaying(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil, false
}
track := &FullTrack{FullTrack: playing.Item, spotifyStatus: ss}
if !playing.Playing {
track.FullTrack = nil
}
w.Header().Add("cache-control", "private, no-store")
trackID := "ruh roh, no track"
if track.FullTrack != nil {
trackID = track.ID.String()
}
w.Header().Set("etag", trackID)
return track, true
}
func (ss *SpotifyStatus) spotifyBadgeHandler(w http.ResponseWriter, r *http.Request) {
track, ok := ss.getTrack(w, r)
if !ok {
return
}
svg, err := track.Badge()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if track.FullTrack != nil {
w.Header().Set("spotify-uri", track.ExternalURLs["spotify"])
}
w.Header().Set("content-type", "image/svg+xml")
w.WriteHeader(http.StatusOK)
w.Write(svg)
}
func (ss *SpotifyStatus) spotifyLinkHandler(w http.ResponseWriter, r *http.Request) {
track, ok := ss.getTrack(w, r)
if !ok {
return
}
if track.FullTrack != nil {
http.Redirect(w, r, track.ExternalURLs["spotify"], http.StatusTemporaryRedirect)
return
}
http.Error(w, "nothing playing...", http.StatusTeapot)
}