Move to Federated Gitea
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package fetcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"coremarketing/internal/api/twitter"
|
||||
"coremarketing/internal/api/youtube"
|
||||
"coremarketing/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
|
||||
}
|
||||
Reference in New Issue
Block a user