penguin/golifehk

@golifehk bot in Telegram

commit 6245ef62906dc3a424b579044d9a4624674e5ee1

author斟酌 鵬兄 <tgckpg@gmail.com>
date2026-02-27T12:07:10Z
subjectFixed kmb again for schedules
commit 6245ef62906dc3a424b579044d9a4624674e5ee1
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2026-02-27T12:07:10Z

    Fixed kmb again for schedules
---
 datasources/kmb/schedules.go | 95 +++++++++++++++++++++-----------------------
 1 file changed, 45 insertions(+), 50 deletions(-)

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