-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
fetch.go
56 lines (53 loc) · 1.82 KB
/
fetch.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
package fetch
import (
"fmt"
"strings"
"github.com/slurdge/goeland/config"
"github.com/slurdge/goeland/internal/goeland"
"github.com/slurdge/goeland/internal/goeland/filters"
"github.com/slurdge/goeland/log"
)
// FetchSource retrieves a source from either a feed, imgur or other sub-sources
func FetchSource(config config.Provider, sourceName string) (*goeland.Source, error) {
if !config.IsSet(fmt.Sprintf("sources.%s", sourceName)) {
return nil, fmt.Errorf("cannot find source: %s", sourceName)
}
sourceType := config.GetString(fmt.Sprintf("sources.%s.type", sourceName))
log.Infof("Fetching source: %s of type %s", sourceName, sourceType)
source := new(goeland.Source)
var err error
switch sourceType {
case "feed":
url := config.GetString(fmt.Sprintf("sources.%s.url", sourceName))
err = fetchFeed(source, url, !strings.HasPrefix(url, "http"))
if err != nil {
log.Errorf("Cannot retrieve feed: %s error: %v", url, err)
return source, err
}
case "imgur":
tag := config.GetString(fmt.Sprintf("sources.%s.tag", sourceName))
err = fetchImgurTag(source, tag)
if err != nil {
log.Errorf("Cannot retrieve imgur tag: %s error: %v", tag, err)
return source, err
}
case "merge":
subSourceNames := config.GetStringSlice(fmt.Sprintf("sources.%s.sources", sourceName))
for _, subSourceName := range subSourceNames {
subSource, err := FetchSource(config, subSourceName)
source.Entries = append(source.Entries, subSource.Entries...)
if err != nil {
log.Errorf("cannot fetch source: %s (%v)", subSourceName, err)
continue
}
}
default:
return nil, fmt.Errorf("cannot understand source type: %s", sourceType)
}
if source != nil {
source.Name = sourceName
}
filters.FilterSource(source, config)
log.Debugf("%v", source)
return source, nil
}