added
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package etoro
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const instrumentsURL = "https://api.etoro.com/metadata/instruments"
|
||||
|
||||
// InstrumentTypeID connus sur eToro
|
||||
const (
|
||||
TypeStock = 5
|
||||
TypeETF = 10
|
||||
TypeCrypto = 12
|
||||
TypeIndex = 21
|
||||
TypeCFD = 6
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
type Instrument struct {
|
||||
InstrumentID int `json:"InstrumentID"`
|
||||
InstrumentDisplayName string `json:"InstrumentDisplayName"`
|
||||
SymbolFull string `json:"SymbolFull"`
|
||||
InstrumentTypeID int `json:"InstrumentTypeID"`
|
||||
IsActive bool `json:"IsActive"`
|
||||
StockIndustryID int `json:"StockIndustryID"`
|
||||
StockExchangeID int `json:"StockExchangeID"`
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
return &Client{
|
||||
http: &http.Client{Timeout: 30 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
// FetchStocks retourne tous les instruments de type Stock actifs sur eToro.
|
||||
func (c *Client) FetchStocks() ([]Instrument, error) {
|
||||
all, err := c.fetchAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var stocks []Instrument
|
||||
for _, inst := range all {
|
||||
if inst.IsActive && inst.InstrumentTypeID == TypeStock {
|
||||
stocks = append(stocks, inst)
|
||||
}
|
||||
}
|
||||
return stocks, nil
|
||||
}
|
||||
|
||||
// FetchAll retourne tous les instruments (stocks + ETFs + crypto + indices).
|
||||
func (c *Client) FetchAll() ([]Instrument, error) {
|
||||
return c.fetchAll()
|
||||
}
|
||||
|
||||
func (c *Client) fetchAll() ([]Instrument, error) {
|
||||
req, err := http.NewRequest("GET", instrumentsURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Headers qui imitent le client web eToro
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
|
||||
req.Header.Set("accounttype", "Demo")
|
||||
req.Header.Set("ApplicationIdentifier", "ReToro")
|
||||
req.Header.Set("Version", "1.211.0")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("etoro: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("etoro: HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var instruments []Instrument
|
||||
if err := json.NewDecoder(resp.Body).Decode(&instruments); err != nil {
|
||||
return nil, fmt.Errorf("etoro: parse error: %w", err)
|
||||
}
|
||||
|
||||
if len(instruments) == 0 {
|
||||
return nil, fmt.Errorf("etoro: empty response — l'API a peut-être changé")
|
||||
}
|
||||
|
||||
return instruments, nil
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package etoro
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.rouggy.com/rouggy/stockradar/internal/db"
|
||||
)
|
||||
|
||||
type SyncStatus struct {
|
||||
Syncing bool `json:"syncing"`
|
||||
Progress int `json:"progress"`
|
||||
Total int `json:"total"`
|
||||
Count int `json:"count"`
|
||||
LastSync time.Time `json:"last_sync"`
|
||||
LastError string `json:"last_error,omitempty"`
|
||||
}
|
||||
|
||||
type Poller struct {
|
||||
db *db.DB
|
||||
client *Client
|
||||
ticker *time.Ticker
|
||||
done chan struct{}
|
||||
|
||||
mu sync.Mutex
|
||||
syncing bool
|
||||
progress int
|
||||
total int
|
||||
lastSync time.Time
|
||||
lastError string
|
||||
}
|
||||
|
||||
func NewPoller(database *db.DB) *Poller {
|
||||
return &Poller{
|
||||
db: database,
|
||||
client: New(),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Poller) Start() {
|
||||
p.ticker = time.NewTicker(24 * time.Hour)
|
||||
go func() {
|
||||
if err := p.Sync(); err != nil {
|
||||
log.Printf("etoro poller: initial sync: %v", err)
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-p.ticker.C:
|
||||
if err := p.Sync(); err != nil {
|
||||
log.Printf("etoro poller: sync: %v", err)
|
||||
}
|
||||
case <-p.done:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (p *Poller) Stop() {
|
||||
if p.ticker != nil {
|
||||
p.ticker.Stop()
|
||||
}
|
||||
close(p.done)
|
||||
}
|
||||
|
||||
func (p *Poller) Status() SyncStatus {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
return SyncStatus{
|
||||
Syncing: p.syncing,
|
||||
Progress: p.progress,
|
||||
Total: p.total,
|
||||
Count: p.dbCount(),
|
||||
LastSync: p.lastSync,
|
||||
LastError: p.lastError,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Poller) Sync() error {
|
||||
p.mu.Lock()
|
||||
if p.syncing {
|
||||
p.mu.Unlock()
|
||||
return nil // déjà en cours
|
||||
}
|
||||
p.syncing = true
|
||||
p.progress = 0
|
||||
p.total = 0
|
||||
p.lastError = ""
|
||||
p.mu.Unlock()
|
||||
|
||||
defer func() {
|
||||
p.mu.Lock()
|
||||
p.syncing = false
|
||||
p.lastSync = time.Now()
|
||||
p.mu.Unlock()
|
||||
}()
|
||||
|
||||
log.Println("etoro: fetching instruments…")
|
||||
stocks, err := p.client.FetchStocks()
|
||||
if err != nil {
|
||||
p.mu.Lock()
|
||||
p.lastError = err.Error()
|
||||
p.mu.Unlock()
|
||||
log.Printf("etoro: fetch error: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
p.total = len(stocks)
|
||||
p.mu.Unlock()
|
||||
|
||||
log.Printf("etoro: %d stocks à synchroniser", len(stocks))
|
||||
|
||||
inserted := 0
|
||||
for i, s := range stocks {
|
||||
_, err := p.db.Exec(`
|
||||
INSERT INTO instruments (instrument_id, ticker, name, exchange_id, asset_class_id, synced_at)
|
||||
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(instrument_id) DO UPDATE SET
|
||||
ticker = excluded.ticker,
|
||||
name = excluded.name,
|
||||
exchange_id = excluded.exchange_id,
|
||||
synced_at = CURRENT_TIMESTAMP
|
||||
`, s.InstrumentID, s.SymbolFull, s.InstrumentDisplayName,
|
||||
s.StockExchangeID, s.InstrumentTypeID)
|
||||
if err == nil {
|
||||
inserted++
|
||||
}
|
||||
|
||||
if (i+1)%100 == 0 || i+1 == len(stocks) {
|
||||
p.mu.Lock()
|
||||
p.progress = i + 1
|
||||
p.mu.Unlock()
|
||||
log.Printf("etoro: %d/%d instruments traités", i+1, len(stocks))
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("etoro: sync terminée — %d/%d instruments en DB", inserted, len(stocks))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Poller) dbCount() int {
|
||||
var n int
|
||||
p.db.QueryRow(`SELECT COUNT(*) FROM instruments`).Scan(&n)
|
||||
return n
|
||||
}
|
||||
|
||||
// IsEtoro vérifie si un ticker est dans l'univers eToro.
|
||||
func IsEtoro(database *db.DB, ticker string) bool {
|
||||
var count int
|
||||
database.QueryRow(`SELECT COUNT(*) FROM instruments WHERE ticker = ?`, ticker).Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
Reference in New Issue
Block a user