-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomark.go
364 lines (276 loc) · 6.47 KB
/
gomark.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package gomark
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"strings"
"time"
)
var YoutubeKey string
type Database struct {
Bookmarks map[string]Bookmark
Filename string
}
func (d *Database) AddBookmark(b *Bookmark) {
d.Bookmarks[b.GetURL()] = *b
}
func (d *Database) GetBookmarks() map[string]Bookmark {
return d.Bookmarks
}
func (d *Database) GetBookmark(url string) (b *Bookmark, err error) {
book, ok := d.Bookmarks[url]
if !ok {
return nil, errors.New(fmt.Sprintf("Bookmark not found: %s", url))
}
b = &book
return
}
func (d *Database) DeleteBookmark(b *Bookmark) {
delete(d.Bookmarks, b.GetURL())
}
func (d *Database) Dump() error {
if len(d.Filename) == 0 {
return fmt.Errorf("No file specified")
}
b, err := json.Marshal(d)
if err != nil {
return err
}
err = ioutil.WriteFile(d.Filename, b, 0600)
return err
}
func NewDatabaseFromFile(filename string) (d *Database, err error) {
d = NewDatabase()
d.Filename = filename
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
// If the file is empty just return a new DB
if len(b) == 0 {
return
}
err = json.Unmarshal(b, d)
if err != nil {
return nil, err
}
return d, err
}
func NewDatabase() (d *Database) {
d = new(Database)
d.Bookmarks = make(map[string]Bookmark)
return
}
type Bookmark struct {
Title string
Date time.Time
RawUrl string
info bookmarkInfo // Needed to serialize easily the private attributes
}
type bookmarkInfo struct {
Url url.URL
Tags map[string]struct{}
}
// Custom JSON
// From http://choly.ca/post/go-json-marshalling/
func (d Bookmark) MarshalJSON() ([]byte, error) {
// We need the alias otherwise it would inherite the methods
// and MarshalJSON would be call infinitely
type alias Bookmark
return json.Marshal(&struct {
Url string
Tags []string
alias
}{
d.GetURL(),
d.GetTags(),
(alias)(d)})
}
func (d *Bookmark) UnmarshalJSON(data []byte) error {
type alias Bookmark
aux := &struct {
Url string
Tags []string
*alias
}{
"",
[]string{},
(*alias)(d)}
err := json.Unmarshal(data, &aux)
if err != nil {
return err
}
url, err := url.Parse(aux.Url)
if err != nil {
return err
}
d.info.Url = *url
d.ResetTags()
d.AddTags(aux.Tags...)
return nil
}
func NewBookmark() *Bookmark {
b := new(Bookmark)
b.info.Tags = make(map[string]struct{})
b.Date = time.Now()
return b
}
func GetTitle(theUrl *url.URL) (title string, err error) {
if YoutubeKey != "" && theUrl.Hostname() == "www.youtube.com" {
title, err = GetTitleYoutube(theUrl)
if err != nil {
log.Printf("Failed to use the youtube API: %s", err)
} else {
return
}
}
return GetTitleGeneric(theUrl)
}
func GetTitleYoutube(theUrl *url.URL) (title string, err error) {
videoId, err := getParam(theUrl.Query(), "v")
if err != nil {
return
}
apiUrl := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?id=%s&key=%s&part=snippet", videoId, YoutubeKey)
resp, err := http.Get(apiUrl)
if err != nil {
return
}
type snippet struct {
Title string `json:"title"`
ChannelTitle string `json:"channelTitle"`
}
type item struct {
Snippet snippet `json:"snippet"`
}
type ErrorResponse struct {
Errors []map[string]string `json:"errors"`
Code int `json:"code"`
Message string `json:"message"`
}
type respStruct struct {
PageInfo map[string]int `json:"pageInfo"`
Items []item `json:"items"`
Error ErrorResponse `json:"error"`
}
var data respStruct
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return
}
if data.Error.Code != 0 {
fmt.Println("Error")
return "", fmt.Errorf("Impossimple to Retrieve Youtube Data: %s", data.Error.Message)
}
video := data.Items[0].Snippet
title = fmt.Sprintf("%s: %s", video.ChannelTitle, video.Title)
return
}
func getParam(values url.Values, name string) (value string, err error) {
tmp, ok := values[name]
if !ok {
err = fmt.Errorf("Param %s not found", name)
return
}
value = tmp[0]
return
}
func GetTitleGeneric(theUrl *url.URL) (title string, err error) {
rawUrl := theUrl.String()
client := &http.Client{}
req, err := http.NewRequest("GET", rawUrl, nil)
if err != nil {
err = fmt.Errorf("Error while creating the request for the page %s: %v", rawUrl, err)
return
}
// User Wget User Agent gets better result with Youtube
req.Header.Set("User-Agent", "Wget/1.19.1 (linux-gnu)")
res, err := client.Do(req)
if err != nil {
err = fmt.Errorf("Error while getting the page %s: %v", rawUrl, err)
return
}
// Getting the title
head := make([]byte, 2000)
_, err = res.Body.Read(head)
if err != nil && err != io.EOF {
// As parsing the title is non mandatory no error is returned
err = fmt.Errorf("Error while reading the page %s: %v", rawUrl, err)
return
}
re := regexp.MustCompile("(?s)<title.*?>(.+)</title>")
matches := re.FindStringSubmatch(string(head))
if len(matches) == 0 {
rest, erra := ioutil.ReadAll(res.Body)
if erra != nil && erra != io.EOF {
err = fmt.Errorf("Error while reading the full page %s: %v", rawUrl, erra)
return
}
bodyText := append(head, rest...)
matches = re.FindStringSubmatch(string(bodyText))
}
if len(matches) == 0 {
err = fmt.Errorf("No Title Found")
return
}
title = matches[1]
return
}
func NewBookmarkUrl(rawUrl string) (*Bookmark, error) {
tmp, err := url.Parse(rawUrl)
if err != nil {
return nil, err
}
b := NewBookmark()
b.info.Url = *tmp
b.RawUrl = rawUrl
title, err := GetTitle(tmp)
if err != nil {
b.Title = b.RawUrl
log.Printf("Impossible to retrieve title for url %s: %v", b.RawUrl, err)
} else {
b.Title = title
}
return b, nil
}
func (b *Bookmark) ResetTags(tags ...string) {
b.info.Tags = make(map[string]struct{})
}
func (b *Bookmark) AddTags(tags ...string) {
for _, tag := range tags {
tag = strings.ToLower(tag)
if _, found := b.info.Tags[tag]; !found {
b.info.Tags[tag] = struct{}{}
}
}
}
func (b *Bookmark) DeleteTags(tags ...string) {
for _, tag := range tags {
tag = strings.ToLower(tag)
delete(b.info.Tags, tag)
}
}
func (b *Bookmark) GetTags(tags ...string) (tagSet []string) {
for tag := range b.info.Tags {
tagSet = append(tagSet, tag)
}
return
}
func (b *Bookmark) HasTags(tags ...string) bool {
for _, tag := range tags {
tag = strings.ToLower(tag)
if _, found := b.info.Tags[tag]; !found {
return false
}
}
return true
}
func (b *Bookmark) GetURL() string {
return b.info.Url.String()
}