feat(sources): add managed RSS feeds (#41)
This commit is contained in:
@ -36,6 +36,7 @@ type UserAsset struct {
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
FeedURLs []string `json:"feed_urls,omitempty"`
|
||||
SourceKey *string `json:"source_key"`
|
||||
ArticleLimit int `json:"article_limit"`
|
||||
ID string `json:"id"`
|
||||
|
||||
@ -149,7 +149,7 @@ func (r *Repository) ListSources() ([]Source, error) {
|
||||
func (r *Repository) GetSourceByType(sourceType string) (*Source, error) {
|
||||
s := &Source{}
|
||||
err := r.db.QueryRow(`
|
||||
SELECT id, name, type, enabled, created_at, article_limit, source_key FROM sources WHERE type=$1 AND source_key IN ('yahoo_finance','bloomberg','cnbc_business')`, sourceType,
|
||||
SELECT id, name, type, enabled, created_at, article_limit, source_key FROM sources WHERE type=$1 AND source_key IN ('yahoo_finance','bloomberg','cnbc_business','investing_com')`, sourceType,
|
||||
).Scan(&s.ID, &s.Name, &s.Type, &s.Enabled, &s.CreatedAt, &s.ArticleLimit, &s.SourceKey)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
@ -160,22 +160,7 @@ func (r *Repository) GetSourceByType(sourceType string) (*Source, error) {
|
||||
var ErrSourceNotSupported = errors.New("source_not_supported")
|
||||
|
||||
func (r *Repository) UpdateSource(id string, enabled bool, articleLimit *int) error {
|
||||
tx, err := r.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var key *string
|
||||
if err = tx.QueryRow(`SELECT source_key FROM sources WHERE id=$1 FOR UPDATE`, id).Scan(&key); err != nil {
|
||||
return err
|
||||
}
|
||||
if key == nil || !SupportedSourceKey(*key) {
|
||||
return ErrSourceNotSupported
|
||||
}
|
||||
if _, err = tx.Exec(`UPDATE sources SET enabled=$1,article_limit=COALESCE($2,article_limit) WHERE id=$3`, enabled, articleLimit, id); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
return r.UpdateSourceConfig(id, enabled, articleLimit, nil)
|
||||
}
|
||||
|
||||
// ── Scrape Credentials ─────────────────────────────────────────────────────
|
||||
|
||||
70
backend/internal/models/source_feeds.go
Normal file
70
backend/internal/models/source_feeds.go
Normal file
@ -0,0 +1,70 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/tradarr/backend/internal/sourcefeeds"
|
||||
)
|
||||
|
||||
func (r *Repository) SourceFeedURLs(ctx context.Context, key string) ([]string, error) {
|
||||
if !sourcefeeds.Supported(key) {
|
||||
return nil, sourcefeeds.ErrUnsupported
|
||||
}
|
||||
rows, err := r.db.QueryContext(ctx, `SELECT f.url FROM source_feeds f JOIN sources s ON s.id=f.source_id WHERE s.source_key=$1 ORDER BY f.position LIMIT 6`, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
values := []string{}
|
||||
for rows.Next() {
|
||||
var value string
|
||||
if err := rows.Scan(&value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, value)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sourcefeeds.Validate(key, values)
|
||||
}
|
||||
|
||||
// UpdateSourceConfig replaces configuration and switches in one transaction.
|
||||
// Omitted feeds preserve the saved configuration; an empty replacement is invalid.
|
||||
func (r *Repository) UpdateSourceConfig(id string, enabled bool, limit *int, feeds *[]string) error {
|
||||
if limit != nil && (*limit < 1 || *limit > 200) {
|
||||
return sourcefeeds.ErrInvalid
|
||||
}
|
||||
tx, err := r.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var key *string
|
||||
if err := tx.QueryRow(`SELECT source_key FROM sources WHERE id=$1 FOR UPDATE`, id).Scan(&key); err != nil {
|
||||
return err
|
||||
}
|
||||
if key == nil || !SupportedSourceKey(*key) {
|
||||
return ErrSourceNotSupported
|
||||
}
|
||||
var normalized []string
|
||||
if feeds != nil {
|
||||
normalized, err = sourcefeeds.Validate(*key, *feeds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(`UPDATE sources SET enabled=$1,article_limit=COALESCE($2,article_limit) WHERE id=$3`, enabled, limit, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if feeds != nil {
|
||||
if _, err = tx.Exec(`DELETE FROM source_feeds WHERE source_id=$1`, id); err != nil {
|
||||
return err
|
||||
}
|
||||
for position, value := range normalized {
|
||||
if _, err = tx.Exec(`INSERT INTO source_feeds(source_id,position,url) VALUES($1,$2,$3)`, id, position, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
@ -3,13 +3,14 @@ package models
|
||||
const (
|
||||
SourceYahooFinance = "yahoo_finance"
|
||||
SourceBloomberg = "bloomberg"
|
||||
SourceInvesting = "investing_com"
|
||||
SourceWatcherGuru = "watcher_guru" // Historical identity; no longer executable.
|
||||
SourceCNBCBusiness = "cnbc_business"
|
||||
)
|
||||
|
||||
func SupportedSourceKey(key string) bool {
|
||||
switch key {
|
||||
case SourceYahooFinance, SourceBloomberg, SourceCNBCBusiness:
|
||||
case SourceYahooFinance, SourceBloomberg, SourceCNBCBusiness, SourceInvesting:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user