aboutsummaryrefslogtreecommitdiff
path: root/cmds/polite.go
blob: 2701436f7aa2f937a06fa84488ea464e167644cd (plain)
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
// Polite command: one-off sweep making every feed a polite fetcher.
//
// Sends PUT /v1/feeds/{id} with the two-field body {"crawler":false,
// "ignore_http_cache":false} per subscription: no original-article scraping
// and HTTP cache (conditional requests) honored. That is the strategy
// against origins blocking the polling — per poll cycle the origin sees only
// the (cheap, mostly 304) feed fetch, not a page request per new entry.
// Only those two fields are ever sent, so no other feed setting can move
// (Miniflux's update is pointer-based: omitted fields stay untouched).
// Fail-fast + idempotent: an interrupted run is resumed by simply rerunning.
//
// Side effect worth knowing: with the scraper off, future entries carry the
// feed-provided content (often an excerpt) instead of the scraped article —
// serve's RSS bodies will follow suit. Title-only scoring is unaffected.
package cmds

import (
	"flag"
	"fmt"
	"io"

	"fluxrec/miniflux"
)

type PoliteCommand struct {
	DryRun bool
}

func (c *PoliteCommand) Name() string { return "polite" }

func (c *PoliteCommand) Init(args []string) error {
	fs := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
	fs.Usage = func() {
		fmt.Fprint(fs.Output(), `Usage: fluxrec polite [--dry-run]

Make every feed a polite fetcher: scraper off, HTTP cache honored. Each feed
is updated with the two-field body {"crawler":false,"ignore_http_cache":false}
— nothing else on the feed changes. Stops at the first feed that errors
(naming it and the remainder); rerun at will, the op is idempotent.
--dry-run lists the feeds without sending anything.
`)
		fs.PrintDefaults()
	}
	fs.BoolVar(&c.DryRun, "dry-run", false, "List the feeds and exit (no PUT sent)")
	if err := fs.Parse(args); err != nil {
		return err
	}
	return nil
}

func (c *PoliteCommand) Run(stdin io.Reader, stdout io.Writer) error {
	cfg, err := miniflux.ConfigFromEnv()
	if err != nil {
		return err
	}
	client := miniflux.NewClient(cfg)

	feeds, err := client.Feeds()
	if err != nil {
		return err
	}

	// API order, one update at a time: a subscription list is small, and
	// serialized requests keep any failure's story simple.
	done := 0
	for _, f := range feeds {
		if c.DryRun {
			fmt.Fprintf(stdout, "would  %-6d %s\n", f.ID, clip(f.Title, 64))
			continue
		}
		if err := client.MakeFeedPolite(f.ID); err != nil {
			return fmt.Errorf("updating feed %d (%s): %w; rerun polite to finish the remaining %d (idempotent)",
				f.ID, f.Title, err, len(feeds)-done)
		}
		done++
		fmt.Fprintf(stdout, "ok     %-6d %s\n", f.ID, clip(f.Title, 64))
	}

	if c.DryRun {
		fmt.Fprintf(stdout, "dry-run: would set crawler=false and ignore_http_cache=false on all %d feeds; nothing was sent\n", len(feeds))
		return nil
	}
	fmt.Fprintf(stdout, "polite sweep: scraper off + HTTP cache honored on %d of %d feeds\n", done, len(feeds))
	return nil
}