golifehk/datasources/mtr/bus/busstops.go

121 lines
3.2 KiB
Go
Raw Normal View History

2022-09-14 09:12:48 +00:00
package bus
2022-09-13 13:42:51 +00:00
import (
"encoding/csv"
"fmt"
"io"
"net/http"
"path/filepath"
"strconv"
2022-09-16 20:33:47 +00:00
query "github.com/tgckpg/golifehk/query"
2022-09-13 13:42:51 +00:00
"github.com/tgckpg/golifehk/utils"
)
2022-09-14 09:12:48 +00:00
var CSV_BUSSTOPS string = filepath.Join( utils.WORKDIR, "mtr_bus_stops.csv" )
2022-09-13 13:42:51 +00:00
2022-09-16 20:33:47 +00:00
func readBusStopData( r io.Reader ) ( *map[string] *BusStop, error ) {
2022-09-13 13:42:51 +00:00
reader := csv.NewReader( r )
entries, err := reader.ReadAll()
if err != nil {
return nil, err
}
2022-09-16 20:33:47 +00:00
busStops := map[string] *BusStop{}
2022-09-14 13:21:39 +00:00
routeStops := map[string] map[string] map[int] *BusStop{}
2022-09-13 13:42:51 +00:00
var headers []string
for i, line := range entries {
if i == 0 {
headers = line
continue
}
var entry BusStop
2022-09-14 13:21:39 +00:00
2022-09-13 13:42:51 +00:00
for j, value := range line {
switch headers[j] {
case "ROUTE_ID":
entry.RouteId = value
case "DIRECTION":
entry.Direction = value
case "STATION_SEQNO":
2023-06-29 10:45:46 +00:00
v, _ := strconv.ParseFloat( value, 64 )
entry.StationSeq = int( v )
2022-09-13 13:42:51 +00:00
case "STATION_ID":
entry.StationId = value
case "STATION_LATITUDE":
v, _ := strconv.ParseFloat( value, 64 )
entry.Latitude = v
case "STATION_LONGITUDE":
v, _ := strconv.ParseFloat( value, 64 )
entry.Longtitude = v
case "STATION_NAME_CHI":
2022-09-16 20:33:47 +00:00
entry.Name_zh = value
2022-09-13 13:42:51 +00:00
case "STATION_NAME_ENG":
2022-09-16 20:33:47 +00:00
entry.Name_en = value
2022-09-13 13:42:51 +00:00
default:
return nil, fmt.Errorf( "Unknown header \"%s\"", headers[j] )
}
}
2022-09-16 20:33:47 +00:00
if busStops[ entry.StationId ] != nil {
2022-09-13 13:42:51 +00:00
return nil, fmt.Errorf( "Duplicated entry %+v", entry )
}
2022-09-14 13:21:39 +00:00
routeDir, hasKey := routeStops[ entry.RouteId ]
if !hasKey {
routeStops[ entry.RouteId ] = map[string] map[int] *BusStop{}
routeDir = routeStops[ entry.RouteId ]
}
route, hasKey := routeDir[ entry.Direction ]
if !hasKey {
routeDir[ entry.Direction ] = map[int] *BusStop{}
route = routeDir[ entry.Direction ]
}
_, hasKey = route[ entry.StationSeq ]
if !hasKey {
route[ entry.StationSeq ] = &entry
}
entry.RouteStops = &route
2022-09-16 20:33:47 +00:00
entry.Reload()
2022-09-14 13:21:39 +00:00
2022-09-16 20:33:47 +00:00
busStops[ entry.StationId ] = &entry
2022-09-13 13:42:51 +00:00
}
return &busStops, nil
}
2022-09-16 20:33:47 +00:00
func getBusStops() (*[] query.ISearchable, error) {
2022-09-13 13:42:51 +00:00
2022-09-14 09:12:48 +00:00
QUERY_FUNC := func() ( io.ReadCloser, error ) {
resp, err := http.Get( "https://opendata.mtr.com.hk/data/mtr_bus_stops.csv" )
if err != nil {
return nil, err
}
return resp.Body, nil
2022-09-13 13:42:51 +00:00
}
2022-09-14 09:12:48 +00:00
buff, err := utils.CacheStream( CSV_BUSSTOPS, QUERY_FUNC, 7 * 24 * 3600 )
2022-09-13 13:42:51 +00:00
if err != nil {
return nil, err
}
2023-06-29 10:45:46 +00:00
utils.ReadBOM( buff )
2022-09-16 20:33:47 +00:00
busStopMap, err := readBusStopData( buff )
if err != nil {
return nil, err
}
searchables := [] query.ISearchable{}
for _, busStop := range *busStopMap {
searchables = append( searchables, busStop )
}
return &searchables, nil
2022-09-13 13:42:51 +00:00
}