// 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/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 } // 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) }}) } } 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 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 }