This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
/
goodreads.go
316 lines (276 loc) · 8.68 KB
/
goodreads.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package api
import (
"context"
"encoding/base64"
"encoding/json"
"encoding/xml"
"fmt"
"log"
"net/http"
"net/url"
"time"
"github.com/gomodule/oauth1/oauth"
"github.com/gorilla/mux"
)
var (
goodreadsCredentialsContextKey = "rfa_goodreads_creds"
goodreadsUserIDContextKey = "rfa_goodreads_user_id"
)
func (api *API) WithGoodreadsCredentials(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
userIDVal := r.Context().Value(userIDContextKey)
if userIDVal == nil {
log.Println("missing user ID in context")
w.WriteHeader(http.StatusInternalServerError)
return
}
userID := userIDVal.(string)
creds := &oauth.Credentials{}
err := api.db.QueryRow("SELECT token, secret FROM goodreads_tokens WHERE user_id = $1", userID).
Scan(&creds.Token, &creds.Secret)
if err != nil {
http.Error(w, "missing Goodreads token", http.StatusUnauthorized)
return
}
f(w, r.WithContext(context.WithValue(r.Context(), goodreadsCredentialsContextKey, creds)))
}
}
type goodreadsAuthUserResponse struct {
XMLName xml.Name `xml:"GoodreadsResponse"`
User struct {
ID string `xml:"id,attr"`
} `xml:"user"`
}
func (api *API) WithGoodreadsUserID(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
credsVal := r.Context().Value(goodreadsCredentialsContextKey)
if credsVal == nil {
http.Error(w, "missing Goodreads token", http.StatusUnauthorized)
return
}
creds := credsVal.(*oauth.Credentials)
resp, err := api.goodreads.Get(nil, creds, "https://www.goodreads.com/api/auth_user", nil)
if err != nil {
http.Error(w, "Internal server error", 500)
return
}
authUserResponse := goodreadsAuthUserResponse{}
err = xml.NewDecoder(resp.Body).Decode(&authUserResponse)
if err != nil {
http.Error(w, "Internal server error: "+err.Error(), 500)
return
}
f(w, r.WithContext(context.WithValue(r.Context(), goodreadsUserIDContextKey, authUserResponse.User.ID)))
}
}
func (api *API) HandleGoodreadsAuth(w http.ResponseWriter, r *http.Request) {
userIDVal := r.Context().Value(userIDContextKey)
if userIDVal == nil {
log.Println("missing user ID in context")
w.WriteHeader(http.StatusInternalServerError)
return
}
callbackURL := "https://www.readfaster.app/goodreads/callback"
if api.devMode {
callbackURL = "http://" + r.Host + "/goodreads/callback"
}
tempCred, err := api.goodreads.RequestTemporaryCredentials(nil, callbackURL, nil)
if err != nil {
http.Error(w, "Error getting temp cred, "+err.Error(), 500)
return
}
marshaled, err := json.Marshal(tempCred)
if err != nil {
http.Error(w, "Error marshaling temp cred, "+err.Error(), 500)
return
}
// Store temp creds in cookie
http.SetCookie(w, &http.Cookie{
Name: "rfa_goodreads_token",
Value: base64.StdEncoding.EncodeToString(marshaled),
Path: "/",
Expires: time.Now().Add(7 * 24 * time.Hour),
Secure: !api.devMode,
HttpOnly: true,
})
http.Redirect(w, r, api.goodreads.AuthorizationURL(tempCred, map[string][]string{
"oauth_callback": {callbackURL},
}), 302)
}
func (api *API) HandleGoodreadsCallback(w http.ResponseWriter, r *http.Request) {
userIDVal := r.Context().Value(userIDContextKey)
if userIDVal == nil {
log.Println("missing user ID in context")
w.WriteHeader(http.StatusInternalServerError)
return
}
userID := userIDVal.(string)
tokenCookie, err := r.Cookie("rfa_goodreads_token")
if err != nil {
http.Error(w, "Missing token, "+err.Error(), 500)
return
}
decodedCookie, err := base64.StdEncoding.DecodeString(tokenCookie.Value)
if err != nil {
http.Error(w, "Bad token, "+err.Error(), 500)
return
}
tempCred := &oauth.Credentials{}
err = json.Unmarshal(decodedCookie, &tempCred)
if err != nil {
if err != nil {
http.Error(w, "Bad token, "+err.Error(), 500)
return
}
}
if tempCred.Token != r.FormValue("oauth_token") {
http.Error(w, "Unknown oauth_token.", 500)
return
}
tokenCred, _, err := api.goodreads.RequestToken(nil, tempCred, r.FormValue("oauth_verifier"))
if err != nil {
http.Error(w, "Error getting request token, "+err.Error(), 500)
return
}
_, err = api.db.Exec("INSERT INTO goodreads_tokens (user_id, token, secret) VALUES ($1, $2, $3) ON CONFLICT (user_id) DO UPDATE SET token = $2, secret = $3",
userID, tokenCred.Token, tokenCred.Secret)
if err != nil {
http.Error(w, "error saving token, "+err.Error(), 500)
return
}
http.Redirect(w, r, "/app", 302)
}
type goodreadsReviewListResponse struct {
XMLName xml.Name `xml:"GoodreadsResponse"`
Reviews struct {
Review []struct {
ID int `xml:"id"`
Book struct {
ID int `xml:"id"`
Title string `xml:"title"`
ImageURL string `xml:"image_url"`
NumPages int `xml:"num_pages"`
Authors struct {
Author []struct {
Name string `xml:"name"`
} `xml:"author"`
} `xml:"authors"`
} `xml:"book"`
} `xml:"review"`
} `xml:"reviews"`
}
type goodreadsReviewResponse struct {
XMLName xml.Name `xml:"GoodreadsResponse"`
Review struct {
UserStatuses struct {
UserStatus []struct {
Page int `xml:"page"`
Percent int `xml:"percent"`
} `xml:"user_status"`
} `xml:"user_statuses"`
} `xml:"review"`
}
type GoodreadsBook struct {
ID int `json:"id"`
Title string `json:"title"`
ImageURL string `json:"image_url"`
Authors []string `json:"authors"`
NumPages int `json:"num_pages"`
Progress struct {
Page int `json:"page"`
Percent int `json:"percent"`
} `json:"progress"`
}
func (api *API) HandleAPIGetGoodreadsReviews(w http.ResponseWriter, r *http.Request) {
credsVal := r.Context().Value(goodreadsCredentialsContextKey)
if credsVal == nil {
http.Error(w, "missing Goodreads token", http.StatusUnauthorized)
return
}
creds := credsVal.(*oauth.Credentials)
goodreadsUserIDVal := r.Context().Value(goodreadsUserIDContextKey)
if goodreadsUserIDVal == nil {
http.Error(w, "missing Goodreads user ID", http.StatusUnauthorized)
return
}
goodreadsUserID := goodreadsUserIDVal.(string)
resp, err := api.goodreads.Get(nil, creds,
fmt.Sprintf("https://www.goodreads.com/review/list/%s.xml", goodreadsUserID), url.Values{
"v": []string{"2"},
"shelf": []string{"currently-reading"},
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
goodreadsResponse := goodreadsReviewListResponse{}
err = xml.NewDecoder(resp.Body).Decode(&goodreadsResponse)
if err != nil {
http.Error(w, "Internal server error: "+err.Error(), 500)
return
}
books := []GoodreadsBook{}
for _, review := range goodreadsResponse.Reviews.Review {
book := review.Book
authors := []string{}
for _, author := range book.Authors.Author {
authors = append(authors, author.Name)
}
reviewResponse := goodreadsReviewResponse{}
resp, err = api.goodreads.Get(nil, creds,
fmt.Sprintf("https://www.goodreads.com/review/show.xml"), url.Values{
"id": []string{fmt.Sprint(review.ID)},
})
if err != nil {
http.Error(w, "Internal server error: "+err.Error(), 500)
return
}
err = xml.NewDecoder(resp.Body).Decode(&reviewResponse)
if err != nil {
http.Error(w, "Internal server error: "+err.Error(), 500)
return
}
bookResponse := GoodreadsBook{
ID: book.ID,
Title: book.Title,
ImageURL: book.ImageURL,
NumPages: book.NumPages,
Authors: authors,
}
if len(reviewResponse.Review.UserStatuses.UserStatus) > 0 {
status := reviewResponse.Review.UserStatuses.UserStatus[0]
bookResponse.Progress.Page = status.Page
bookResponse.Progress.Percent = status.Percent
}
books = append(books, bookResponse)
}
w.Header().Add("content-type", "application/json")
w.Header().Add("cache-control", "max-age=60")
json.NewEncoder(w).Encode(map[string]interface{}{
"books": books,
})
}
func (api *API) HandleAPIPostGoodreadsProgress(w http.ResponseWriter, r *http.Request) {
credsVal := r.Context().Value(goodreadsCredentialsContextKey)
if credsVal == nil {
http.Error(w, "missing Goodreads token", http.StatusUnauthorized)
return
}
creds := credsVal.(*oauth.Credentials)
goodreadsUserIDVal := r.Context().Value(goodreadsUserIDContextKey)
if goodreadsUserIDVal == nil {
http.Error(w, "missing Goodreads user ID", http.StatusUnauthorized)
return
}
goodreadsBook := mux.Vars(r)["goodreads_book_id"]
_, err := api.goodreads.Post(nil, creds,
"https://www.goodreads.com/user_status.xml", url.Values{
"user_status[book_id]": []string{goodreadsBook},
"user_status[percent]": []string{r.URL.Query().Get("percent")},
})
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}