prematch fetch support for betfair
This commit is contained in:
parent
68796cdf0c
commit
0960555680
|
|
@ -23,6 +23,7 @@ type Event struct {
|
||||||
MatchPeriod int
|
MatchPeriod int
|
||||||
IsLive bool
|
IsLive bool
|
||||||
Status string
|
Status string
|
||||||
|
Source string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BetResult struct {
|
type BetResult struct {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ func (s *Store) SaveEvent(ctx context.Context, e domain.Event) error {
|
||||||
MatchPeriod: pgtype.Int4{Int32: int32(e.MatchPeriod), Valid: true},
|
MatchPeriod: pgtype.Int4{Int32: int32(e.MatchPeriod), Valid: true},
|
||||||
IsLive: pgtype.Bool{Bool: e.IsLive, Valid: true},
|
IsLive: pgtype.Bool{Bool: e.IsLive, Valid: true},
|
||||||
Status: pgtype.Text{String: e.Status, Valid: true},
|
Status: pgtype.Text{String: e.Status, Valid: true},
|
||||||
|
Source: pgtype.Text{String: e.Source, Valid: true},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
func (s *Store) SaveUpcomingEvent(ctx context.Context, e domain.UpcomingEvent) error {
|
func (s *Store) SaveUpcomingEvent(ctx context.Context, e domain.UpcomingEvent) error {
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,29 @@ func New(token string, store *repository.Store) Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) FetchLiveEvents(ctx context.Context) error {
|
func (s *service) FetchLiveEvents(ctx context.Context) error {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
urls := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
}{
|
||||||
|
{"https://api.b365api.com/v1/bet365/inplay?sport_id=%d&token=%s", "bet365"},
|
||||||
|
{"https://api.b365api.com/v1/betfair/sb/inplay?sport_id=%d&token=%s", "betfair"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, url := range urls {
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
s.fetchLiveEvents(ctx, url.name, url.source)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) fetchLiveEvents(ctx context.Context, url, source string) error {
|
||||||
sportIDs := []int{1, 13, 78, 18, 91, 16, 17, 14, 12, 3, 2, 4, 83, 15, 92, 94, 8, 19, 36, 66, 9, 75, 90, 95, 110, 107, 151, 162, 148}
|
sportIDs := []int{1, 13, 78, 18, 91, 16, 17, 14, 12, 3, 2, 4, 83, 15, 92, 94, 8, 19, 36, 66, 9, 75, 90, 95, 110, 107, 151, 162, 148}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
@ -39,7 +62,7 @@ func (s *service) FetchLiveEvents(ctx context.Context) error {
|
||||||
go func(sportID int) {
|
go func(sportID int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
url := fmt.Sprintf("https://api.b365api.com/v1/bet365/inplay?sport_id=%d&token=%s", sportID, s.token)
|
url := fmt.Sprintf(url, sportID, s.token)
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf(" Failed request for sport_id=%d: %v\n", sportID, err)
|
fmt.Printf(" Failed request for sport_id=%d: %v\n", sportID, err)
|
||||||
|
|
@ -49,13 +72,38 @@ func (s *service) FetchLiveEvents(ctx context.Context) error {
|
||||||
|
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
events := []domain.Event{}
|
||||||
|
switch source {
|
||||||
|
case "bet365":
|
||||||
|
events = handleBet365prematch(body, sportID)
|
||||||
|
case "betfair":
|
||||||
|
events = handleBetfairprematch(body, sportID)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, event := range events {
|
||||||
|
if err := s.store.SaveEvent(ctx, event); err != nil {
|
||||||
|
fmt.Printf("Could not store live event [id=%s]: %v\n", event.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(sportID)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
fmt.Println("All live events fetched and stored.")
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleBet365prematch(body []byte, sportID int) []domain.Event {
|
||||||
var data struct {
|
var data struct {
|
||||||
Success int `json:"success"`
|
Success int `json:"success"`
|
||||||
Results [][]map[string]interface{} `json:"results"`
|
Results [][]map[string]interface{} `json:"results"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
events := []domain.Event{}
|
||||||
if err := json.Unmarshal(body, &data); err != nil || data.Success != 1 {
|
if err := json.Unmarshal(body, &data); err != nil || data.Success != 1 {
|
||||||
fmt.Printf(" Decode failed for sport_id=%d\nRaw: %s\n", sportID, string(body))
|
fmt.Printf(" Decode failed for sport_id=%d\nRaw: %s\n", sportID, string(body))
|
||||||
return
|
return events
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, group := range data.Results {
|
for _, group := range data.Results {
|
||||||
|
|
@ -83,19 +131,51 @@ func (s *service) FetchLiveEvents(ctx context.Context) error {
|
||||||
Status: "live",
|
Status: "live",
|
||||||
MatchPeriod: getInt(ev["MD"]),
|
MatchPeriod: getInt(ev["MD"]),
|
||||||
AddedTime: getInt(ev["TA"]),
|
AddedTime: getInt(ev["TA"]),
|
||||||
|
Source: "bet365",
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.store.SaveEvent(ctx, event); err != nil {
|
events = append(events, event)
|
||||||
fmt.Printf("Could not store live event [id=%s]: %v\n", event.ID, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}(sportID)
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
return events
|
||||||
fmt.Println("All live events fetched and stored.")
|
}
|
||||||
return nil
|
|
||||||
|
func handleBetfairprematch(body []byte, sportID int) []domain.Event {
|
||||||
|
var data struct {
|
||||||
|
Success int `json:"success"`
|
||||||
|
Results []map[string]interface{} `json:"results"`
|
||||||
|
}
|
||||||
|
|
||||||
|
events := []domain.Event{}
|
||||||
|
if err := json.Unmarshal(body, &data); err != nil || data.Success != 1 {
|
||||||
|
fmt.Printf(" Decode failed for sport_id=%d\nRaw: %s\n", sportID, string(body))
|
||||||
|
return events
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ev := range data.Results {
|
||||||
|
homeRaw, _ := ev["home"].(map[string]interface{})
|
||||||
|
homeId, _ := homeRaw["id"].(string)
|
||||||
|
|
||||||
|
awayRaw, _ := ev["home"].(map[string]interface{})
|
||||||
|
awayId, _ := awayRaw["id"].(string)
|
||||||
|
|
||||||
|
event := domain.Event{
|
||||||
|
ID: getString(ev["id"]),
|
||||||
|
SportID: fmt.Sprintf("%d", sportID),
|
||||||
|
TimerStatus: getString(ev["time_status"]),
|
||||||
|
HomeTeamID: homeId,
|
||||||
|
AwayTeamID: awayId,
|
||||||
|
StartTime: time.Now().UTC().Format(time.RFC3339),
|
||||||
|
IsLive: true,
|
||||||
|
Status: "live",
|
||||||
|
Source: "betfair",
|
||||||
|
}
|
||||||
|
|
||||||
|
events = append(events, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
return events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) FetchUpcomingEvents(ctx context.Context) error {
|
func (s *service) FetchUpcomingEvents(ctx context.Context) error {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user