adding prematch
This commit is contained in:
parent
aba4b89bb0
commit
ed0d107f1a
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
|
@ -21,6 +22,7 @@ func New(token string, store *repository.Store) *ServiceImpl {
|
|||
return &ServiceImpl{token: token, store: store}
|
||||
}
|
||||
|
||||
|
||||
func (s *ServiceImpl) FetchNonLiveOdds(ctx context.Context) error {
|
||||
sportIDs := []int{
|
||||
1, 13, 78, 18, 91, 16, 17, 14, 12, 3, 2, 4,
|
||||
|
|
@ -29,8 +31,10 @@ func (s *ServiceImpl) FetchNonLiveOdds(ctx context.Context) error {
|
|||
}
|
||||
for _, sportID := range sportIDs {
|
||||
upcomingURL := "https://api.b365api.com/v1/bet365/upcoming?sport_id=" + strconv.Itoa(sportID) + "&token=" + s.token
|
||||
log.Printf("Fetching upcoming odds for sport ID: %d from URL: %s", sportID, upcomingURL)
|
||||
resp, err := http.Get(upcomingURL)
|
||||
if err != nil {
|
||||
log.Printf("Error fetching upcoming odds for sport ID: %d, error: %v", sportID, err)
|
||||
continue
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
|
@ -43,14 +47,19 @@ func (s *ServiceImpl) FetchNonLiveOdds(ctx context.Context) error {
|
|||
} `json:"results"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &upcomingData); err != nil || upcomingData.Success != 1 {
|
||||
log.Printf("Failed to parse upcoming odds for sport ID: %d, error: %v", sportID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("Successfully fetched upcoming odds for sport ID: %d", sportID)
|
||||
|
||||
for _, ev := range upcomingData.Results {
|
||||
eventID := ev.ID
|
||||
prematchURL := "https://api.b365api.com/v3/bet365/prematch?token=" + s.token + "&FI=" + eventID
|
||||
log.Printf("Fetching prematch odds for event ID: %s from URL: %s", eventID, prematchURL)
|
||||
oddsResp, err := http.Get(prematchURL)
|
||||
if err != nil {
|
||||
log.Printf("Error fetching prematch odds for event ID: %s, error: %v", eventID, err)
|
||||
continue
|
||||
}
|
||||
defer oddsResp.Body.Close()
|
||||
|
|
@ -65,6 +74,7 @@ func (s *ServiceImpl) FetchNonLiveOdds(ctx context.Context) error {
|
|||
} `json:"results"`
|
||||
}
|
||||
if err := json.Unmarshal(oddsBody, &oddsData); err != nil || oddsData.Success != 1 || len(oddsData.Results) == 0 {
|
||||
log.Printf("Failed to parse prematch odds for event ID: %s, error: %v", eventID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -74,9 +84,11 @@ func (s *ServiceImpl) FetchNonLiveOdds(ctx context.Context) error {
|
|||
finalID = result.FI
|
||||
}
|
||||
if finalID == "" {
|
||||
log.Printf("Skipping event with missing final ID for event ID: %s", eventID)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("Storing odds for event ID: %s, final ID: %s", eventID, finalID)
|
||||
s.storeSection(ctx, finalID, result.FI, "main", result.Main)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ func StartDataFetchingCrons(eventService eventsvc.Service, oddsService oddssvc.S
|
|||
spec string
|
||||
task func()
|
||||
}{
|
||||
|
||||
{
|
||||
spec: "0 0 * * * *", // Every hour
|
||||
task: func() {
|
||||
|
|
@ -24,6 +25,8 @@ func StartDataFetchingCrons(eventService eventsvc.Service, oddsService oddssvc.S
|
|||
}
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
{
|
||||
spec: "*/5 * * * * *", // Every 5 seconds
|
||||
task: func() {
|
||||
|
|
@ -32,8 +35,10 @@ func StartDataFetchingCrons(eventService eventsvc.Service, oddsService oddssvc.S
|
|||
}
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
{
|
||||
spec: "0 */5 * * * *", // Every 5 minutes
|
||||
spec: "0 * * * * *", // Every 1 minute
|
||||
task: func() {
|
||||
if err := oddsService.FetchNonLiveOdds(context.Background()); err != nil {
|
||||
log.Printf("FetchNonLiveOdds error: %v", err)
|
||||
|
|
@ -41,10 +46,9 @@ func StartDataFetchingCrons(eventService eventsvc.Service, oddsService oddssvc.S
|
|||
},
|
||||
},
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
for _, job := range schedule {
|
||||
if _, err := c.AddFunc(job.spec, job.task); err != nil {
|
||||
log.Fatalf("Failed to schedule cron job: %v", err)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user