up
This commit is contained in:
+90
-59
@@ -5,31 +5,35 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const instrumentsURL = "https://api.etoro.com/metadata/instruments"
|
||||
|
||||
// InstrumentTypeID connus sur eToro
|
||||
const (
|
||||
TypeStock = 5
|
||||
TypeETF = 10
|
||||
TypeCrypto = 12
|
||||
TypeIndex = 21
|
||||
TypeCFD = 6
|
||||
)
|
||||
const baseURL = "https://public-api.etoro.com/api/v1"
|
||||
|
||||
type Client struct {
|
||||
http *http.Client
|
||||
http *http.Client
|
||||
apiKey string
|
||||
userKey string
|
||||
}
|
||||
|
||||
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"`
|
||||
InstrumentID int `json:"internalInstrumentId"`
|
||||
InstrumentDisplayName string `json:"internalInstrumentDisplayName"`
|
||||
SymbolFull string `json:"internalSymbolFull"`
|
||||
AssetClassID int `json:"internalAssetClassId"`
|
||||
ExchangeID int `json:"internalExchangeId"`
|
||||
IsHidden bool `json:"isHiddenFromClient"`
|
||||
IsDelisted bool `json:"isDelisted"`
|
||||
IsActiveInPlatform bool `json:"isActiveInPlatform"`
|
||||
IsBuyEnabled bool `json:"isBuyEnabled"`
|
||||
}
|
||||
|
||||
type searchResponse struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
TotalItems int `json:"totalItems"`
|
||||
Items []Instrument `json:"items"`
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
@@ -38,58 +42,85 @@ func New() *Client {
|
||||
}
|
||||
}
|
||||
|
||||
// FetchStocks retourne tous les instruments de type Stock actifs sur eToro.
|
||||
func (c *Client) FetchStocks() ([]Instrument, error) {
|
||||
all, err := c.fetchAll()
|
||||
func NewWithKeys(apiKey, userKey string) *Client {
|
||||
return &Client{
|
||||
http: &http.Client{Timeout: 30 * time.Second},
|
||||
apiKey: apiKey,
|
||||
userKey: userKey,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) SetKeys(apiKey, userKey string) {
|
||||
c.apiKey = apiKey
|
||||
c.userKey = userKey
|
||||
}
|
||||
|
||||
func (c *Client) get(path string) (*http.Response, error) {
|
||||
req, err := http.NewRequest("GET", baseURL+path, nil)
|
||||
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("x-api-key", c.apiKey)
|
||||
req.Header.Set("x-user-key", c.userKey)
|
||||
req.Header.Set("x-request-id", uuid.NewString())
|
||||
req.Header.Set("Accept", "application/json")
|
||||
return c.http.Do(req)
|
||||
}
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
func (c *Client) fetchPage(assetClassID, pageSize, page int) (*searchResponse, error) {
|
||||
resp, err := c.get(fmt.Sprintf("/market-data/search?internalAssetClassId=%d&pageSize=%d&page=%d", assetClassID, pageSize, page))
|
||||
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)
|
||||
var sr searchResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&sr); err != nil {
|
||||
return nil, fmt.Errorf("etoro: parse: %w", err)
|
||||
}
|
||||
|
||||
if len(instruments) == 0 {
|
||||
return nil, fmt.Errorf("etoro: empty response — l'API a peut-être changé")
|
||||
}
|
||||
|
||||
return instruments, nil
|
||||
return &sr, nil
|
||||
}
|
||||
|
||||
// FetchStocks retourne tous les stocks actifs disponibles sur eToro (toutes pages).
|
||||
func (c *Client) FetchStocks() ([]Instrument, error) {
|
||||
if c.apiKey == "" || c.userKey == "" {
|
||||
return nil, fmt.Errorf("etoro: clés API non configurées")
|
||||
}
|
||||
|
||||
const pageSize = 500
|
||||
var all []Instrument
|
||||
|
||||
for page := 1; ; page++ {
|
||||
resp, err := c.get(fmt.Sprintf("/market-data/search?internalAssetClassId=5&pageSize=%d&page=%d", pageSize, page))
|
||||
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 sr searchResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&sr); err != nil {
|
||||
return nil, fmt.Errorf("etoro: parse: %w", err)
|
||||
}
|
||||
|
||||
for _, inst := range sr.Items {
|
||||
if !inst.IsHidden && !inst.IsDelisted && inst.IsBuyEnabled {
|
||||
all = append(all, inst)
|
||||
}
|
||||
}
|
||||
|
||||
if page*pageSize >= sr.TotalItems {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(all) == 0 {
|
||||
return nil, fmt.Errorf("etoro: aucun stock retourné")
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
|
||||
+69
-38
@@ -1,6 +1,7 @@
|
||||
package etoro
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -18,10 +19,11 @@ type SyncStatus struct {
|
||||
}
|
||||
|
||||
type Poller struct {
|
||||
db *db.DB
|
||||
client *Client
|
||||
ticker *time.Ticker
|
||||
done chan struct{}
|
||||
db *db.DB
|
||||
client *Client
|
||||
getKeys func() (apiKey, userKey string, err error)
|
||||
ticker *time.Ticker
|
||||
done chan struct{}
|
||||
|
||||
mu sync.Mutex
|
||||
syncing bool
|
||||
@@ -31,19 +33,25 @@ type Poller struct {
|
||||
lastError string
|
||||
}
|
||||
|
||||
func NewPoller(database *db.DB) *Poller {
|
||||
func NewPoller(database *db.DB, getKeys func() (string, string, error)) *Poller {
|
||||
return &Poller{
|
||||
db: database,
|
||||
client: New(),
|
||||
done: make(chan struct{}),
|
||||
db: database,
|
||||
client: New(),
|
||||
getKeys: getKeys,
|
||||
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)
|
||||
// Sync uniquement si la DB est vide
|
||||
if p.dbCount() == 0 {
|
||||
if err := p.Sync(); err != nil {
|
||||
log.Printf("etoro poller: initial sync: %v", err)
|
||||
}
|
||||
} else {
|
||||
log.Printf("etoro: %d instruments déjà en DB, sync ignorée au démarrage", p.dbCount())
|
||||
}
|
||||
for {
|
||||
select {
|
||||
@@ -97,47 +105,70 @@ func (p *Poller) Sync() error {
|
||||
p.mu.Unlock()
|
||||
}()
|
||||
|
||||
log.Println("etoro: fetching instruments…")
|
||||
stocks, err := p.client.FetchStocks()
|
||||
if err != nil {
|
||||
apiKey, userKey, err := p.getKeys()
|
||||
if err != nil || apiKey == "" || userKey == "" {
|
||||
p.mu.Lock()
|
||||
p.lastError = err.Error()
|
||||
p.lastError = "clés API eToro non configurées (Settings)"
|
||||
p.mu.Unlock()
|
||||
log.Printf("etoro: fetch error: %v", err)
|
||||
return err
|
||||
return fmt.Errorf("etoro: clés manquantes")
|
||||
}
|
||||
p.client.SetKeys(apiKey, userKey)
|
||||
|
||||
p.mu.Lock()
|
||||
p.total = len(stocks)
|
||||
p.mu.Unlock()
|
||||
|
||||
log.Printf("etoro: %d stocks à synchroniser", len(stocks))
|
||||
log.Println("etoro: fetching instruments…")
|
||||
|
||||
const pageSize = 500
|
||||
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++
|
||||
fetched := 0
|
||||
|
||||
for page := 1; ; page++ {
|
||||
sr, err := p.client.fetchPage(5, pageSize, page)
|
||||
if err != nil {
|
||||
p.mu.Lock()
|
||||
p.lastError = err.Error()
|
||||
p.mu.Unlock()
|
||||
log.Printf("etoro: fetch error page %d: %v", page, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if (i+1)%100 == 0 || i+1 == len(stocks) {
|
||||
// On connaît le total dès la première page
|
||||
if page == 1 {
|
||||
p.mu.Lock()
|
||||
p.progress = i + 1
|
||||
p.total = sr.TotalItems
|
||||
p.mu.Unlock()
|
||||
log.Printf("etoro: %d/%d instruments traités", i+1, len(stocks))
|
||||
log.Printf("etoro: %d stocks à synchroniser", sr.TotalItems)
|
||||
}
|
||||
|
||||
for _, s := range sr.Items {
|
||||
if s.IsHidden || s.IsDelisted || !s.IsBuyEnabled {
|
||||
continue
|
||||
}
|
||||
_, 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.ExchangeID, s.AssetClassID)
|
||||
if err == nil {
|
||||
inserted++
|
||||
}
|
||||
fetched++
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
p.progress = fetched
|
||||
p.mu.Unlock()
|
||||
log.Printf("etoro: page %d — %d/%d traités", page, fetched, sr.TotalItems)
|
||||
|
||||
if page*pageSize >= sr.TotalItems {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("etoro: sync terminée — %d/%d instruments en DB", inserted, len(stocks))
|
||||
log.Printf("etoro: sync terminée — %d instruments en DB", inserted)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user