set Vore user agent, include sub stats

j3s.sh 8ffdaf6e 9815e967

verified
Changed files
+59 -2
files
reaper
rss
sqlite
+1
files/settings.tmpl.html
··· 33 33 https://100r.co/links/rss.xml 34 34 https://begriffs.com/atom.xml 35 35 https://blog.stillgreenmoss.net/feed/ 36 + https://capsul.bearblog.dev/rss.xml 36 37 https://facklambda.dev/atom.xml 37 38 https://herman.bearblog.dev/feed/ 38 39 https://j3s.sh/feed.atom
+32 -1
reaper/reaper.go
··· 1 1 package reaper 2 2 3 3 import ( 4 + "context" 4 5 "errors" 6 + "fmt" 5 7 "log" 8 + "net/http" 6 9 "sort" 7 10 "sync" 8 11 "time" ··· 19 22 db *sqlite.DB 20 23 } 21 24 25 + func (r *Reaper) fetchFunc() rss.FetchFunc { 26 + reaperFetchFunc := func(url string) (resp *http.Response, err error) { 27 + client := http.Client{ 28 + Timeout: 20 * time.Second, 29 + } 30 + 31 + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil) 32 + if err != nil { 33 + return nil, err 34 + } 35 + 36 + req.Header.Set("User-Agent", "Vore") 37 + 38 + fid, exists := r.db.GetFeedIDAndExists(url) 39 + if exists { 40 + subs := r.db.GetSubscriberCount(url) 41 + ua := fmt.Sprintf("Vore feed-id:%d - %d subscribers", fid, subs) 42 + req.Header.Set("User-Agent", ua) 43 + } 44 + 45 + fmt.Println(req.Header.Get("User-Agent")) 46 + 47 + return client.Do(req) 48 + } 49 + return reaperFetchFunc 50 + } 51 + 22 52 func New(db *sqlite.DB) *Reaper { 23 53 r := &Reaper{ 24 54 feeds: make(map[string]*rss.Feed), ··· 92 122 // refreshFeed triggers a fetch on the given feed, 93 123 // and sets a fetch error in the db if there is one. 94 124 func (r *Reaper) refreshFeed(f *rss.Feed) { 125 + f.FetchFunc = r.fetchFunc() 95 126 err := f.Update() 96 127 if err != nil { 97 128 r.handleFeedFetchFailure(f.UpdateURL, err) ··· 183 214 // Fetch attempts to fetch a feed from a given url, marshal 184 215 // it into a feed object, and manage it via reaper. 185 216 func (r *Reaper) Fetch(url string) error { 186 - feed, err := rss.Fetch(url) 217 + feed, err := rss.FetchByFunc(r.fetchFunc(), url) 187 218 if err != nil { 188 219 return err 189 220 }
-1
rss/rss.go
··· 39 39 // DefaultFetchFunc uses http.DefaultClient to fetch a feed. 40 40 var DefaultFetchFunc = func(url string) (resp *http.Response, err error) { 41 41 client := http.DefaultClient 42 - client.Timeout = 20 * time.Second 43 42 return client.Get(url) 44 43 } 45 44
+26
sqlite/sqlite.go
··· 298 298 } 299 299 return "", nil 300 300 } 301 + 302 + func (db *DB) GetSubscriberCount(feedURL string) int { 303 + var count int 304 + err := db.sql.QueryRow(` 305 + SELECT COUNT(s.user_id) 306 + FROM subscribe s 307 + JOIN feed f ON s.feed_id = f.id 308 + WHERE f.url = ? 309 + `, feedURL).Scan(&count) 310 + if err != nil { 311 + log.Fatal(err) 312 + } 313 + return count 314 + } 315 + 316 + func (db *DB) GetFeedIDAndExists(feedURL string) (int, bool) { 317 + var fid int 318 + err := db.sql.QueryRow("SELECT id FROM feed WHERE url=?", feedURL).Scan(&fid) 319 + if err == sql.ErrNoRows { 320 + return 0, false 321 + } 322 + if err != nil { 323 + log.Fatal(err) 324 + } 325 + return fid, true 326 + }