Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
2.2 KiB
Go
107 lines
2.2 KiB
Go
package fetcher
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
|
|
"supermarket/internal/api/twitter"
|
|
"supermarket/internal/api/youtube"
|
|
"supermarket/internal/models"
|
|
)
|
|
|
|
type Fetcher struct {
|
|
twitter *twitter.Client
|
|
youtube *youtube.Client
|
|
}
|
|
|
|
type Result struct {
|
|
Items []models.ContentItem
|
|
Errors []string
|
|
}
|
|
|
|
func New(tw *twitter.Client, yt *youtube.Client) *Fetcher {
|
|
return &Fetcher{twitter: tw, youtube: yt}
|
|
}
|
|
|
|
// FetchForApp concurrently fetches content from all configured sources.
|
|
func (f *Fetcher) FetchForApp(ctx context.Context, app *models.Application, since time.Time) Result {
|
|
type batch struct {
|
|
items []models.ContentItem
|
|
err error
|
|
label string
|
|
}
|
|
|
|
jobs := make(chan func() batch, 8)
|
|
results := make(chan batch, 8)
|
|
|
|
// Worker pool
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 4; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for job := range jobs {
|
|
results <- job()
|
|
}
|
|
}()
|
|
}
|
|
|
|
// Enqueue fetches
|
|
enqueue := func(label string, fn func() ([]models.ContentItem, error)) {
|
|
jobs <- func() batch {
|
|
items, err := fn()
|
|
return batch{items: items, err: err, label: label}
|
|
}
|
|
}
|
|
|
|
if f.twitter != nil && app.TwitterHandle != "" {
|
|
enqueue("X (account @"+app.TwitterHandle+")", func() ([]models.ContentItem, error) {
|
|
return f.twitter.GetUserTweets(ctx, app.TwitterHandle, since, app.Name)
|
|
})
|
|
}
|
|
|
|
if f.youtube != nil && app.YouTubeChannelID != "" {
|
|
enqueue("YouTube (channel)", func() ([]models.ContentItem, error) {
|
|
return f.youtube.GetChannelVideos(ctx, app.YouTubeChannelID, since, app.Name)
|
|
})
|
|
}
|
|
|
|
close(jobs)
|
|
go func() {
|
|
wg.Wait()
|
|
close(results)
|
|
}()
|
|
|
|
var result Result
|
|
for b := range results {
|
|
if b.err != nil {
|
|
result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", b.label, b.err))
|
|
} else {
|
|
result.Items = append(result.Items, b.items...)
|
|
}
|
|
}
|
|
|
|
// Deduplicate by ID and sort newest first
|
|
result.Items = dedup(result.Items)
|
|
sort.Slice(result.Items, func(i, j int) bool {
|
|
return result.Items[i].PublishedAt.After(result.Items[j].PublishedAt)
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
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
|
|
}
|