-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadwise-api.go
More file actions
147 lines (128 loc) · 3.84 KB
/
Copy pathreadwise-api.go
File metadata and controls
147 lines (128 loc) · 3.84 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type ReadwiseBooks struct {
Count int `json:"count"`
Next string `json:"next"`
Previous interface{} `json:"previous"`
Results []Book `json:"results"`
}
type Book struct {
Id int `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Category string `json:"category"`
NumHighlights int `json:"num_highlights"`
LastHighlightAt time.Time `json:"last_highlight_at"`
Updated time.Time `json:"updated"`
CoverImageUrl string `json:"cover_image_url"`
HighlightsUrl string `json:"highlights_url"`
SourceUrl interface{} `json:"source_url"`
Asin string `json:"asin"`
Tags []interface{} `json:"tags"`
MemURL string `json:"mem_url"`
MemId string `json:"mem_id"`
}
type ReadwiseHighlights struct {
Count int `json:"count"`
Next string `json:"next"`
Previous interface{} `json:"previous"`
Results []Highlight `json:"results"`
}
type Highlight struct {
Id int `json:"id"`
Text string `json:"text"`
Note string `json:"note"`
Location int `json:"location"`
LocationType string `json:"location_type"`
HighlightedAt time.Time `json:"highlighted_at"`
Url string `json:"url"`
Color string `json:"color"`
Updated time.Time `json:"updated"`
BookId int `json:"book_id"`
Tags []struct {
Id int `json:"id"`
Name string `json:"name"`
} `json:"tags"`
}
func GetHighlights(ctx Context) []Highlight {
// Get highlights (GET https://readwise.io/api/v2/highlights/)
// Create client
client := &http.Client{}
var apiResult ReadwiseHighlights
var result []Highlight
// Fetch Request
done := false
next := "https://readwise.io/api/v2/highlights/"
lastUpdate, update := GetTimeFromCache(ctx)
for !done {
req, err := http.NewRequest("GET", next, nil)
req.Header.Add("Authorization", "Token "+ctx.config.ReadwiseKey)
if update {
q := req.URL.Query()
q.Add("updated__gt", lastUpdate)
req.URL.RawQuery = q.Encode()
// log.Println("get highlights URL: ", req.URL.String())
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
respBody, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBody, &apiResult)
result = append(result, apiResult.Results...)
log.Print("Got ", len(result), " of ", apiResult.Count, " highlights")
if apiResult.Next == "" || apiResult.Next == next {
done = true
} else {
next = apiResult.Next
}
}
// Read Response Body
return result
}
func GetBooks(ctx Context) []Book {
// Get Books (GET https://readwise.io/api/v2/books/)
// Create client
client := &http.Client{}
var apiResult ReadwiseBooks
var result []Book
// Fetch Request
done := false
next := "https://readwise.io/api/v2/books/"
lastUpdate, update := GetTimeFromCache(ctx)
for !done {
req, err := http.NewRequest("GET", next, nil)
req.Header.Add("Authorization", "Token "+ctx.config.ReadwiseKey)
if update {
q := req.URL.Query()
q.Add("updated__gt", lastUpdate)
req.URL.RawQuery = q.Encode()
// log.Println("get books URL: ", req.URL.String())
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBody, &apiResult)
if err != nil {
log.Print("Error unmarshalling highlights", err)
}
result = append(result, apiResult.Results...)
log.Print("Got ", len(result), " of ", apiResult.Count, " books")
if apiResult.Next == "" || apiResult.Next == next {
done = true
} else {
next = apiResult.Next
}
}
return result
}