-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
200 lines (161 loc) · 4.4 KB
/
main.go
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
package main
import (
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"github.com/spf13/viper"
"github.com/zmb3/spotify"
)
// 128513-128591
type Session struct {
Session_Code [3]int
}
type Index_Context struct {
LoginUrl string
}
type Client struct {
client spotify.Client
logged_in bool
}
var live_sessions []Session
const redirectURI = "http://11155126.ngrok.io/callback"
var (
auth = spotify.NewAuthenticator(redirectURI, spotify.ScopeUserReadCurrentlyPlaying, spotify.ScopeUserReadPlaybackState, spotify.ScopeUserModifyPlaybackState)
ch = make(chan *spotify.Client)
state = "abc123"
)
var (
clients []Client
)
func generate_session_code() [3]int {
// Generates a random session code and checks that it's not being used
var session_code [3]int
valid := false
for valid == false {
session_code = [3]int{rand.Intn(78) + 128513, rand.Intn(78) + 128513, rand.Intn(78) + 128513}
valid = true
// UPGRADE THIS TO BINARY SEARCH
for _, sess := range live_sessions {
if session_code == sess.Session_Code {
valid = false
break
}
}
// STOP BEING LAZY
}
live_sessions = append(live_sessions, Session{Session_Code: session_code})
return session_code
}
func index(w http.ResponseWriter, r *http.Request) {
url := auth.AuthURL(state)
t, _ := template.ParseFiles("index.html")
t.Execute(w, Index_Context{LoginUrl: url})
}
func authenticate(w http.ResponseWriter, r *http.Request) {
tok, err := auth.Token(state, r)
if err != nil {
http.Error(w, "Couldn't get token", http.StatusForbidden)
log.Fatal(err)
}
if st := r.FormValue("state"); st != state {
http.NotFound(w, r)
log.Fatalf("State mismatch: %s != %s\n", st, state)
}
// use the token to get an authenticated client
clients = append(clients, Client{auth.NewClient(tok), true})
log.Println("Logged In!")
http.Redirect(w, r, "http://11155126.ngrok.io/joinSession", 303)
}
func joinSession(w http.ResponseWriter, r *http.Request) {
var session_code [3]int
s := r.FormValue(sessionCode)
session_code = []int(r.FormValue(sessionCode))
// Check if session exists
valid := false
for _, sess := range live_sessions {
if session_code == sess.Session_Code {
valid = true
break
}
}
if !valid {
log.Printf("Session %v not found", session_code)
return
}
log.Printf("User %s joining session %s", clients[0].client.CurrentUser().DisplayName, session_code)
var templateData = struct {
curSession [3]int
users []string
}{}
templateData.curSession = session_code
for _, i := range clients {
templateData.users = append(templateData.users, i.client.CurrentUser().DisplayName)
}
t, _ := template.ParseFiles("session.html")
t.Execute(w, &templateData)
}
func session(w http.ResponseWriter, r *http.Request) {
if !client[0].logged_in {
http.Redirect(w, r, "http://11155126.ngrok.io", 307)
return
}
session_code := r.Form.Get(sessionCode)
var templateData = struct {
curSession [3]int
users []string
}{}
templateData.curSession = session_code
for _, i := range clients {
templateData.users = append(templateData.users, i.client.CurrentUser().DisplayName)
}
t, _ := template.ParseFiles("session.html")
t.Execute(w, &templateData)
err := clients[0].client.Play()
if err != nil {
log.Print(err)
}
player, err := clients[0].client.PlayerCurrentlyPlaying()
if err != nil {
log.Print(err)
return
}
log.Println(player)
}
func newSession(w http.ResponseWriter, r *http.Request) {
// Session Page:
// - Session Code
// - Change Session Button
if !client[0].logged_in {
http.Redirect(w, r, "http://11155126.ngrok.io", 307)
return
}
log.Println(live_sessions)
session_code := generate_session_code()
for _, code := range session_code {
log.Println(code)
}
url := fmt.Sprintf("http://11155126.ngrok.io/session?sessionCode=%v", session_code)
http.Redirect(w, r, url, 307)
/*err := clients[0].client.Play()
if err != nil {
log.Print(err)
}
player, err := clients[0].client.PlayerCurrentlyPlaying()
if err != nil {
log.Print(err)
return
}
log.Println(player)*/
}
func main() {
viper.AutomaticEnv()
auth = spotify.NewAuthenticator(redirectURI, spotify.ScopeUserReadCurrentlyPlaying, spotify.ScopeUserReadPlaybackState, spotify.ScopeUserModifyPlaybackState)
http.HandleFunc("/callback", authenticate)
http.HandleFunc("/newSession", newSession)
http.HandleFunc("/joinSession", joinSession)
http.HandleFunc("/", index)
go http.ListenAndServe(":8888", nil)
select {}
}