set Vore user agent, include sub stats
Jes Olson j3s@c3f.net
Fri, 11 Apr 2025 14:41:56 -0500
4 files changed,
59 insertions(+),
2 deletions(-)
M
files/settings.tmpl.html
→
files/settings.tmpl.html
@@ -33,6 +33,7 @@
https://100r.co/links/rss.xml https://begriffs.com/atom.xml https://blog.stillgreenmoss.net/feed/ +https://capsul.bearblog.dev/rss.xml https://facklambda.dev/atom.xml https://herman.bearblog.dev/feed/ https://j3s.sh/feed.atom
M
reaper/reaper.go
→
reaper/reaper.go
@@ -1,8 +1,11 @@
package reaper import ( + "context" "errors" + "fmt" "log" + "net/http" "sort" "sync" "time"@@ -19,6 +22,33 @@
db *sqlite.DB } +func (r *Reaper) fetchFunc() rss.FetchFunc { + reaperFetchFunc := func(url string) (resp *http.Response, err error) { + client := http.Client{ + Timeout: 20 * time.Second, + } + + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + req.Header.Set("User-Agent", "Vore") + + fid, exists := r.db.GetFeedIDAndExists(url) + if exists { + subs := r.db.GetSubscriberCount(url) + ua := fmt.Sprintf("Vore feed-id:%d - %d subscribers", fid, subs) + req.Header.Set("User-Agent", ua) + } + + fmt.Println(req.Header.Get("User-Agent")) + + return client.Do(req) + } + return reaperFetchFunc +} + func New(db *sqlite.DB) *Reaper { r := &Reaper{ feeds: make(map[string]*rss.Feed),@@ -92,6 +122,7 @@
// refreshFeed 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) { + f.FetchFunc = r.fetchFunc() err := f.Update() if err != nil { r.handleFeedFetchFailure(f.UpdateURL, err)@@ -183,7 +214,7 @@
// Fetch attempts to fetch a feed from a given url, marshal // it into a feed object, and manage it via reaper. func (r *Reaper) Fetch(url string) error { - feed, err := rss.Fetch(url) + feed, err := rss.FetchByFunc(r.fetchFunc(), url) if err != nil { return err }
M
rss/rss.go
→
rss/rss.go
@@ -39,7 +39,6 @@
// DefaultFetchFunc uses http.DefaultClient to fetch a feed. var DefaultFetchFunc = func(url string) (resp *http.Response, err error) { client := http.DefaultClient - client.Timeout = 20 * time.Second return client.Get(url) }
M
sqlite/sqlite.go
→
sqlite/sqlite.go
@@ -298,3 +298,29 @@ return result.String, nil
} return "", nil } + +func (db *DB) GetSubscriberCount(feedURL string) int { + var count int + err := db.sql.QueryRow(` + SELECT COUNT(s.user_id) + FROM subscribe s + JOIN feed f ON s.feed_id = f.id + WHERE f.url = ? + `, feedURL).Scan(&count) + if err != nil { + log.Fatal(err) + } + return count +} + +func (db *DB) GetFeedIDAndExists(feedURL string) (int, bool) { + var fid int + err := db.sql.QueryRow("SELECT id FROM feed WHERE url=?", feedURL).Scan(&fid) + if err == sql.ErrNoRows { + return 0, false + } + if err != nil { + log.Fatal(err) + } + return fid, true +}