Fixed kmb again for schedules

This commit is contained in:
2026-02-27 20:07:10 +08:00
parent cda11cef2e
commit 6245ef6290

View File

@@ -1,62 +1,57 @@
package kmb package kmb
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "path/filepath"
"path/filepath" "time"
"time"
"github.com/tgckpg/golifehk/utils" "github.com/tgckpg/golifehk/utils"
) )
type Schedule struct { type Schedule struct {
ETA time.Time `json:"eta,string"` ETA time.Time `json:"eta,string"`
Index int `json:"eta_seq"` Index int `json:"eta_seq"`
DateCreated time.Time `json:"data_timestamp,string"` DateCreated time.Time `json:"data_timestamp,string"`
Remarks_en string `json:"rmk_en"` Remarks_en string `json:"rmk_en"`
Remarks_sc string `json:"rmk_sc"` Remarks_sc string `json:"rmk_sc"`
Remarks_tc string `json:"rmk_tc"` Remarks_tc string `json:"rmk_tc"`
} }
type Schedules struct { type Schedules struct {
Type string `json:"type"` Type string `json:"type"`
Version string `json:"version"` Version string `json:"version"`
DateCreated time.Time `json:"generated_timestamp,string"` DateCreated time.Time `json:"generated_timestamp,string"`
Schedules [] *Schedule `json:"data"` Schedules []*Schedule `json:"data"`
} }
func getSchedule( r *RouteStop ) ( *[] *Schedule, error ) { func getSchedule(r *RouteStop) (*[]*Schedule, error) {
CACHE_PATH := filepath.Join( CACHE_PATH := filepath.Join(
utils.WORKDIR, utils.WORKDIR,
"kmb-" + r.BusStop.BusStopId + "-" + r.RouteId + "-" + r.ServiceType + ".json", "kmb-"+r.BusStop.BusStopId+"-"+r.RouteId+"-"+r.ServiceType+".json",
) )
QUERY_FUNC := func() ( io.ReadCloser, error ) { QUERY_FUNC := func() (io.ReadCloser, error) {
resp, err := http.Get( fmt.Sprintf( return utils.HttpGet(fmt.Sprintf(
"https://data.etabus.gov.hk/v1/transport/kmb/eta/%s/%s/%s", "https://data.etabus.gov.hk/v1/transport/kmb/eta/%s/%s/%s",
r.BusStop.BusStopId, r.BusStop.BusStopId,
r.RouteId, r.RouteId,
r.ServiceType, r.ServiceType,
) ) ))
if err != nil { }
return nil, err
}
return resp.Body, nil
}
buff, err := utils.CacheStream( CACHE_PATH, QUERY_FUNC, 60 ) buff, err := utils.CacheStream(CACHE_PATH, QUERY_FUNC, 60)
if err != nil { if err != nil {
return nil, err return nil, err
} }
schedules := Schedules{} schedules := Schedules{}
err = json.Unmarshal( buff.Bytes(), &schedules ) err = json.Unmarshal(buff.Bytes(), &schedules)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &schedules.Schedules, nil return &schedules.Schedules, nil
} }