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