Added kmb and refactored query.Parse
This commit is contained in:
@@ -9,12 +9,13 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
query "github.com/tgckpg/golifehk/query"
|
||||
"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 ) {
|
||||
func readBusStopData( r io.Reader ) ( *map[string] *BusStop, error ) {
|
||||
|
||||
reader := csv.NewReader( r )
|
||||
entries, err := reader.ReadAll()
|
||||
@@ -22,7 +23,7 @@ func readBusStopData( r io.Reader ) ( *map[string]BusStop, error ) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
busStops := map[string]BusStop{}
|
||||
busStops := map[string] *BusStop{}
|
||||
routeStops := map[string] map[string] map[int] *BusStop{}
|
||||
|
||||
var headers []string
|
||||
@@ -34,7 +35,6 @@ func readBusStopData( r io.Reader ) ( *map[string]BusStop, error ) {
|
||||
}
|
||||
|
||||
var entry BusStop
|
||||
i18n_Name := map[string] string{}
|
||||
|
||||
for j, value := range line {
|
||||
switch headers[j] {
|
||||
@@ -54,15 +54,15 @@ func readBusStopData( r io.Reader ) ( *map[string]BusStop, error ) {
|
||||
v, _ := strconv.ParseFloat( value, 64 )
|
||||
entry.Longtitude = v
|
||||
case "STATION_NAME_CHI":
|
||||
i18n_Name["zh"] = value
|
||||
entry.Name_zh = value
|
||||
case "STATION_NAME_ENG":
|
||||
i18n_Name["en"] = value
|
||||
entry.Name_en = value
|
||||
default:
|
||||
return nil, fmt.Errorf( "Unknown header \"%s\"", headers[j] )
|
||||
}
|
||||
}
|
||||
|
||||
if _, t := busStops[ entry.StationId ]; t {
|
||||
if busStops[ entry.StationId ] != nil {
|
||||
return nil, fmt.Errorf( "Duplicated entry %+v", entry )
|
||||
}
|
||||
|
||||
@@ -83,15 +83,15 @@ func readBusStopData( r io.Reader ) ( *map[string]BusStop, error ) {
|
||||
route[ entry.StationSeq ] = &entry
|
||||
}
|
||||
|
||||
entry.Name = &i18n_Name
|
||||
entry.RouteStops = &route
|
||||
entry.Reload()
|
||||
|
||||
busStops[ entry.StationId ] = entry
|
||||
busStops[ entry.StationId ] = &entry
|
||||
}
|
||||
return &busStops, nil
|
||||
}
|
||||
|
||||
func getBusStops() (*map[string]BusStop, error) {
|
||||
func getBusStops() (*[] query.ISearchable, error) {
|
||||
|
||||
QUERY_FUNC := func() ( io.ReadCloser, error ) {
|
||||
resp, err := http.Get( "https://opendata.mtr.com.hk/data/mtr_bus_stops.csv" )
|
||||
@@ -106,5 +106,16 @@ func getBusStops() (*map[string]BusStop, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return readBusStopData( buff )
|
||||
busStopMap, err := readBusStopData( buff )
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
searchables := [] query.ISearchable{}
|
||||
for _, busStop := range *busStopMap {
|
||||
searchables = append( searchables, busStop )
|
||||
}
|
||||
|
||||
return &searchables, nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user