-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
437 lines (390 loc) · 9.44 KB
/
main.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright (C) 2020 Evgeny Kuznetsov (evgeny@kuznetsov.md)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//go:generate go run version_generate.go
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
ipath "evgenykuznetsov.org/go/webmention.io-backup/internal/path"
)
const endpoint = "https://webmention.io/api/mentions"
type cfg struct {
filename string
token string
domain string
useJF2 bool
tlo bool
pretty bool
contentDir string
squashLeft []string
languages bool
timestamp bool
}
var version string = "custom"
func main() {
fmt.Printf("webmention.io-backup version %s\n", version)
config := cfg{}
var sl string
flag.StringVar(&config.filename, "f", "webmentions.json", "filename")
flag.StringVar(&config.token, "t", "", "API token")
flag.StringVar(&config.domain, "d", "", "domain to fetch webmentions for")
flag.BoolVar(&config.useJF2, "jf2", false, "use JF2 endpoint instead of the classic one")
flag.BoolVar(&config.tlo, "tlo", true, "wrap output in a top-level object (links list or feed)")
flag.BoolVar(&config.pretty, "p", false, "pretty-print the output (jq-style)")
flag.StringVar(&config.contentDir, "cd", "", "directory to look for structure in; if specified, attempts are made to save according to paths")
flag.StringVar(&sl, "l", "", "list of top-level subdirs to drop while saving according to paths, comma-separated")
flag.BoolVar(&config.languages, "lang", false, "insert language into the filename before extension (for Hugo page bundles)")
flag.BoolVar(&config.timestamp, "ts", false, "save timestamp to root dir file and only fetch newer mentions")
flag.Parse()
config.squashLeft = strings.Split(sl, ",")
url := endpointUrl(config)
mm, err := readFile(filepath.Join(config.contentDir, config.filename))
if err != nil && config.contentDir == "" {
fmt.Println(err)
} else {
fmt.Printf("Found %d existing webmentions, will fetch newer IDs.\n", len(mm))
}
var m []interface{}
if !config.timestamp {
m, err = getNew(url, findLast(mm))
} else {
fmt.Println("Will check for timestamp.")
m, err = getNew(url, getTimestamp(mm))
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if len(m) == 0 {
fmt.Println("No new webmentions found.")
} else {
if config.contentDir != "" {
if err := saveToDirs(m, config); err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
fmt.Printf("Appending %d new webmentions.\n", len(m))
mm = append(mm, m...)
err = writeFile(mm, config)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("Saved %d webmentions to %s.\n", len(mm), config.filename)
}
}
}
fmt.Println("All done!")
}
func readFile(fn string) (mm []interface{}, err error) {
data, err := ioutil.ReadFile(fn)
if err != nil {
return
}
mm, err = parsePage(data)
return
}
func findLast(mm []interface{}) (latest int) {
for _, f := range mm {
m, _ := f.(map[string]interface{})
id := either(m, []string{"id", "wm-id"})
if l, ok := id.(float64); ok {
this := int(l)
if this > latest {
latest = this
}
}
}
return
}
func writeFile(mm []interface{}, c cfg) error {
var bb bytes.Buffer
var f interface{}
if !c.tlo {
f = mm
} else {
if c.useJF2 {
f = struct {
Type string `json:"type"`
Name string `json:"name"`
Children []interface{} `json:"children"`
}{"feed", "Webmentions", mm}
} else {
f = struct {
Links []interface{} `json:"links"`
}{mm}
}
}
enc := json.NewEncoder(&bb)
if c.pretty {
enc.SetIndent("", " ")
}
enc.SetEscapeHTML(false)
err := enc.Encode(f)
if err != nil {
return err
}
err = ioutil.WriteFile(c.filename, bb.Bytes(), 0644)
return err
}
func saveToDirs(mm []interface{}, c cfg) (err error) {
var ts time.Time
if c.timestamp {
fn := filepath.Join(c.contentDir, c.filename)
mm, _ := readFile(fn)
ts = getTimestamp(mm)
}
for _, m := range mm {
t := timeOf(m)
if t.After(ts) {
ts = t
}
if !saveToDir(m, c) {
if err = saveToContentDir(m, c); err != nil {
return
}
}
}
if c.timestamp {
err = writeTimestamp(ts, c)
}
return
}
func saveToDir(m interface{}, c cfg) bool {
mn, _ := m.(map[string]interface{})
t := either(mn, []string{"target", "wm-target"})
tgt, ok := t.(string)
if !ok {
return false
}
dir := ipath.DirFromUrl(tgt, c.squashLeft)
if c.languages {
c.filename = ipath.FilenameFromUrl(tgt, c.squashLeft, c.filename)
}
c.contentDir = filepath.Join(c.contentDir, dir)
err := saveToContentDir(m, c)
return err == nil
}
func saveToContentDir(m interface{}, c cfg) error {
if c.filename == "" {
return fmt.Errorf("no filename specified")
}
c.filename = filepath.Join(c.contentDir, c.filename)
return saveToFile(m, c)
}
func saveToFile(m interface{}, c cfg) (err error) {
mm, _ := readFile(c.filename)
for _, exm := range mm {
if sameMention(exm, m) {
return
}
}
mm = append(mm, m)
fmt.Printf("Saving new mention to %s...", c.filename)
err = writeFile(mm, c)
if err == nil {
fmt.Println(" Saved!")
} else {
fmt.Println()
}
return
}
func sameMention(ma, mb interface{}) bool {
mapa, oka := ma.(map[string]interface{})
mapb, okb := mb.(map[string]interface{})
if !oka || !okb {
return false
}
q := []string{"source", "wm-source"}
sa, sb := either(mapa, q), either(mapb, q)
oa, oka := sa.(string)
ob, okb := sb.(string)
if !oka || !okb || oa != ob {
return false
}
q = []string{"verified_date", "wm-received"}
sa, sb = either(mapa, q), either(mapb, q)
oa, oka = sa.(string)
ob, okb = sb.(string)
ta, err := time.Parse(time.RFC3339, oa)
if err != nil {
return false
}
tb, err := time.Parse(time.RFC3339, ob)
if err != nil {
return false
}
if !oka || !okb || !ta.Equal(tb) {
return false
}
return true
}
func getPage(url string) (mm []interface{}, err error) {
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
mm, err = parsePage(b)
return
}
func parsePage(b []byte) (mm []interface{}, err error) {
var f interface{}
err = json.Unmarshal(b, &f)
// can be classic api/mentions with "links" array as a root object
// or JF2 feed
// or just an array of objects like we write it
switch m := f.(type) {
case map[string]interface{}:
mentions := either(m, []string{"links", "children"})
if mnts, ok := mentions.([]interface{}); ok {
mm = mnts
}
case []interface{}:
mm = m
default:
err = fmt.Errorf("could not parse JSON")
}
return
}
func getNew(uri string, latest interface{}) (mm []interface{}, err error) {
u, err := url.Parse(uri)
if err != nil {
return
}
q := u.Query()
switch l := latest.(type) {
case int:
q.Set("since_id", strconv.Itoa(l))
case time.Time:
q.Set("since", l.Format(time.RFC3339))
}
u.RawQuery = q.Encode()
ok := true
for i := 0; ok; i++ {
m, err := getNextPage(u, i)
if err != nil {
return mm, err
}
mm = append(mm, m...)
if len(m) == 0 {
ok = false
}
}
return
}
func getNextPage(u *url.URL, page int) (mm []interface{}, err error) {
q := u.Query()
q.Set("page", strconv.Itoa(page))
u.RawQuery = q.Encode()
mm, err = getPage(u.String())
return
}
func timeOf(m interface{}) (t time.Time) {
e, _ := m.(map[string]interface{})
tm := either(e, []string{"verified_date", "wm-received"})
if ts, ok := tm.(string); ok {
if te, err := time.Parse(time.RFC3339, ts); err == nil {
t = te
}
}
return
}
func parseTimestamp(m interface{}) (ts time.Time, ok bool) {
e, _ := m.(map[string]interface{})
t := e["timestamp"]
if tst, yep := t.(string); yep {
tm, err := time.Parse(time.RFC3339, tst)
if err == nil {
ts = tm
ok = true
}
}
return
}
func getTimestamp(mm []interface{}) (ts time.Time) {
for _, m := range mm {
if tst, ok := parseTimestamp(m); ok && tst.After(ts) {
ts = tst
}
}
return
}
func setTimestamp(mm []interface{}, ts time.Time) (mmt []interface{}) {
for _, m := range mm {
if _, ok := parseTimestamp(m); !ok {
mmt = append(mmt, m)
}
}
t := struct {
T string `json:"timestamp"`
}{ts.Format(time.RFC3339)}
mmt = append(mmt, t)
return
}
func writeTimestamp(ts time.Time, c cfg) (err error) {
fn := filepath.Join(c.contentDir, c.filename)
mm, err := readFile(fn)
if err != nil {
return
}
mm = setTimestamp(mm, ts)
c.filename = fn
err = writeFile(mm, c)
return
}
func endpointUrl(c cfg) string {
q := url.Values{}
vv := map[string]string{
"token": c.token,
"domain": c.domain,
}
for k, v := range vv {
if v != "" {
q.Set(k, v)
}
}
var e string
if c.useJF2 {
e = ".jf2"
}
ep := fmt.Sprintf("%s%s", endpoint, e)
u, _ := url.Parse(ep)
u.RawQuery = q.Encode()
return u.String()
}
// either returns the first value it finds while iterating kk for key
func either(m map[string]interface{}, kk []string) interface{} {
for _, k := range kk {
if v, ok := m[k]; ok {
return v
}
}
return nil
}