reaper/reaper.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package reaper import ( "fmt" "io" "net/http" "sort" "strings" "sync" "time" "git.j3s.sh/vore/rss" "git.j3s.sh/vore/sqlite" ) type Reaper struct { // internal list of all rss feeds where the map // key represents the primary id of the key in the db feeds []rss.Feed // this mutex is used for locking writes to Feeds mu sync.Mutex db *sqlite.DB } func Summon(db *sqlite.DB) *Reaper { var feeds []rss.Feed reaper := Reaper{ feeds: feeds, mu: sync.Mutex{}, db: db, } return &reaper } func (r *Reaper) Start() { // Make initial url list urls := r.db.GetAllFeedURLs() for _, url := range urls { // Setting UpdateURL lets us defer fetching feed := rss.Feed{ UpdateURL: url, } r.feeds = append(r.feeds, feed) } for { r.refreshAll() time.Sleep(15 * time.Minute) } } // Add the given rss feed to Reaper for maintenance. // If the given feed is already in reaper.Feeds, Add does nothing. func (r *Reaper) addFeed(f rss.Feed) { if !r.HasFeed(f.UpdateURL) { r.mu.Lock() r.feeds = append(r.feeds, f) r.mu.Unlock() } } // UpdateAll fetches every feed & attempts updating them // asynchronously, then prints the duration of the sync func (r *Reaper) refreshAll() { start := time.Now() var wg sync.WaitGroup for i := range r.feeds { if r.staleFeed(&r.feeds[i]) { wg.Add(1) go func(f *rss.Feed) { defer wg.Done() r.refreshFeed(f) }(&r.feeds[i]) } } wg.Wait() fmt.Printf("reaper: refresh complete in %s\n", time.Since(start)) } func (r *Reaper) staleFeed(f *rss.Feed) bool { if f.Refresh.After(time.Now()) { return false } return true } // updateFeed triggers a fetch on the given feed, // and sets a fetch error in the db if there is one. func (r *Reaper) refreshFeed(f *rss.Feed) { err := f.UpdateByFunc(reaperFetchFunc) if err != nil { fmt.Printf("[err] reaper: fetch failure '%s': %s\n", f.UpdateURL, err) r.db.SetFeedFetchError(f.UpdateURL, err.Error()) } } // Have checks whether a given url is represented // in the reaper cache. func (r *Reaper) HasFeed(url string) bool { for i := range r.feeds { if r.feeds[i].UpdateURL == url { return true } } return false } // GetUserFeeds returns a list of feeds func (r *Reaper) GetUserFeeds(username string) []rss.Feed { urls := r.db.GetUserFeedURLs(username) var result []rss.Feed for i := range r.feeds { for _, url := range urls { if r.feeds[i].UpdateURL == url { result = append(result, r.feeds[i]) } } } r.SortFeeds(result) return result } func (r *Reaper) SortFeeds(f []rss.Feed) { sort.Slice(f, func(i, j int) bool { return f[i].UpdateURL < f[j].UpdateURL }) } func (r *Reaper) SortFeedItemsByDate(f []rss.Feed) []rss.Item { var posts []rss.Item for _, f := range f { for _, i := range f.Items { posts = append(posts, *i) } } sort.Slice(posts, func(i, j int) bool { return posts[i].Date.After(posts[j].Date) }) return posts } var reaperFetchFunc = func(url string) (*http.Response, error) { client := http.DefaultClient resp, err := client.Get(url) if err != nil { return nil, err } bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return nil, err } // escape un-parseable HTML elements body := string(bodyBytes) body = strings.ReplaceAll(body, "–", "") // body = strings.ReplaceAll(body, "&&", "–") // body = strings.ReplaceAll(body, "®", "") // fmt.Println(b.String()) // if err != nil { // return nil, err // } // body := html.UnescapeString(string(bodyBytes)) // re-encode as XML // err = xml.EscapeText(&b, []byte(body)) // overwrite the body resp.Body.Close() resp.Body = io.NopCloser(strings.NewReader(body)) return resp, nil } // FetchFeed attempts to fetch a feed from a given url, marshal // it into a feed object, and add it to Reaper. func (r *Reaper) Fetch(url string) error { feed, err := rss.FetchByFunc(reaperFetchFunc, url) if err != nil { return err } r.addFeed(*feed) return nil }