aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/parse.go
blob: a2812bd8f85bf4ae866b764a8be4aa79961673c3 (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
package feed

import (
	"fmt"
	"io"
	"os/exec"

	"github.com/google/uuid"
	"github.com/mmcdole/gofeed"

	"github.com/Necoro/feed2imap-go/internal/http"
)

func (feed *Feed) Parse() error {
	fp := gofeed.NewParser()

	var reader io.Reader
	var cleanup func() error

	if feed.Url != "" {
		// we do not use the http support in gofeed, so that we can control the behavior of http requests
		// and ensure it to be the same in all places
		resp, cancel, err := http.Get(feed.Url, feed.Global.Timeout, feed.NoTLS)
		if err != nil {
			return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err)
		}
		defer cancel() // includes resp.Body.Close

		reader = resp.Body
		cleanup = func() error { return nil }
	} else { // exec
		// we use the same context as for HTTP
		ctx, cancel := http.Context(feed.Global.Timeout)
		cmd := exec.CommandContext(ctx, feed.Exec[0], feed.Exec[1:]...)
		defer func() {
			cancel()
			// cmd.Wait might have already been called -- but call it again to be sure
			_ = cmd.Wait()
		}()

		stdout, err := cmd.StdoutPipe()
		if err != nil {
			return fmt.Errorf("preparing exec for feed '%s': %w", feed.Name, err)
		}

		if err = cmd.Start(); err != nil {
			return fmt.Errorf("starting exec for feed '%s: %w", feed.Name, err)
		}

		reader = stdout
		cleanup = cmd.Wait
	}

	parsedFeed, err := fp.Parse(reader)
	if err != nil {
		return fmt.Errorf("parsing feed '%s': %w", feed.Name, err)
	}

	feed.feed = parsedFeed
	feed.items = make([]Item, len(parsedFeed.Items))
	for idx, feedItem := range parsedFeed.Items {
		feed.items[idx] = Item{Feed: parsedFeed, feed: feed, Item: feedItem, ID: uuid.New()}
	}
	return cleanup()
}