Handle new special route listing for mtr buses

This commit is contained in:
2025-12-16 11:10:27 +08:00
parent 0ed4c618dc
commit 6e94adbed4
3 changed files with 250 additions and 239 deletions

View File

@@ -1,62 +1,63 @@
package bus package bus
import ( import (
i18n "github.com/tgckpg/golifehk/i18n" i18n "github.com/tgckpg/golifehk/i18n"
) )
type BusStop struct { type BusStop struct {
RouteId string RouteId string
Direction string ReferenceId string
StationSeq int Direction string
StationId string StationSeq int
Latitude float64 StationId string
Longtitude float64 Latitude float64
Name_zh string Longtitude float64
Name_en string Name_zh string
Name_en string
// RouteStops[ StationSeq ] = BusStop // RouteStops[ StationSeq ] = BusStop
RouteStops *map[int] *BusStop RouteStops *map[int]*BusStop
i18n.Generics i18n.Generics
} }
func ( this *BusStop ) PrevStop() *BusStop { func (this *BusStop) PrevStop() *BusStop {
if v, hasKey := (*this.RouteStops)[ this.StationSeq - 1 ]; hasKey { if v, hasKey := (*this.RouteStops)[this.StationSeq-1]; hasKey {
return v return v
} }
return nil return nil
} }
func ( this *BusStop ) NextStop() *BusStop { func (this *BusStop) NextStop() *BusStop {
if v, hasKey := (*this.RouteStops)[ this.StationSeq + 1 ]; hasKey { if v, hasKey := (*this.RouteStops)[this.StationSeq+1]; hasKey {
return v return v
} }
return nil return nil
} }
func ( this *BusStop ) Reload() { func (this *BusStop) Reload() {
i18n_Name := map[string] string{} i18n_Name := map[string]string{}
i18n_Name["en"] = this.Name_en i18n_Name["en"] = this.Name_en
i18n_Name["zh-Hant"] = this.Name_zh i18n_Name["zh-Hant"] = this.Name_zh
searchData := [] *string{} searchData := []*string{}
searchData = append( searchData, &this.Name_en ) searchData = append(searchData, &this.Name_en)
searchData = append( searchData, &this.Name_zh ) searchData = append(searchData, &this.Name_zh)
this.Name = &i18n_Name this.Name = &i18n_Name
this.Key = &this.RouteId this.Key = &this.RouteId
this.SearchData = &searchData this.SearchData = &searchData
} }
type ByRoute [] *BusStop type ByRoute []*BusStop
func (a ByRoute) Len() int { return len(a) } func (a ByRoute) Len() int { return len(a) }
func (a ByRoute) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByRoute) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRoute) Less(i, j int) bool { func (a ByRoute) Less(i, j int) bool {
_a := *a[i] _a := *a[i]
_b := *a[j] _b := *a[j]
if _a.RouteId == _b.RouteId { if _a.RouteId == _b.RouteId {
return _a.Direction < _b.Direction return _a.Direction < _b.Direction
} }
return _a.RouteId < _b.RouteId return _a.RouteId < _b.RouteId
} }

View File

