-
Notifications
You must be signed in to change notification settings - Fork 0
/
skimmer.go
858 lines (811 loc) · 21.5 KB
/
skimmer.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
package skimmer
import (
"bufio"
"bytes"
"database/sql"
"encoding/json"
"fmt"
"html"
"io"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strings"
"time"
// 3rd Party Packages
_ "github.com/glebarez/go-sqlite"
"github.com/kayako/bluemonday"
"github.com/mmcdole/gofeed"
)
// ParseURLList takes a filename and byte slice source, parses the contents
// returning a map of urls to labels and an error value.
func ParseURLList(fName string, src []byte) (map[string]*FeedSource, error) {
urls := map[string]*FeedSource{}
// Parse the url value collecting our keys and values
s := bufio.NewScanner(bytes.NewBuffer(src))
key, val, userAgent := "", "", ""
line := 1
for s.Scan() {
txt := strings.TrimSpace(s.Text())
if strings.HasPrefix(txt, "#") {
txt = ""
}
if txt != "" {
parts := strings.SplitN(txt, ` "`, 2)
switch len(parts) {
case 1:
key, val, userAgent = parts[0], "", ""
case 2:
key, val, userAgent = parts[0], parts[1], ""
pos := strings.LastIndex(val, `"`)
if pos > -1 {
if len(val) > pos {
userAgent = strings.TrimSpace(val[pos+1:])
}
val = strings.TrimSpace(val[0:pos])
}
val = strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(val, `~`), `"`))
}
urls[key] = &FeedSource{
Url: key,
Label: val,
UserAgent: userAgent,
}
}
line++
}
return urls, nil
}
// FeedSource describes the source of a feed. It includes the URL,
// an optional label, user agent string.
type FeedSource struct {
Url string `json:"url,omitempty"`
Label string `json:"label,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
}
// Skimmer is the application structure that holds configuration
// and ties the app to the runner for the cli.
type Skimmer struct {
// AppName holds the name of the application
AppName string `json:"app_name,omitempty"`
// UserAgent holds the user agent string used by skimmer.
// Right now I plan to default it to
// app.AppName + "/" + app.Version + " (" + ReleaseDate + "." + ReleaseHash + ")"
UserAgent string `json:"user_agent,omitempty"`
// DbName holds the path to the SQLite3 database
DBName string `json:"db_name,omitempty"`
// Urls are the map of urls to labels to be fetched or read
Urls map[string]*FeedSource `json:"urls,omitempty"`
// Limit contrains the number of items shown
Limit int `json:"limit,omitempty"`
// Prune contains the date to use to prune the database.
Prune bool `json:"prune,omitempty"`
// Interactive if true causes Run to display one item at a time with a minimal of input
Interactive bool `json:"interactive,omitempty"`
// AsURLs, output the skimmer feeds as a newsboat style url file
AsURLs bool `json:"urls,omitempty"`
// Map in some private data to keep things easy to work with.
in io.Reader
out io.Writer
eout io.Writer
}
func NewSkimmer(appName string) (*Skimmer, error) {
app := new(Skimmer)
app.AppName = appName
app.UserAgent = fmt.Sprintf("User-Agent: %s/%s", app.AppName, strings.TrimPrefix(Version, "v"))
return app, nil
}
// Setup checks to see if anything needs to be setup (or fixed) for skimmer to run.
func (app *Skimmer) Setup(fPath string) error {
// Check if we have an appDir
bName := path.Base(fPath)
xName := path.Ext(bName)
fName := fPath
if xName != ".skim" {
fName = strings.TrimSuffix(fPath, xName) + ".skim"
// Check to see if we have an existing skimmer file
if _, err := os.Stat(fName); os.IsNotExist(err) {
stmt := fmt.Sprintf(SQLCreateTables, app.AppName, time.Now().Format("2006-01-02"))
dsn := fName
db, err := sql.Open("sqlite", dsn)
if err != nil {
return err
}
defer db.Close()
if db == nil {
return fmt.Errorf("%s opened and returned nil", fName)
}
_, err = db.Exec(stmt)
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
}
}
app.DBName = fName
return nil
}
// ReadUrls reads urls or OPML file provided and updates the feeds in the skimmer
// skimmer file.
//
// Newsboat's url file format is `<URL><SPACE>"~<LABEL>"` one entry per line
// The hash mark, "#" at the start of the line indicates a comment line.
//
// OPML is documented at http://opml.org
func (app *Skimmer) ReadUrls(fName string) error {
src, err := os.ReadFile(fName)
if err != nil {
return err
}
app.Urls, err = ParseURLList(fName, src)
if err != nil {
return err
}
if len(app.Urls) == 0 {
return fmt.Errorf("no urls found")
}
return nil
}
func (app *Skimmer) redirectHandler (req *http.Request, via []*http.Request) error {
if len(via) >= 5 {
urlList := []string{}
for _, redirect := range via {
urlList = append(urlList, redirect.URL.String())
}
return fmt.Errorf("stopped after 5 redirectos: %s", strings.Join(urlList, ", "))
}
fmt.Fprintf(app.eout, "redirecting to %s because %s\n", req.URL.String(), http.ErrUseLastResponse)
return http.ErrUseLastResponse
}
// webget retrieves a feed and parses it.
// Uses mmcdole's gofeed, see docs at https://pkg.go.dev/github.com/mmcdole/gofeed
func (app *Skimmer) webget(href string, userAgent string) (*gofeed.Feed, error) {
// NOTE: I'm assuming only http, https at this point, later this will
// need to be split up so I can handle Gopher, Gemini and sftp.
client := &http.Client{
CheckRedirect: app.redirectHandler,
}
req, err := http.NewRequest("GET", href, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
// Set the accepted content types.
req.Header.Set("accept", "application/rss+xml, application/atom+xml, application/feed+json, application/xml, application/json;q=0.9, */*;q=0.8")
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error: %s", res.Status)
}
// See if we can clean up some stuff that'll break feed parsing
src, err := io.ReadAll(res.Body)
if err != err {
return nil, err
}
src = bytes.ReplaceAll(src , []byte(``), []byte(``))
buf := bytes.NewBuffer(src)
fp := gofeed.NewParser()
feed, err := fp.Parse(buf)
if err != nil {
return nil, fmt.Errorf("feed error for %q, %s", href, err)
}
if feed.Link == "" || feed.Link == "/" {
u, err := url.Parse(href)
if err != nil {
return nil, err
}
u.Path = "/"
feed.Link = u.String()
}
return feed, nil
}
// SaveChannel will write the Channel information to a skimmer channel table.
func SaveChannel(db *sql.DB, link string, feedLabel string, channel *gofeed.Feed) error {
/*
link, title, description, feed_link, links,
updated, published,
authors, language, copyright, generator,
categories, feed_type, feed_version
*/
var (
err error
src []byte
title string
linksStr string
authorsStr string
categoriesStr string
)
linksStr = link
title = feedLabel
if feedLabel == "" {
title = channel.Title
}
if channel.Links != nil {
src, err = JSONMarshal(channel.Links)
if err != nil {
return err
}
linksStr = fmt.Sprintf("%s", src)
}
if channel.Authors != nil {
src, err = JSONMarshal(channel.Authors)
if err != nil {
return err
}
authorsStr = fmt.Sprintf("%s", src)
}
if channel.Categories != nil {
src, err = JSONMarshal(channel.Categories)
if err != nil {
return err
}
categoriesStr = fmt.Sprintf("%s", src)
}
stmt := SQLUpdateChannel
_, err = db.Exec(stmt,
&link, &title, channel.Description, channel.FeedLink, linksStr,
channel.Updated, channel.Published,
authorsStr, channel.Language, channel.Copyright, channel.Generator,
categoriesStr, channel.FeedType, channel.FeedVersion)
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
return nil
}
// SaveItem saves a gofeed item to the item table in the skimmer database
func SaveItem(db *sql.DB, feedLabel string, item *gofeed.Item) error {
var (
published string
updated string
)
if item.UpdatedParsed != nil {
updated = item.UpdatedParsed.Format("2006-01-02 15:04:05")
}
if item.PublishedParsed != nil {
published = item.PublishedParsed.Format("2006-01-02 15:04:05")
}
var (
dcExt []byte
authors []byte
err error
)
if item.Authors != nil {
authors, err = json.Marshal(item.Authors)
if err != nil {
return fmt.Errorf("failed to marshal item.Authors, %s", err)
}
//fmt.Fprintf(os.Stderr, "DEBUG authors => %s\n", authors)
}
if item.DublinCoreExt != nil {
dcExt, err = json.Marshal(item.DublinCoreExt)
if err != nil {
return fmt.Errorf("failed to marshal item.DublinCoreExt, %s", err)
}
//fmt.Fprintf(os.Stderr, "DEBUG dc_ext => %s\n", dcExt)
}
stmt := SQLUpdateItem
_, err = db.Exec(stmt,
item.Link, item.Title,
item.Description, updated, published,
feedLabel, string(authors), string(dcExt))
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
return nil
}
func (app *Skimmer) ResetChannels(db *sql.DB) error {
stmt := SQLResetChannels
_, err := db.Exec(stmt)
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
return nil
}
func (app *Skimmer) MarkItem(db *sql.DB, link string, val string) error {
stmt := SQLMarkItem
_, err := db.Exec(stmt, val, link)
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
return nil
}
func (app *Skimmer) TagItem(db *sql.DB, link string, tag string) error {
stmt := SQLTagItem
_, err := db.Exec(stmt, tag, link)
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
return nil
}
// ChannelsToUrls converts the current channels table to Urls formated output
// and refreshes app.Urls data structure.
func (app *Skimmer) ChannelsToUrls(db *sql.DB) ([]byte, error) {
stmt := SQLChannelsAsUrls
rows, err := db.Query(stmt)
if err != nil {
return nil, fmt.Errorf("%s\nstmt: %s", err, stmt)
}
defer rows.Close()
lines := []string{}
if app.Urls == nil {
app.Urls = map[string]*FeedSource{}
}
for rows.Next() {
var (
link string
title string
)
if err := rows.Scan(&link, &title); err != nil {
return nil, err
}
if strings.HasPrefix(title, `"`) {
title = strings.Trim(title, `"~`)
}
if link != "" {
if title != "" {
lines = append(lines, fmt.Sprintf(`%s "~%s"%s`, link, title, "\n"))
app.Urls[link].Label = title
} else {
lines = append(lines, fmt.Sprintf(`%s%s`, link, "\n"))
}
app.Urls[link].Url = link
}
}
if err = rows.Err(); err != nil {
return nil, err
}
return []byte(strings.Join(lines, "")), nil
}
// Download the contents from app.Urls
func (app *Skimmer) Download(db *sql.DB) error {
eCnt := 0
/*
dsn := app.DBName
db, err := sql.Open("sqlite", dsn)
if err != nil {
return err
}
defer db.Close()
*/
for k, v := range app.Urls {
userAgent := v.UserAgent
if userAgent == "" {
userAgent = app.UserAgent
}
feed, err := app.webget(k, userAgent)
if err != nil {
eCnt++
fmt.Fprintf(app.eout, "failed to get %q, %s\n", k, err)
continue
}
if err := SaveChannel(db, k, v.Label, feed); err != nil {
fmt.Fprintf(app.eout, "failed to save chanel %q, %s\n", k, err)
continue
}
// Setup a progress output
t0 := time.Now()
rptTime := time.Now()
reportProgress := false
tot := feed.Len()
fmt.Fprintf(app.out, "processing %d items from %s %s\n", tot, v.Label, v.UserAgent)
i := 0
for _, item := range feed.Items {
if strings.HasPrefix(item.Link, "/") {
item.Link = fmt.Sprintf("%s%s", strings.TrimSuffix(feed.Link, "/"), item.Link)
}
// Add items from feed to database table
if err := SaveItem(db, v.Label, item); err != nil {
return err
}
if rptTime, reportProgress = CheckWaitInterval(rptTime, (20 * time.Second)); reportProgress {
fmt.Fprintf(app.out, "(%d/%d) %s", i, tot, ProgressETA(t0, i, tot))
}
i++
}
fmt.Fprintf(app.out, "processed %d/%d from %s %s\n", i, tot, v.Label, v.UserAgent)
}
if eCnt > 0 {
return fmt.Errorf("%d errors encounter downloading feeds", eCnt)
}
return nil
}
// ItemCount returns the total number items in the database.
func (app *Skimmer) ItemCount(db *sql.DB) (int, error) {
stmt := SQLItemCount
rows, err := db.Query(stmt)
if err != nil {
return -1, fmt.Errorf("%s\nstmt: %s", err, stmt)
}
defer rows.Close()
cnt := 0
for rows.Next() {
if err := rows.Scan(&cnt); err != nil {
return -1, err
}
}
if err := rows.Err(); err != nil {
return -1, err
}
return cnt, nil
}
// PruneItems takes a timestamp and performs a row delete on the table
// for items that are older than the timestamp.
func (app *Skimmer) PruneItems(db *sql.DB, pruneDT time.Time) error {
stmt := SQLPruneItems
dt := pruneDT.Format("2006-01-02 15:04:05")
_, err := db.Exec(stmt, dt, dt)
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
return err
}
func (app *Skimmer) DisplayItem(link string, title string, description string, updated string, published string, label string, tags string) error {
// Then see about formatting things.
pressTime := published
if updated != "" {
pressTime = updated
}
if len(pressTime) > 10 {
pressTime = pressTime[0:10]
}
if description != "" {
if title == "" {
title = fmt.Sprintf("@%s (date: %s)", label, pressTime)
} else {
title = fmt.Sprintf("## %s\n\ndate: %s", title, pressTime)
}
fmt.Fprintf(app.out, `---
%s
%s
<%s>
%s
`, title, description, link, tags)
} else if title != "" {
fmt.Fprintf(app.out, `---
## %s
<%s>
`, title, link)
}
return nil
}
// Display the contents from database
func (app *Skimmer) Write(db *sql.DB) error {
stmt := SQLDisplayItems
if app.Limit > 0 {
stmt = fmt.Sprintf("%s LIMIT %d", stmt, app.Limit)
}
rows, err := db.Query(stmt, "")
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
defer rows.Close()
for rows.Next() {
var (
link string
title string
description string
updated string
published string
label string
tags string
)
if err := rows.Scan(&link, &title, &description, &updated, &published, &label, &tags); err != nil {
fmt.Fprintf(app.eout, "%s\n", err)
continue
}
if err := app.DisplayItem(link, title, description, updated, published, label, tags); err != nil {
return err
}
}
if err := rows.Err(); err != nil {
return err
}
return nil
}
// normalizeTFormat sorts out the format string to used to parse the datestamp/timestamp
func normalizeTFormat(t string) (string, error) {
fmtStr := "2006-01-02"
switch {
case t == "today":
fmtStr = "2006-01-02"
t = time.Now().Format(fmtStr)
case t == "now":
fmtStr = "2006-01-02 15:04:05"
t = time.Now().Format(fmtStr)
case len(t) == 10:
fmtStr = "2006-01-02"
case len(t) == 12:
fmtStr = "2006-01-02 15"
case len(t) == 15:
fmtStr = "2006-01-02 15:04"
case len(t) == 18:
fmtStr = "2006-01-02 15:04:05"
default:
return "", fmt.Errorf("bad prune date %q", t)
}
return fmtStr, nil
}
func displayStats(out io.Writer, db *sql.DB, dbName string) error {
stmt := SQLItemStats
rows, err := db.Query(stmt)
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
defer rows.Close()
fmt.Fprintf(out, "stats: %s\n\tstatus\tcount\n", dbName)
for rows.Next() {
var (
status string
cnt int
)
if err := rows.Scan(&status, &cnt); err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
fmt.Fprintf(out, "\t%s\t%d\n", status, cnt)
}
return nil
}
// RunInteractive provides a sliver of interactive UI, basically displaying an item then
// prompting for an action.
func (app *Skimmer) RunInteractive(db *sql.DB) error {
SetupScreen(app.out)
ClearScreen()
// Get the item count
tot, err := app.ItemCount(db)
if err != nil {
return err
}
padding := int(len(fmt.Sprintf("%d", tot)))
promptStr := "(n)ext, (s)ave, (q)uit %" + fmt.Sprintf("%d", padding) + fmt.Sprintf("d/%d > ", tot)
stmt := SQLDisplayItems
if app.Limit > 0 {
stmt = fmt.Sprintf("%s LIMIT %d", stmt, app.Limit)
}
rows, err := db.Query(stmt, "")
if err != nil {
return fmt.Errorf("%s\nstmt: %s", err, stmt)
}
i := 0
readItems := []string{}
savedItems := []string{}
tagItems := []map[string]string{}
// Step 1 don't trust the data, sanitize it with BlueMonday
//p := bluemonday.UGCPolicy()
p := bluemonday.NewPolicy()
p.AllowStandardURLs()
p.AllowAttrs("href").Matching(regexp.MustCompile(`(?i)mailto|http|https|gopher|ftp?`)).OnElements("a")
p.AllowElements("p")
p.AllowElements("b")
p.AllowElements("i")
p.AllowElements("strong")
p.AllowElements("em")
p.AllowElements("ul")
p.AllowElements("ol")
p.AllowElements("li")
p.AllowElements("code")
p.AllowElements("pre")
p.AllowElements("figure")
p.AllowElements("img")
p.AllowElements("blockquote")
//p.AllowElements("div")
// Step 2 get data and then sanitize it.
for rows.Next() {
var (
link string
title string
description string
updated string
published string
label string
tags string
)
i++
if err := rows.Scan(&link, &title, &description, &updated, &published, &label, &tags); err != nil {
fmt.Fprintf(app.eout, "%s\n", err)
continue
}
// Now sanitize each element
title = html.UnescapeString(p.Sanitize(title))
description = html.UnescapeString(p.Sanitize(description))
if err := app.DisplayItem(link, title, description, updated, published, label, tags); err != nil {
return err
}
// Wait for some input
quit := false
prompt := true
for prompt {
buf := bufio.NewReader(app.in)
fmt.Fprintf(app.out, promptStr, i)
src, err := buf.ReadBytes('\n')
if err != nil {
fmt.Fprintf(app.eout, "do not understand %q?\n", src)
} else {
prompt = false
}
answer := strings.ToLower(strings.Trim(string(src), " \t\r\n"))
switch answer {
case "":
prompt = false
ClearScreen()
case "o":
OpenInBrowser(app.in, app.out, app.eout, link)
prompt = true
case "n":
readItems = append(readItems, link)
prompt = false
ClearScreen()
case "s":
savedItems = append(savedItems, link)
prompt = false
ClearScreen()
case "t":
fmt.Fprintf(app.out, "Enter tags (separated by commas) > ")
tagBuf := bufio.NewReader(app.in)
src, err = tagBuf.ReadBytes('\n')
if err != nil {
fmt.Fprintf(app.eout, "failed to read tags, %s\n", err)
} else {
tag := string(src)
if tag != "" {
tagItems = append(tagItems, map[string]string{
"link": link,
"tag": tag,
})
}
// Tagged items should also be auto-saved.
savedItems = append(savedItems, link)
}
prompt = true
case "q":
prompt = false
quit = true
case "stats":
prompt = true
if err := displayStats(app.out, db, app.DBName); err != nil {
fmt.Fprintf(app.eout, "%s\n", err)
}
default:
fmt.Fprintf(app.eout, "do not understand %q?\n", answer)
prompt = true
}
}
if quit {
break
}
}
if err := rows.Err(); err != nil {
return err
}
rows.Close()
if len(savedItems) > 0 {
fmt.Fprintf(app.out, "saving %d items ...\n", len(savedItems))
for _, link := range savedItems {
if err := app.MarkItem(db, link, "saved"); err != nil {
return err
}
}
}
if len(readItems) > 0 {
fmt.Fprintf(app.out, "marking %d items read ...\n", len(readItems))
for _, link := range readItems {
if err := app.MarkItem(db, link, "read"); err != nil {
return err
}
}
}
if len(tagItems) > 0 {
fmt.Fprintf(app.out, "tagging %d items ...\n", len(tagItems))
for _, obj := range tagItems {
if link, hasLink := obj["link"]; hasLink {
if tag, hasTag := obj["tag"]; hasTag && tag != "" {
if strings.Contains(tag, ",") {
tags := strings.Split(tag, ",")
for i, s := range tags {
tags[i] = strings.TrimSpace(s)
}
src, err := JSONMarshal(tags)
if err != nil {
return err
}
tag = string(src)
} else {
tag = fmt.Sprintf("[%q]", tag)
}
if err := app.TagItem(db, link, tag); err != nil {
return err
}
}
}
}
}
return nil
}
// Run provides the runner for skimmer. It allows for testing of much of the cli functionality
func (app *Skimmer) Run(in io.Reader, out io.Writer, eout io.Writer, args []string) error {
app.in = in
app.out = out
app.eout = eout
if len(args) == 0 {
return fmt.Errorf("expected a .skim, an OPML or urls file to process")
}
fPath := args[0]
xName := path.Ext(fPath)
// See if we need to setup things.
if err := app.Setup(fPath); err != nil {
return err
}
var (
db *sql.DB
err error
)
datestamp := ""
if len(args) > 1 {
datestamp = args[1]
}
dsn := app.DBName
db, err = sql.Open("sqlite", dsn)
if err != nil {
return err
}
defer db.Close()
// See if we need to update the channels
if xName != ".skim" {
if err := app.ReadUrls(fPath); err != nil {
return err
}
if err := app.ResetChannels(db); err != nil {
return err
}
// By Downloading the urls all the channel data will get created/updated.
if err := app.Download(db); err != nil {
return err
}
cnt, err := app.ItemCount(db)
if err != nil {
fmt.Fprintf(app.eout, "fail to count items, %s\n", err)
}
fmt.Fprintf(app.out, "\n%d items available to read\n", cnt)
return nil
}
if app.Prune {
var err error
if len(args) < 2 {
return fmt.Errorf("expected a date or timestamp for pruning")
}
datestampFmt, err := normalizeTFormat(datestamp)
if err != nil {
return err
}
dt, err := time.Parse(datestampFmt, datestamp)
if err != nil {
return err
}
if err := app.PruneItems(db, dt); err != nil {
return err
}
cnt, err := app.ItemCount(db)
if err != nil {
fmt.Fprintf(app.eout, "fail to count items, %s\n", err)
}
fmt.Fprintf(app.out, "\n%d items available to read\n", cnt)
return nil
}
if app.AsURLs {
src, err := app.ChannelsToUrls(db)
if err != nil {
return err
}
fmt.Fprintf(app.out, "%s\n", src)
return nil
}
if app.Interactive {
return app.RunInteractive(db)
}
if err := app.Write(db); err != nil {
return err
}
return nil
}