111 lines
3.0 KiB
Go
111 lines
3.0 KiB
Go
package bus
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/tgckpg/golifehk/utils"
|
|
)
|
|
|
|
var CSV_BUSSTOPS string = filepath.Join( utils.WORKDIR, "mtr_bus_stops.csv" )
|
|
|
|
func readBusStopData( r io.Reader ) ( *map[string]BusStop, error ) {
|
|
|
|
reader := csv.NewReader( r )
|
|
entries, err := reader.ReadAll()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
busStops := map[string]BusStop{}
|
|
routeStops := map[string] map[string] map[int] *BusStop{}
|
|
|
|
var headers []string
|
|
for i, line := range entries {
|
|
if i == 0 {
|
|
headers = line
|
|
line[0] = strings.TrimLeft( line[0], utils.BOM )
|
|
continue
|
|
}
|
|
|
|
var entry BusStop
|
|
i18n_Name := map[string] string{}
|
|
|
|
for j, value := range line {
|
|
switch headers[j] {
|
|
case "ROUTE_ID":
|
|
entry.RouteId = value
|
|
case "DIRECTION":
|
|
entry.Direction = value
|
|
case "STATION_SEQNO":
|
|
v, _ := strconv.Atoi( value )
|
|
entry.StationSeq = v
|
|
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":
|
|
i18n_Name["zh"] = value
|
|
case "STATION_NAME_ENG":
|
|
i18n_Name["en"] = value
|
|
default:
|
|
return nil, fmt.Errorf( "Unknown header \"%s\"", headers[j] )
|
|
}
|
|
}
|
|
|
|
if _, t := busStops[ entry.StationId ]; t {
|
|
return nil, fmt.Errorf( "Duplicated entry %+v", entry )
|
|
}
|
|
|
|
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.Name = &i18n_Name
|
|
entry.RouteStops = &route
|
|
|
|
busStops[ entry.StationId ] = entry
|
|
}
|
|
return &busStops, nil
|
|
}
|
|
|
|
func getBusStops() (*map[string]BusStop, error) {
|
|
|
|
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
|
|
}
|
|
|
|
buff, err := utils.CacheStream( CSV_BUSSTOPS, QUERY_FUNC, 7 * 24 * 3600 )
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return readBusStopData( buff )
|
|
}
|