@@ -1,146 +1,146 @@
package bus package bus
import ( import (
"fmt" "bytes"
"bytes" "encoding/json"
"encoding/json" "fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
"path/filepath" "path/filepath"
"time" "time"
"github.com/tgckpg/golifehk/utils" "github.com/tgckpg/golifehk/utils"
) )
type Location struct { type Location struct {
latitude float32 latitude float32
longitude float32 longitude float32
} }
type Bus struct { type Bus struct {
ETA int `json:"arrivalTimeInSecond,string"` ETA int `json:"arrivalTimeInSecond,string"`
ETAText string `json:"arrivalTimeText"` ETAText string `json:"arrivalTimeText"`
BusId string `json:"busId"` BusId string `json:"busId"`
BusLocation Location `json:"busLocation"` BusLocation Location `json:"busLocation"`
ETD string `json:"departureTimeInSecond"` ETD string `json:"departureTimeInSecond"`
ETDText string `json:"departureTimeText"` ETDText string `json:"departureTimeText"`
Delayed string `json:"isDelayed"` Delayed string `json:"isDelayed"`
Scheduled string `json:"isScheduled"` Scheduled string `json:"isScheduled"`
LineRef string `json:"lineRef"` LineRef string `json:"lineRef"`
} }
type BusStopBuses struct { type BusStopBuses struct {
Buses [] Bus `json:"bus"` Buses []Bus `json:"bus"`
BusStopId string `json:"busStopId"` BusStopId string `json:"busStopId"`
Suspended string `json:"isSuspended"` Suspended string `json:"isSuspended"`
} }
type ScheduleStatusTime struct { type ScheduleStatusTime struct {
time.Time time.Time
} }
type BusSchedule struct { type BusSchedule struct {
RefreshTime int `json:"appRefreshTimeInSecond,string"` RefreshTime int `json:"appRefreshTimeInSecond,string"`
BusStops [] BusStopBuses `json:"busStop"` BusStops []BusStopBuses `json:"busStop"`
StatusTime ScheduleStatusTime `json:"routeStatusTime"` StatusTime ScheduleStatusTime `json:"routeStatusTime"`
RemarksTitle string `json:"routeStatusRemarkTitle"` RemarksTitle string `json:"routeStatusRemarkTitle"`
// 0 = OK // 0 = OK
// 100 = Rate limit ( Unconfirmed. Not in spec ) // 100 = Rate limit ( Unconfirmed. Not in spec )
Status int `json:"status,string"` Status int `json:"status,string"`
} }
func (t *ScheduleStatusTime) UnmarshalJSON(b []byte) (err error) { func (t *ScheduleStatusTime) UnmarshalJSON(b []byte) (err error) {
date, err := time.Parse(`"2006\/01\/02 15:04"`, string(b)) date, err := time.Parse(`"2006\/01\/02 15:04"`, string(b))
if err != nil { if err != nil {
return err return err
} }
t.Time = date t.Time = date
return return
} }
func getSchedule( lang string, routeName string ) ( *BusSchedule, error ) { func getSchedule(lang string, routeName string) (*BusSchedule, error) {
CACHE_PATH := filepath.Join( CACHE_PATH := filepath.Join(
utils.WORKDIR, utils.WORKDIR,
"mtr_bsch" + "-" + lang + "-" + routeName + ".json", "mtr_bsch"+"-"+lang+"-"+routeName+".json",
) )
postLang := "en" postLang := "en"
if lang == "zh-Hant" { if lang == "zh-Hant" {
postLang = "zh" postLang = "zh"
} }
QUERY_FUNC := func() ( io.ReadCloser, error ) { QUERY_FUNC := func() (io.ReadCloser, error) {
// Query Remote // Query Remote
values := map[string]string { "language": postLang, "routeName": routeName } values := map[string]string{"language": postLang, "routeName": routeName}
jsonValue, _ := json.Marshal(values) jsonValue, _ := json.Marshal(values)
resp, err := http.Post( resp, err := http.Post(
"https://rt.data.gov.hk/v1/transport/mtr/bus/getSchedule", "https://rt.data.gov.hk/v1/transport/mtr/bus/getSchedule",
"application/json", "application/json",
bytes.NewBuffer( jsonValue ), bytes.NewBuffer(jsonValue),
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.Body, nil return resp.Body, nil
} }
cs, err := utils.CacheStreamEx( CACHE_PATH, QUERY_FUNC ) cs, err := utils.CacheStreamEx(CACHE_PATH, QUERY_FUNC)
if err != nil { if err != nil {
return nil, err return nil, err
} }
oldSch := BusSchedule{ oldSch := BusSchedule{
Status: -1, Status: -1,
} }
if cs.Local != nil { if cs.Local != nil {
err = json.Unmarshal( cs.Local.Bytes(), &oldSch ) err = json.Unmarshal(cs.Local.Bytes(), &oldSch)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
newSch := BusSchedule{ newSch := BusSchedule{
Status: -1, Status: -1,
} }
for i := 0; i < 3; i ++ { for i := 0; i < 3; i++ {
if cs.Remote != nil { if cs.Remote != nil {
err = json.Unmarshal( cs.Remote.Bytes(), &newSch ) err = json.Unmarshal(cs.Remote.Bytes(), &newSch)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
if newSch.Status == 0 { if newSch.Status == 0 {
cs.Commit() cs.Commit()
return &newSch, nil return &newSch, nil
} }
if oldSch.Status == 0 { if oldSch.Status == 0 {
if cs.NotExpired( 60 ) || oldSch.StatusTime.Time == newSch.StatusTime.Time { if cs.NotExpired(60) || oldSch.StatusTime.Time == newSch.StatusTime.Time {
log.Printf( "Using cache: %s", CACHE_PATH ) log.Printf("Using cache: %s", CACHE_PATH)
return &oldSch, nil return &oldSch, nil
} }
} }
// First time + try again i times // First time + try again i times
err = cs.Reload() err = cs.Reload()
log.Printf( "Reloading (%d): %s", i, CACHE_PATH ) log.Printf("Reloading (%d): %s", i, CACHE_PATH)
if err != nil { if err != nil {
err = fmt.Errorf( "Error retrieving data: %s", err ) err = fmt.Errorf("Error retrieving data: %s", err)
return nil, err return nil, err
} }
} }
if newSch.Status != 0 { if newSch.Status != 0 {
err = fmt.Errorf( "%s (%d)", newSch.RemarksTitle, newSch.Status ) err = fmt.Errorf("%s (%d)", newSch.RemarksTitle, newSch.Status)
} }
return &newSch, err return &newSch, err
} }

View File

@@ -1,120 +1,130 @@
package bus package bus
import ( import (
"encoding/csv" "encoding/csv"
"fmt" "fmt"
"io" "io"
"net/http" "log"
"path/filepath" "net/http"
"strconv" "path/filepath"
"strconv"
query "github.com/tgckpg/golifehk/query" query "github.com/tgckpg/golifehk/query"
"github.com/tgckpg/golifehk/utils" "github.com/tgckpg/golifehk/utils"
) )
var CSV_BUSSTOPS string = filepath.Join( utils.WORKDIR, "mtr_bus_stops.csv" ) var CSV_BUSSTOPS string = filepath.Join(utils.WORKDIR, "mtr_bus_stops.csv")
func readBusStopData( r io.Reader ) ( *map[string] *BusStop, error ) { func readBusStopData(r io.Reader) (*map[string]*BusStop, error) {
reader := csv.NewReader( r ) reader := csv.NewReader(r)
entries, err := reader.ReadAll() entries, err := reader.ReadAll()
if err != nil { if err != nil {
return nil, err return nil, err
} }
busStops := map[string] *BusStop{} busStops := map[string]*BusStop{}
routeStops := map[string] map[string] map[int] *BusStop{} routeStops := map[string]map[string]map[int]*BusStop{}
var headers []string var headers []string
for i, line := range entries { for i, line := range entries {
if i == 0 { if i == 0 {
headers = line headers = line
continue continue
} }
var entry BusStop var entry BusStop
for j, value := range line { for j, value := range line {
switch headers[j] { switch headers[j] {
case "ROUTE_ID": case "ROUTE_ID":
entry.RouteId = value entry.RouteId = value
case "DIRECTION": case "DIRECTION":
entry.Direction = value entry.Direction = value
case "STATION_SEQNO": case "STATION_SEQNO":
v, _ := strconv.ParseFloat( value, 64 ) v, _ := strconv.ParseFloat(value, 64)
entry.StationSeq = int( v ) entry.StationSeq = int(v)
case "STATION_ID": case "STATION_ID":
entry.StationId = value entry.StationId = value
case "STATION_LATITUDE": case "STATION_LATITUDE":
v, _ := strconv.ParseFloat( value, 64 ) v, _ := strconv.ParseFloat(value, 64)
entry.Latitude = v entry.Latitude = v
case "STATION_LONGITUDE": case "STATION_LONGITUDE":
v, _ := strconv.ParseFloat( value, 64 ) v, _ := strconv.ParseFloat(value, 64)
entry.Longtitude = v entry.Longtitude = v
case "STATION_NAME_CHI": case "STATION_NAME_CHI":
entry.Name_zh = value entry.Name_zh = value
case "STATION_NAME_ENG": case "STATION_NAME_ENG":
entry.Name_en = value entry.Name_en = value
default: case "REFERENCE_ID":
return nil, fmt.Errorf( "Unknown header \"%s\"", headers[j] ) entry.ReferenceId = value
} default:
} return nil, fmt.Errorf("Unknown header \"%s\"", headers[j])
}
}
if busStops[ entry.StationId ] != nil { // Ignoring special route for now as getSchedule does not reflect
return nil, fmt.Errorf( "Duplicated entry %+v", entry ) // which bus is a special route
} if entry.ReferenceId != entry.RouteId {
log.Printf("Ignoring special Route: %s", entry.ReferenceId)
continue
}
routeDir, hasKey := routeStops[ entry.RouteId ] if busStops[entry.StationId] != nil {
if !hasKey { return nil, fmt.Errorf("Duplicated entry %+v", entry)
routeStops[ entry.RouteId ] = map[string] map[int] *BusStop{} }
routeDir = routeStops[ entry.RouteId ]
}
route, hasKey := routeDir[ entry.Direction ] routeDir, hasKey := routeStops[entry.RouteId]
if !hasKey { if !hasKey {
routeDir[ entry.Direction ] = map[int] *BusStop{} routeStops[entry.RouteId] = map[string]map[int]*BusStop{}
route = routeDir[ entry.Direction ] routeDir = routeStops[entry.RouteId]
} }
_, hasKey = route[ entry.StationSeq ] route, hasKey := routeDir[entry.Direction]
if !hasKey { if !hasKey {
route[ entry.StationSeq ] = &entry routeDir[entry.Direction] = map[int]*BusStop{}
} route = routeDir[entry.Direction]
}
entry.RouteStops = &route _, hasKey = route[entry.StationSeq]
entry.Reload() if !hasKey {
route[entry.StationSeq] = &entry
}
busStops[ entry.StationId ] = &entry entry.RouteStops = &route
} entry.Reload()
return &busStops, nil
busStops[entry.StationId] = &entry
}
return &busStops, nil
} }
func getBusStops() (*[] query.ISearchable, error) { func getBusStops() (*[]query.ISearchable, error) {
QUERY_FUNC := func() ( io.ReadCloser, error ) { QUERY_FUNC := func() (io.ReadCloser, error) {
resp, err := http.Get( "https://opendata.mtr.com.hk/data/mtr_bus_stops.csv" ) resp, err := http.Get("https://opendata.mtr.com.hk/data/mtr_bus_stops.csv")
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.Body, nil return resp.Body, nil
} }
buff, err := utils.CacheStream( CSV_BUSSTOPS, QUERY_FUNC, 7 * 24 * 3600 ) buff, err := utils.CacheStream(CSV_BUSSTOPS, QUERY_FUNC, 7*24*3600)
if err != nil { if err != nil {
return nil, err return nil, err
} }
utils.ReadBOM( buff ) utils.ReadBOM(buff)
busStopMap, err := readBusStopData( buff ) busStopMap, err := readBusStopData(buff)
if err != nil { if err != nil {
return nil, err return nil, err
} }
searchables := [] query.ISearchable{} searchables := []query.ISearchable{}
for _, busStop := range *busStopMap { for _, busStop := range *busStopMap {
searchables = append( searchables, busStop ) searchables = append(searchables, busStop)
} }
return &searchables, nil return &searchables, nil
} }