repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
26a8a1f
parent
34b19ea
author
Eric Bower
date
2022-12-21 22:29:16 +0000 UTC
fix(feeds): added logging statements and some different fetch logic
1 files changed,  +37, -6
M feeds/cron.go
+37, -6
  1@@ -3,6 +3,7 @@ package feeds
  2 import (
  3 	"errors"
  4 	"fmt"
  5+	"net/http"
  6 	"strings"
  7 	"text/template"
  8 	"time"
  9@@ -16,6 +17,15 @@ import (
 10 
 11 var ErrNoRecentArticles = errors.New("no recent articles")
 12 
 13+type UserAgentTransport struct {
 14+	http.RoundTripper
 15+}
 16+
 17+func (c *UserAgentTransport) RoundTrip(r *http.Request) (*http.Response, error) {
 18+	r.Header.Set("User-Agent", "linux:feeds:v1")
 19+	return c.RoundTripper.RoundTrip(r)
 20+}
 21+
 22 type FeedItem struct {
 23 	Title       string
 24 	Link        string
 25@@ -81,6 +91,7 @@ func (f *Fetcher) RunPost(user *db.User, post *db.Post) error {
 26 
 27 	parsed := shared.ListParseText(post.Text, shared.NewNullLinkify())
 28 
 29+	f.cfg.Logger.Infof("Last digest at (%s)", post.Data.LastDigest)
 30 	err := f.Validate(post.Data.LastDigest, parsed)
 31 	if err != nil {
 32 		f.cfg.Logger.Info(err.Error())
 33@@ -126,7 +137,7 @@ func (f *Fetcher) RunUser(user *db.User) error {
 34 	}
 35 
 36 	if len(posts.Data) > 0 {
 37-		f.cfg.Logger.Infof("(%s) found (%d) feed posts", user.Name, posts.Total)
 38+		f.cfg.Logger.Infof("(%s) found (%d) feed posts", user.Name, len(posts.Data))
 39 	}
 40 
 41 	for _, post := range posts.Data {
 42@@ -140,6 +151,8 @@ func (f *Fetcher) RunUser(user *db.User) error {
 43 }
 44 
 45 func (f *Fetcher) Fetch(fp *gofeed.Parser, url string, lastDigest *time.Time) (*Feed, error) {
 46+	f.cfg.Logger.Infof("(%s) fetching feed", url)
 47+
 48 	feed, err := fp.ParseURL(url)
 49 	if err != nil {
 50 		return nil, err
 51@@ -192,6 +205,9 @@ func (f *Fetcher) Print(feedTmpl *DigestFeed) (string, error) {
 52 
 53 func (f *Fetcher) FetchAll(urls []string, lastDigest *time.Time) (string, error) {
 54 	fp := gofeed.NewParser()
 55+	fp.Client = &http.Client{
 56+		Transport: &UserAgentTransport{http.DefaultTransport},
 57+	}
 58 	feeds := &DigestFeed{}
 59 
 60 	for _, url := range urls {
 61@@ -207,6 +223,10 @@ func (f *Fetcher) FetchAll(urls []string, lastDigest *time.Time) (string, error)
 62 		feeds.Feeds = append(feeds.Feeds, feedTmpl)
 63 	}
 64 
 65+	if len(feeds.Feeds) == 0 {
 66+		return "", fmt.Errorf("%w, skipping", ErrNoRecentArticles)
 67+	}
 68+
 69 	str, err := f.Print(feeds)
 70 	if err != nil {
 71 		return "", nil
 72@@ -227,6 +247,8 @@ func (f *Fetcher) SendEmail(username, email, msg string) error {
 73 	plainTextContent := msg
 74 	htmlContent := msg
 75 
 76+	f.cfg.Logger.Infof("message body (%s)", plainTextContent)
 77+
 78 	message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
 79 	client := sendgrid.NewSendClient(f.cfg.SendgridKey)
 80 
 81@@ -236,11 +258,20 @@ func (f *Fetcher) SendEmail(username, email, msg string) error {
 82 		return err
 83 	}
 84 
 85-	f.cfg.Logger.Infof(
 86-		"(%s) successfully sent email digest (x-message-id: %s)",
 87-		email,
 88-		response.Headers["X-Message-Id"][0],
 89-	)
 90+	f.cfg.Logger.Infof("(%s) email digest response: %v", username, response)
 91+
 92+	if len(response.Headers["X-Message-Id"]) > 0 {
 93+		f.cfg.Logger.Infof(
 94+			"(%s) successfully sent email digest (x-message-id: %s)",
 95+			email,
 96+			response.Headers["X-Message-Id"][0],
 97+		)
 98+	} else {
 99+		f.cfg.Logger.Errorf(
100+			"(%s) could not find x-message-id, which means sending an email failed",
101+			email,
102+		)
103+	}
104 
105 	return nil
106 }