49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package bloomberg
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/tradarr/backend/internal/crypto"
|
|
"github.com/tradarr/backend/internal/models"
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
)
|
|
|
|
type DynamicBloomberg struct {
|
|
repo *models.Repository
|
|
enc *crypto.Encryptor
|
|
scraperURL string
|
|
}
|
|
|
|
func NewDynamic(repo *models.Repository, enc *crypto.Encryptor, scraperURL string) *DynamicBloomberg {
|
|
return &DynamicBloomberg{repo: repo, enc: enc, scraperURL: scraperURL}
|
|
}
|
|
|
|
func (d *DynamicBloomberg) Name() string { return "bloomberg" }
|
|
|
|
func (d *DynamicBloomberg) Scrape(ctx context.Context, symbols []string) ([]scraper.Article, error) {
|
|
source, err := d.repo.GetSourceByType("bloomberg")
|
|
if err != nil || source == nil {
|
|
return nil, fmt.Errorf("bloomberg source not found")
|
|
}
|
|
|
|
cred, err := d.repo.GetCredentials(source.ID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get bloomberg credentials: %w", err)
|
|
}
|
|
if cred == nil || cred.Username == "" {
|
|
return nil, fmt.Errorf("bloomberg credentials not configured — configure them in the admin panel")
|
|
}
|
|
|
|
password := ""
|
|
if cred.PasswordEncrypted != "" {
|
|
password, err = d.enc.Decrypt(cred.PasswordEncrypted)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("decrypt bloomberg password: %w", err)
|
|
}
|
|
}
|
|
|
|
b := New(d.scraperURL)
|
|
return b.ScrapeWithCredentials(ctx, cred.Username, password, symbols)
|
|
}
|