79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
|
package mtr
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
// "byte"
|
||
|
// "net/http"
|
||
|
"io"
|
||
|
"os"
|
||
|
"encoding/json"
|
||
|
)
|
||
|
|
||
|
type Location struct {
|
||
|
latitude float32
|
||
|
longitude float32
|
||
|
}
|
||
|
|
||
|
type Bus struct {
|
||
|
ETA int `json:"arrivalTimeInSecond,string"`
|
||
|
ETAText string `json:"arrivalTimeText"`
|
||
|
BusId string `json:"busId"`
|
||
|
BusLocation Location `json:"busLocation"`
|
||
|
ETD string `json:"departureTimeInSecond"`
|
||
|
ETDText string `json:"departureTimeText"`
|
||
|
Delayed string `json:"isDelayed"`
|
||
|
Scheduled string `json:"isScheduled"`
|
||
|
LineRef string `json:"lineRef"`
|
||
|
}
|
||
|
|
||
|
type BusStopSchedules struct {
|
||
|
Buses [] Bus `json:"bus"`
|
||
|
Suspended string `json:"isSuspended"`
|
||
|
}
|
||
|
|
||
|
type BusSchedule struct {
|
||
|
RefreshTime int `json:"appRefreshTimeInSecond,string"`
|
||
|
BusStops [] BusStopSchedules `json:"busStop"`
|
||
|
}
|
||
|
|
||
|
func getSchedule( lang string, routeName string ) ( *BusSchedule, error ) {
|
||
|
|
||
|
/*
|
||
|
values := map[string]string { "language": lang , "routeName": routeName }
|
||
|
|
||
|
jsonValue, _ := json.Marshal(values)
|
||
|
|
||
|
resp, err := http.Post(
|
||
|
"https://rt.data.gov.hk/v1/transport/mtr/bus/getSchedule",
|
||
|
"application/json",
|
||
|
bytes.NewBuffer( jsonValue ),
|
||
|
)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
data, err := io.ReadAll( resp.Body )
|
||
|
/*/
|
||
|
f, err := os.Open( "abc.json" )
|
||
|
defer f.Close()
|
||
|
|
||
|
data, err := io.ReadAll( f )
|
||
|
//*/
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
schedules := BusSchedule{}
|
||
|
|
||
|
err = json.Unmarshal( data, &schedules )
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
fmt.Printf( "%+v", schedules )
|
||
|
return &schedules, nil
|
||
|
}
|