Files
supermarket/internal/fetcher/fetcher.go
T
davidandClaude Opus 5.5 cd24f2a06d Phase 1, part 4: promote the company's own products
- Content lists the company first, at /content/own: its products from
  the profile (always there to pick from), its own feeds (a new profile
  field for the blog, news and releases), and new or updated pages from
  its websites' sitemaps, with each page's title and description.
- Drafts from it are kind "product" (utm_campaign=product), filed under
  the company's name, and prompted to promote the company's own work
  without unsupported claims; on Reddit, in the makers' voice, with a
  warning that self-promotion is often restricted.

This completes Phase 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 14:42:37 -07:00

177 lines
4.6 KiB
Go

// Package fetcher gathers recent content about an inventory entry from every
// source it lists and that has credentials: its X account, X posts with its
// hashtags, its YouTube channel and its RSS feeds.
package fetcher
import (
"context"
"fmt"
"sort"
"sync"
"time"
"supermarket/internal/api/rss"
"supermarket/internal/api/sitemap"
"supermarket/internal/api/twitter"
"supermarket/internal/api/youtube"
"supermarket/internal/inventory"
"supermarket/internal/models"
)
// Fetcher holds the source clients; a nil client skips that source.
type Fetcher struct {
Twitter *twitter.Client
YouTube *youtube.Client
RSS *rss.Client
// Sitemap, if set, also reads new pages from the entry's websites'
// sitemaps. It's set for the company's own content only: a partner's
// every page change isn't news.
Sitemap *sitemap.Client
}
// Result is what a fetch found, and what failed or was skipped.
type Result struct {
Items []models.ContentItem
Errors []string
Skipped []string // sources the entry lists but that have no credentials
}
// Fetch gets the entry's content published after since, from all its sources
// at once. One source failing doesn't stop the others.
func (f *Fetcher) Fetch(ctx context.Context, e *inventory.Entry, since time.Time) Result {
type job struct {
label string
run func() ([]models.ContentItem, error)
}
var jobs []job
var res Result
if e.TwitterHandle != "" {
if f.Twitter != nil {
jobs = append(jobs, job{"X @" + e.TwitterHandle, func() ([]models.ContentItem, error) {
return f.Twitter.GetUserTweets(ctx, e.TwitterHandle, since, e.Name)
}})
} else {
res.Skipped = append(res.Skipped, "X @"+e.TwitterHandle+" (no X bearer token)")
}
}
if len(e.Hashtags) > 0 {
if f.Twitter != nil {
q := twitter.BuildSearchQuery(e.Hashtags, e.TwitterHandle)
jobs = append(jobs, job{"X search", func() ([]models.ContentItem, error) {
return f.Twitter.SearchTweets(ctx, q, since, e.Name)
}})
} else {
res.Skipped = append(res.Skipped, "X hashtag search (no X bearer token)")
}
}
if e.YouTubeChannelID != "" {
if f.YouTube != nil {
jobs = append(jobs, job{"YouTube", func() ([]models.ContentItem, error) {
return f.YouTube.GetChannelVideos(ctx, e.YouTubeChannelID, since, e.Name)
}})
} else {
res.Skipped = append(res.Skipped, "YouTube (no YouTube API key)")
}
}
if f.RSS != nil {
for _, feed := range e.RSS {
feed := feed
jobs = append(jobs, job{"RSS " + feed, func() ([]models.ContentItem, error) {
return f.RSS.Fetch(ctx, feed, since, e.Name)
}})
}
}
if f.Sitemap != nil {
for _, site := range e.Websites {
site := site
jobs = append(jobs, job{"sitemap " + site, func() ([]models.ContentItem, error) {
pages, err := f.Sitemap.NewPages(ctx, site, since)
if err != nil {
return nil, err
}
var items []models.ContentItem
for _, pg := range pages {
it, err := f.Sitemap.Item(ctx, pg, e.Name)
if err != nil {
continue // one unreadable page doesn't spoil the rest
}
items = append(items, it)
}
return items, nil
}})
}
}
type batch struct {
label string
items []models.ContentItem
err error
}
results := make(chan batch, len(jobs))
sem := make(chan struct{}, 4)
var wg sync.WaitGroup
for _, j := range jobs {
wg.Add(1)
go func(j job) {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
items, err := j.run()
results <- batch{j.label, items, err}
}(j)
}
wg.Wait()
close(results)
for b := range results {
if b.err != nil {
res.Errors = append(res.Errors, fmt.Sprintf("%s: %v", b.label, b.err))
continue
}
res.Items = append(res.Items, b.items...)
}
sort.Strings(res.Errors)
res.Items = dedup(res.Items)
sort.Slice(res.Items, func(i, j int) bool {
return res.Items[i].PublishedAt.After(res.Items[j].PublishedAt)
})
return res
}
// Sources says what Fetch would read for e, for the page.
func Sources(e *inventory.Entry) []string {
var out []string
if e.TwitterHandle != "" {
out = append(out, "X @"+e.TwitterHandle)
}
if len(e.Hashtags) > 0 {
out = append(out, "X hashtags")
}
if e.YouTubeChannelID != "" {
out = append(out, "YouTube")
}
if e.Relationship == "product" && len(e.Websites) > 0 {
out = append(out, "new pages on your websites")
}
if n := len(e.RSS); n == 1 {
out = append(out, "1 RSS feed")
} else if n > 1 {
out = append(out, fmt.Sprintf("%d RSS feeds", n))
}
return out
}
func dedup(items []models.ContentItem) []models.ContentItem {
seen := map[string]bool{}
out := items[:0]
for _, item := range items {
if !seen[item.ID] {
seen[item.ID] = true
out = append(out, item)
}
}
return out
}