/
readwise.go
115 lines (91 loc) · 2.99 KB
/
readwise.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
package source
import (
"fmt"
"os"
"github.com/elchead/misou/search"
"github.com/gocarina/gocsv"
"github.com/pkg/errors"
)
var readwiseProvider = "readwise"
// var baseUrl = "https://readwise.io/api/v2/"
// type ReadwiseClient struct {
// http *http.Client
// token string
// }
// func NewReadwiseClient(token string) *ReadwiseClient {
// return &ReadwiseClient{&http.Client{},"Token "+token}
// }
// type payload struct {
// Count int `json:"count"`
// Results []result `json:"results"`
// }
// type result struct {
// Id int `json:"id"`
// Text string `json:"text"`
// Note string `json:"note"`
// HiglightedAt time.Time `json:"highlighted_at"`
// Url string `json:"url"`
// BookId int `json:"book_id"`
// Tags []string `json:"tags"`
// }
// //only returns small subset and incomplete info about title, etc from HTTP request.
// func (c *ReadwiseClient) GetData() *payload {
// req, _ := http.NewRequest("GET",baseUrl+"highlights/",nil)
// req.Header.Set("Authorization",c.token)
// resp, _ := c.http.Do(req)
// var data *payload
// jsoniter.NewDecoder(resp.Body).Decode(&data)
// return data
// }
// func (c *ReadwiseClient) ParseData(data *payload) []*search.IndexData {
// fmt.Printf("Len: %d, Count: %d",len(data.Results),data.Count)
// res := make([]*search.IndexData, len(data.Results))
// for i,entry := range data.Results {
// res[i] = &search.IndexData{Title: "",Link: entry.Url, Content: fmt.Sprintf("%s\nNote: %s", entry.Text,entry.Note), Provider: readwiseProvider, ContentType: "text"}
// }
// return res
// }
type readwiseEntry struct {
Highlight string `csv:"Highlight"`
Title string `csv:"Book Title"`
Author string `csv:"Book Author"`
Note string `csv:"Note"`
}
func UnmarshalEntriesFromCSV(in *os.File) ([]*readwiseEntry,error) {
var entries []*readwiseEntry
err := gocsv.UnmarshalFile(in, &entries)
return entries, err
}
type ReadwiseCSVParser struct {
File *os.File
}
func NewReadwiseCSVParser(file *os.File) *ReadwiseCSVParser {
c := &ReadwiseCSVParser{File: file}
return c
}
// TODO refactor to care less about search implementation
func (c ReadwiseCSVParser) TransformToData() ([]*search.IndexData,error) {
entries, err := UnmarshalEntriesFromCSV(c.File)
if err != nil {
return nil,errors.Wrap(err,"unable to unmarshal entries from Readwise CSV")
}
res := make([]*search.IndexData, len(entries))
for i,entry := range entries {
res[i] = &search.IndexData{Title: entry.Title,Link: "", Content: fmt.Sprintf("%s\nNote: %s", entry.Highlight,entry.Note), Provider: readwiseProvider, ContentType: "text"}
}
return res,nil
}
func (c *ReadwiseCSVParser) Search(keywords string) ([]search.SearchResult,error) {
return search.Search(keywords)
}
// TODO delete
// func (r ReadwiseCSVParser) Register() error {
// entries, err := UnmarshalEntriesFromCSV(r.File)
// if err != nil {
// return errors.Wrap(err,"unable to unmarshal entries from Readwise CSV")
// }
// data := r.TransformToData(entries)
// AddEntries(data)
// GenerateIndexer()
// return nil
// }