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

import (
	"fmt"

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

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

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

	// 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

	parsedFeed, err := fp.Parse(resp.Body)
	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, Item: feedItem, itemId: uuid.New(), feed: feed}
	}
	return nil
}

func handleFeed(feed *Feed) {
	log.Printf("Fetching %s from %s", feed.Name, feed.Url)

	err := feed.parse()
	if err != nil {
		if feed.cached.Failures() >= feed.Global.MaxFailures {
			log.Error(err)
		} else {
			log.Print(err)
		}
	}
}
20 +0200'>2014-05-08inplace: mutually exclusive with forceJason A. Donenfeld2-3/+3 2014-05-08usage: tab to spacesJason A. Donenfeld1-1/+1 2014-05-08generate: use nice ansi colors instead.Jason A. Donenfeld1-3/+2 Revert "Mute git-commit messages to make pass insert readable" This reverts commit f30ce6374d554e704162d5fa8e49acd9c6fd0ecc. I decided I like the git output. Instead highlight generated passwords using nice terminal output instead. 2014-05-08zsh: posix compatible sed fix for zsh-completionJason A. Donenfeld1-1/+1 This reverts commit 56381287a16792b4c6410f07db68e02f3574c213, and further fixes things. 2014-05-07Implement interactive init functionSvend Sorensen1-0/+8 2014-05-07Implement interactive rename functionSvend Sorensen1-0/+7 2014-05-07Reorder interactive function to match order of helper functionsSvend Sorensen1-13/+13 2014-05-07Make edit helper function name consistent with other helpersSvend Sorensen1-1/+1 2014-05-07Factor out password completing-read functionSvend Sorensen1-4/+8 2014-05-07Add dash to Package-RequiresSvend Sorensen1-1/+1 2014-05-06Force sane sort order.Jason A. Donenfeld1-2/+2 2014-05-06generate: add --in-place optionJason A. Donenfeld4-9/+33