-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
127 lines (113 loc) · 2.49 KB
/
parser.go
File metadata and controls
127 lines (113 loc) · 2.49 KB
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
package feedly
import (
"context"
"fmt"
"runtime"
"sync/atomic"
)
type OnErrorStrategy int
// Error strategy
const (
IGNORE = iota // ignore error
ABORT // Cancel all other requests
)
type FeedRes struct {
Url string
Rss *Rss
Err error
Code uint32
}
type feedArgs struct {
ctx context.Context
client *Client
outChan chan FeedRes
strategy OnErrorStrategy
}
// ParseFromUrl parses the RSS feed and returns a list of items.
func ParseFromUrl(url string) (rss *Rss, err error) {
c := NewClient()
code, res, reserr := c.FetchFeed(url)
if reserr.error != nil {
return nil, reserr
}
if code != 200 {
return nil, err
}
rss, unmarshallerr := NewRSS(res)
if unmarshallerr.error != nil {
return nil, unmarshallerr
}
return rss, nil
}
func parseFeedChunk(args *feedArgs, urlChunk []string) {
for _, url := range urlChunk {
fmt.Println("Parsing url: ", url)
select {
case <-args.ctx.Done():
return
default:
code, rssp, err := args.client.FetchFeed(url)
if err.error != nil {
if args.strategy == ABORT {
return
} else {
args.outChan <- FeedRes{Url: url, Rss: nil, Err: err, Code: code}
continue
}
}
if code != 200 {
fmt.Println("Error: ", err)
if args.strategy == ABORT {
return
} else {
args.outChan <- FeedRes{Url: url, Rss: nil, Err: err, Code: code}
continue
}
}
rss, rsserr := NewRSS(rssp)
if rsserr.error != nil {
if args.strategy == ABORT {
return
} else {
args.outChan <- FeedRes{Url: url, Rss: nil, Err: rsserr, Code: code}
continue
}
}
args.outChan <- FeedRes{Url: url, Rss: rss, Err: nil, Code: code}
}
}
}
func ParseMultipleFeeds(urls []string, strategy OnErrorStrategy) (rss []FeedRes, err error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
args := feedArgs{
ctx: ctx,
client: NewClient(),
outChan: make(chan FeedRes),
strategy: strategy,
}
var feeds = make([]FeedRes, 0)
var nextChunk = 0
for i := 0; i < len(urls); i += runtime.NumCPU() {
if i+runtime.NumCPU() > len(urls) {
nextChunk = len(urls) - 1
} else {
nextChunk = i + runtime.NumCPU()
}
go parseFeedChunk(&args, urls[i:nextChunk])
}
var count atomic.Uint32
for res := range args.outChan {
count.Add(1)
if res.Err != nil && strategy == ABORT {
cancel()
close(args.outChan)
return nil, res.Err
}
feeds = append(feeds, res)
if count.Load() == uint32(len(urls)) {
close(args.outChan)
}
}
return feeds, nil
}