-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrdio.go
More file actions
166 lines (156 loc) · 4.29 KB
/
rdio.go
File metadata and controls
166 lines (156 loc) · 4.29 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
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"github.com/nickdirienzo/go-json-rest"
"github.com/nickdirienzo/rdigo"
"log"
"net/http"
"os"
// "time"
)
const (
month = 60 * 60 * 24 * 30
hackersBaseUrl = "http://buffalohackers.com/%s"
)
type Redirect struct {
See string `json:"see"`
}
func getCookie(r *rest.Request, name string) *http.Cookie {
for _, cookie := range r.Cookies() {
if cookie.Name == name {
return cookie
}
}
return nil
}
func newRdioClient() rdigo.Rdio {
return rdigo.NewClient(os.Getenv("RDIOTOKEN"), os.Getenv("RDIOSECRET"))
}
func authedRdioClient(at, ats string) rdigo.Rdio {
return rdigo.AuthenticatedClient(os.Getenv("RDIOTOKEN"), os.Getenv("RDIOSECRET"), at, ats)
}
func (self *Api) LogIn(w *rest.ResponseWriter, r *rest.Request) {
http.SetCookie(w, &http.Cookie{
Name: "at",
Value: "",
})
http.SetCookie(w, &http.Cookie{
Name: "ats",
Value: "",
})
http.SetCookie(w, &http.Cookie{
Name: "rt",
Value: "",
})
http.SetCookie(w, &http.Cookie{
Name: "rts",
Value: "",
})
rdio := newRdioClient()
rToken, url, err := rdio.BeginAuthentication(fmt.Sprintf(hackersBaseUrl, "rdio"))
if err != nil {
log.Println("Rdio Auth Error:", err.Error())
rest.Error(w, "Could not authenticate with Rdio.", http.StatusBadRequest, "login.get")
return
}
http.SetCookie(w, &http.Cookie{
Name: "rt",
Value: rToken.Token,
})
http.SetCookie(w, &http.Cookie{
Name: "rts",
Value: rToken.Secret,
})
w.WriteJson(&Redirect{See: url}, http.StatusTemporaryRedirect)
}
func (self *Api) RdioCallback(w *rest.ResponseWriter, r *rest.Request) {
rt := getCookie(r, "rt")
rts := getCookie(r, "rts")
if rt == nil || rts == nil {
log.Println("rt or rts not set")
rest.Error(w, "Could not authenticate with Rdio.", http.StatusBadRequest, "rdio.get")
return
}
verifier := r.URL.Query().Get("oauth_verifier")
log.Println(verifier)
rdio := newRdioClient()
err := rdio.CompleteAuthentication(rt.Value, rts.Value, verifier)
if err != nil {
log.Println("Rdio Auth Error:", err.Error())
rest.Error(w, "Could not authenticate with Rdio.", http.StatusBadRequest, "rdio.get")
return
}
http.SetCookie(w, &http.Cookie{
Name: "at",
Value: rdio.AccessToken.Token,
})
http.SetCookie(w, &http.Cookie{
Name: "ats",
Value: rdio.AccessToken.Secret,
})
http.SetCookie(w, &http.Cookie{
Name: "rt",
Value: "",
})
http.SetCookie(w, &http.Cookie{
Name: "rts",
Value: "",
})
req := http.Request{
Method: r.Method,
URL: r.URL,
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Header: r.Header,
Body: r.Body,
ContentLength: r.ContentLength,
TransferEncoding: r.TransferEncoding,
Close: r.Close,
Host: r.Host,
Form: r.Form,
PostForm: r.PostForm,
MultipartForm: r.MultipartForm,
Trailer: r.Trailer,
RemoteAddr: r.RemoteAddr,
RequestURI: r.RequestURI,
TLS: r.TLS,
}
http.Redirect(w, &req, fmt.Sprintf(hackersBaseUrl, "gpsong/"), http.StatusTemporaryRedirect)
}
func (self *Api) GetPlaybackToken(w *rest.ResponseWriter, r *rest.Request) {
at := getCookie(r, "at")
ats := getCookie(r, "ats")
if at == nil || ats == nil {
log.Println("at or ats not found.")
rest.Error(w, "Could not authenticate with Rdio.", http.StatusBadRequest, "playbackToken.get")
return
}
rdio := authedRdioClient(at.Value, ats.Value)
ret, err := rdio.GetPlaybackToken("buffalohackers.com")
if err != nil {
log.Println("Rdio Call Fail:", err.Error())
rest.Error(w, "Rdio Call Failed", http.StatusBadRequest, "playbackToken.get")
return
}
w.WriteJson(&ret, http.StatusOK)
}
func (self *Api) SearchRdio(w *rest.ResponseWriter, r *rest.Request) {
at := getCookie(r, "at")
ats := getCookie(r, "ats")
if at == nil || ats == nil {
log.Println("at or ats not found.")
rest.Error(w, "Could not authenticate with Rdio.", http.StatusBadRequest, "search.get")
return
}
q := r.URL.Query().Get("q")
rdio := authedRdioClient(at.Value, ats.Value)
options := make(map[string]string)
ret, err := rdio.Search(q, "Track", options)
if err != nil {
log.Println("Rdio Search Fail:", err.Error())
rest.Error(w, "Rdio Search Failed", http.StatusBadRequest, "search.get")
return
}
w.WriteJson(&ret, http.StatusOK)
}