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,67 +22,78 @@ 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,
|
||||
83, 15, 92, 94, 8, 19, 36, 66, 9, 75, 90,
|
||||
95, 110, 107, 151, 162, 148,
|
||||
}
|
||||
for _, sportID := range sportIDs {
|
||||
upcomingURL := "https://api.b365api.com/v1/bet365/upcoming?sport_id=" + strconv.Itoa(sportID) + "&token=" + s.token
|
||||
resp, err := http.Get(upcomingURL)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
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,
|
||||
}
|
||||
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()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
var upcomingData struct {
|
||||
Success int `json:"success"`
|
||||
Results []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"results"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &upcomingData); err != nil || upcomingData.Success != 1 {
|
||||
continue
|
||||
}
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
var upcomingData struct {
|
||||
Success int `json:"success"`
|
||||
Results []struct {
|
||||
ID string `json:"id"`
|
||||
} `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
|
||||
}
|
||||
|
||||
for _, ev := range upcomingData.Results {
|
||||
eventID := ev.ID
|
||||
prematchURL := "https://api.b365api.com/v3/bet365/prematch?token=" + s.token + "&FI=" + eventID
|
||||
oddsResp, err := http.Get(prematchURL)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer oddsResp.Body.Close()
|
||||
log.Printf("Successfully fetched upcoming odds for sport ID: %d", sportID)
|
||||
|
||||
oddsBody, _ := io.ReadAll(oddsResp.Body)
|
||||
var oddsData struct {
|
||||
Success int `json:"success"`
|
||||
Results []struct {
|
||||
EventID string `json:"event_id"`
|
||||
FI string `json:"FI"`
|
||||
Main OddsSection `json:"main"`
|
||||
} `json:"results"`
|
||||
}
|
||||
if err := json.Unmarshal(oddsBody, &oddsData); err != nil || oddsData.Success != 1 || len(oddsData.Results) == 0 {
|
||||
continue
|
||||
}
|
||||
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()
|
||||
|
||||
result := oddsData.Results[0]
|
||||
finalID := result.EventID
|
||||
if finalID == "" {
|
||||
finalID = result.FI
|
||||
}
|
||||
if finalID == "" {
|
||||
continue
|
||||
}
|
||||
oddsBody, _ := io.ReadAll(oddsResp.Body)
|
||||
var oddsData struct {
|
||||
Success int `json:"success"`
|
||||
Results []struct {
|
||||
EventID string `json:"event_id"`
|
||||
FI string `json:"FI"`
|
||||
Main OddsSection `json:"main"`
|
||||
} `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
|
||||
}
|
||||
|
||||
s.storeSection(ctx, finalID, result.FI, "main", result.Main)
|
||||
}
|
||||
}
|
||||
result := oddsData.Results[0]
|
||||
finalID := result.EventID
|
||||
if finalID == "" {
|
||||
finalID = result.FI
|
||||
}
|
||||
if finalID == "" {
|
||||
log.Printf("Skipping event with missing final ID for event ID: %s", eventID)
|
||||
continue
|
||||
}
|
||||
|
||||
return nil
|
||||
log.Printf("Storing odds for event ID: %s, final ID: %s", eventID, finalID)
|
||||
s.storeSection(ctx, finalID, result.FI, "main", result.Main)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) storeSection(ctx context.Context, eventID, fi, sectionName string, section OddsSection) {
|
||||
|
|
|
|||
|
|
@ -1,56 +1,60 @@
|
|||
package httpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"context"
|
||||
"log"
|
||||
|
||||
eventsvc "github.com/SamuelTariku/FortuneBet-Backend/internal/services/event"
|
||||
oddssvc "github.com/SamuelTariku/FortuneBet-Backend/internal/services/odds"
|
||||
"github.com/robfig/cron/v3"
|
||||
eventsvc "github.com/SamuelTariku/FortuneBet-Backend/internal/services/event"
|
||||
oddssvc "github.com/SamuelTariku/FortuneBet-Backend/internal/services/odds"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
func StartDataFetchingCrons(eventService eventsvc.Service, oddsService oddssvc.Service) {
|
||||
c := cron.New(cron.WithSeconds())
|
||||
c := cron.New(cron.WithSeconds())
|
||||
|
||||
schedule := []struct {
|
||||
spec string
|
||||
task func()
|
||||
}{
|
||||
{
|
||||
spec: "0 0 * * * *", // Every hour
|
||||
task: func() {
|
||||
if err := eventService.FetchUpcomingEvents(context.Background()); err != nil {
|
||||
log.Printf(" FetchUpcomingEvents error: %v", err)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
spec: "*/5 * * * * *", // Every 5 seconds
|
||||
task: func() {
|
||||
if err := eventService.FetchLiveEvents(context.Background()); err != nil {
|
||||
log.Printf(" FetchLiveEvents error: %v", err)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
spec: "0 */5 * * * *", // Every 5 minutes
|
||||
schedule := []struct {
|
||||
spec string
|
||||
task func()
|
||||
}{
|
||||
|
||||
{
|
||||
spec: "0 0 * * * *", // Every hour
|
||||
task: func() {
|
||||
if err := eventService.FetchUpcomingEvents(context.Background()); err != nil {
|
||||
log.Printf("FetchUpcomingEvents error: %v", err)
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
{
|
||||
spec: "*/5 * * * * *", // Every 5 seconds
|
||||
task: func() {
|
||||
if err := eventService.FetchLiveEvents(context.Background()); err != nil {
|
||||
log.Printf("FetchLiveEvents error: %v", err)
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
{
|
||||
spec: "0 * * * * *", // Every 1 minute
|
||||
task: func() {
|
||||
if err := oddsService.FetchNonLiveOdds(context.Background()); err != nil {
|
||||
log.Printf(" FetchNonLiveOdds error: %v", err)
|
||||
log.Printf("FetchNonLiveOdds error: %v", err)
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for _, job := range schedule {
|
||||
if _, err := c.AddFunc(job.spec, job.task); err != nil {
|
||||
log.Fatalf(" Failed to schedule cron job: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, job := range schedule {
|
||||
if _, err := c.AddFunc(job.spec, job.task); err != nil {
|
||||
log.Fatalf("Failed to schedule cron job: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
c.Start()
|
||||
log.Println(" Cron jobs started for event and odds services")
|
||||
}
|
||||
c.Start()
|
||||
log.Println("Cron jobs started for event and odds services")
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user