golifehk/datasources/kmb/routestops.go

148 lines
4.0 KiB
Go
Raw Permalink Normal View History

2022-09-16 20:33:47 +00:00
package kmb
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"path/filepath"
// "strings"
query "github.com/tgckpg/golifehk/query"
"github.com/tgckpg/golifehk/utils"
)
var JSON_ROUTESTOPS string = filepath.Join( utils.WORKDIR, "kmb-routestops.json" )
var JSON_BUSSTOPS string = filepath.Join( utils.WORKDIR, "kmb-busstops.json" )
2022-09-25 10:48:22 +00:00
func readRouteStopsData( busStops *map[string] *BusStop, buff *bytes.Buffer ) ( *[] *RouteStop, error ) {
2022-09-16 20:33:47 +00:00
routeStopsData := RouteStops{}
err := json.Unmarshal( buff.Bytes(), &routeStopsData )
if err != nil {
2022-09-25 10:48:22 +00:00
return nil, err
2022-09-16 20:33:47 +00:00
}
// routeStops[ Route ][ ServiceType ][ Direction ][ Seq ] = RouteStop
routeStops := map[string] *map[string] *map[ string ] *map[ int ] *RouteStop{}
2022-09-25 10:48:22 +00:00
allRouteStops := [] *RouteStop{}
2022-09-16 20:33:47 +00:00
for _, entry := range routeStopsData.RouteStops {
busStop := (*busStops)[ entry.StationId ]
if busStop == nil {
busStop = &BusStop {
BusStopId: entry.StationId,
Name_en: "???", Name_tc: "???", Name_sc: "???",
}
busStop.Reload()
(*busStops)[ entry.StationId ] = busStop
}
if busStop.Routes == nil {
busStopRoutes := [] *RouteStop{}
busStop.Routes = &busStopRoutes
}
(*busStop.Routes) = append( (*busStop.Routes), entry )
entry.BusStop = busStop
route := routeStops[ entry.RouteId ]
if route == nil {
route = &map[string] *map[ string ] *map[ int ] *RouteStop{}
routeStops[ entry.RouteId ] = route
}
service := (*route)[ entry.ServiceType ]
if service == nil {
service = &map[ string ] *map[ int ] *RouteStop{}
(*route)[ entry.ServiceType ] = service
}
direction := (*service)[ entry.Direction ]
if direction == nil {
direction = &map[ int ] *RouteStop{}
(*service)[ entry.Direction ] = direction
}
2022-09-25 10:48:22 +00:00
entry.RouteStops = direction
2022-09-16 20:33:47 +00:00
seq := (*direction)[ entry.StationSeq ]
if seq == nil {
(*direction)[ entry.StationSeq ] = entry
}
2022-09-25 10:48:22 +00:00
allRouteStops = append( allRouteStops, entry )
2022-09-16 20:33:47 +00:00
}
2022-09-25 10:48:22 +00:00
return &allRouteStops, nil
2022-09-16 20:33:47 +00:00
}
func readBusStopsData( buff *bytes.Buffer ) ( *map[string]*BusStop, error ) {
busStopsData := BusStops{}
err := json.Unmarshal( buff.Bytes(), &busStopsData )
if err != nil {
return nil, err
}
busStopMap := map[string] *BusStop{}
for _, entry := range busStopsData.BusStops {
entry.Reload()
if _, ok := busStopMap[ entry.BusStopId ]; ok {
return nil, fmt.Errorf( "Duplicated BusStop: %s", entry.BusStopId )
}
busStopMap[ entry.BusStopId ] = entry
}
return &busStopMap, nil
}
2022-09-25 10:48:22 +00:00
func getRouteStops() (*[] query.ISearchable, error) {
2022-09-16 20:33:47 +00:00
QUERY_FUNC := func() ( io.ReadCloser, error ) {
resp, err := http.Get( "https://data.etabus.gov.hk/v1/transport/kmb/stop" )
if err != nil {
return nil, err
}
return resp.Body, nil
}
buff, err := utils.CacheStream( JSON_BUSSTOPS, QUERY_FUNC, 7 * 24 * 3600 )
if err != nil {
return nil, err
}
busStopMap, err := readBusStopsData( buff )
if err != nil {
return nil, err
}
QUERY_FUNC = func() ( io.ReadCloser, error ) {
resp, err := http.Get( "https://data.etabus.gov.hk/v1/transport/kmb/route-stop" )
if err != nil {
return nil, err
}
return resp.Body, nil
}
buff, err = utils.CacheStream( JSON_ROUTESTOPS, QUERY_FUNC, 7 * 24 * 3600 )
if err != nil {
return nil, err
}
2022-09-25 10:48:22 +00:00
routeStops, err := readRouteStopsData( busStopMap, buff )
2022-09-16 20:33:47 +00:00
if err != nil {
return nil, err
}
searchables := [] query.ISearchable{}
2022-09-25 10:48:22 +00:00
for _, routeStop := range *routeStops {
searchables = append( searchables, routeStop )
routeStop.Reload()
2022-09-16 20:33:47 +00:00
}
return &searchables, nil
}