Can now accept location
This commit is contained in:
@@ -40,9 +40,9 @@ func writeCCharInfo(sb *strings.Builder, cc *CChar) {
|
||||
}
|
||||
}
|
||||
|
||||
func (this QueryResult) DataType() string { return this.ResultType }
|
||||
func (this QueryResult) Consumed() bool { return this.isConsumed }
|
||||
func (this QueryResult) GetTableData() [][]map[string]string { return nil }
|
||||
func (this QueryResult) DataType() string { return this.ResultType }
|
||||
func (this QueryResult) Consumed() bool { return this.isConsumed }
|
||||
func (this QueryResult) GetTableData() [][]query.TableCell { return nil }
|
||||
|
||||
func (this QueryResult) Message() (string, error) {
|
||||
|
||||
|
||||
@@ -1,40 +1,52 @@
|
||||
package kmb
|
||||
|
||||
import (
|
||||
i18n "github.com/tgckpg/golifehk/i18n"
|
||||
i18n "github.com/tgckpg/golifehk/i18n"
|
||||
query "github.com/tgckpg/golifehk/query"
|
||||
)
|
||||
|
||||
type BusStopJson struct {
|
||||
BusStopId string `json:"stop"`
|
||||
Latitude float64 `json:"lat,string"`
|
||||
Longitude float64 `json:"long,string"`
|
||||
Name_en string `json:"name_en"`
|
||||
Name_tc string `json:"name_tc"`
|
||||
Name_sc string `json:"name_sc"`
|
||||
}
|
||||
|
||||
type BusStop struct {
|
||||
BusStopId string `json:"stop"`
|
||||
Latitude float64 `json:"lat,string"`
|
||||
Longtitude float64 `json:"long,string"`
|
||||
Name_en string `json:"name_en"`
|
||||
Name_tc string `json:"name_tc"`
|
||||
Name_sc string `json:"name_sc"`
|
||||
BusStopId string
|
||||
|
||||
// Routes[ Route ][ Direction ]
|
||||
Routes *[] *RouteStop
|
||||
// Routes[ Route ][ Direction ]
|
||||
Routes *[]*RouteStop
|
||||
|
||||
i18n.Generics
|
||||
i18n.Generics
|
||||
query.GeoLocation
|
||||
}
|
||||
|
||||
type BusStops struct {
|
||||
Type string `json:"type"`
|
||||
Version string `json:"version"`
|
||||
DateCreated string `json:"generated_timestamp"`
|
||||
BusStops [] *BusStop `json:"data"`
|
||||
type BusStopsJson struct {
|
||||
Type string `json:"type"`
|
||||
Version string `json:"version"`
|
||||
DateCreated string `json:"generated_timestamp"`
|
||||
BusStops []*BusStopJson `json:"data"`
|
||||
}
|
||||
|
||||
func ( this *BusStop ) Reload() {
|
||||
i18n_Name := map[string] string{}
|
||||
i18n_Name["en"] = this.Name_en
|
||||
i18n_Name["zh-Hant"] = this.Name_tc
|
||||
|
||||
searchData := [] *string{}
|
||||
searchData = append( searchData, &this.Name_en )
|
||||
searchData = append( searchData, &this.Name_tc )
|
||||
|
||||
this.Name = &i18n_Name
|
||||
this.Key = &this.BusStopId
|
||||
this.SearchData = &searchData
|
||||
func (b BusStop) Register(registers map[string]struct{}) bool {
|
||||
if _, ok := registers[b.BusStopId]; ok {
|
||||
return false
|
||||
}
|
||||
registers[b.BusStopId] = struct{}{}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *BusStop) Reload() {
|
||||
searchData := []*string{}
|
||||
searchData = append(searchData, &this.BusStopId)
|
||||
|
||||
for _, v := range *this.Name {
|
||||
searchData = append(searchData, &v)
|
||||
}
|
||||
|
||||
this.Key = &this.BusStopId
|
||||
this.SearchData = &searchData
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ type QueryResult struct {
|
||||
Query *query.QueryObject
|
||||
|
||||
isConsumed bool
|
||||
dataType string
|
||||
tableData [][]query.TableCell
|
||||
}
|
||||
|
||||
func writeRouteHead(sb *strings.Builder, r *RouteStop) {
|
||||
@@ -50,12 +52,14 @@ func writeShortRoute(lang *string, sb *strings.Builder, r *RouteStop) {
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
func (this QueryResult) DataType() string { return "MarkdownV2" }
|
||||
func (this QueryResult) Consumed() bool { return this.isConsumed }
|
||||
func (this QueryResult) GetTableData() [][]map[string]string { return nil }
|
||||
func (this QueryResult) DataType() string { return this.dataType }
|
||||
func (this QueryResult) Consumed() bool { return this.isConsumed }
|
||||
func (this QueryResult) GetTableData() [][]query.TableCell { return this.tableData }
|
||||
|
||||
func (this *QueryResult) Message() (string, error) {
|
||||
|
||||
this.dataType = "PlainText"
|
||||
|
||||
if this.Error != nil {
|
||||
return "", this.Error
|
||||
}
|
||||
@@ -66,27 +70,61 @@ func (this *QueryResult) Message() (string, error) {
|
||||
|
||||
// Print Stop Names, then print the list of routes
|
||||
if this.Query.Key == "" {
|
||||
busStops := map[string]*BusStop{}
|
||||
for _, item := range *this.Query.Results {
|
||||
var r *RouteStop
|
||||
r = any(item).(*RouteStop)
|
||||
loc := this.Query.Message.Location
|
||||
if loc != nil {
|
||||
sb.WriteString("九巴 100m")
|
||||
this.dataType = "Table"
|
||||
|
||||
b := r.BusStop
|
||||
if b.Routes == nil {
|
||||
continue
|
||||
table := [][]query.TableCell{}
|
||||
|
||||
for _, item := range *this.Query.Results {
|
||||
b := any(item).(*BusStop)
|
||||
|
||||
row := []query.TableCell{}
|
||||
|
||||
cell := query.TableCell{
|
||||
Name: fmt.Sprintf("%.2fm %s", b.Dist(loc.Lat(), loc.Lon()), (*b.Name)[this.Lang]),
|
||||
Value: fmt.Sprintf("%s", b.BusStopId),
|
||||
}
|
||||
row = append(row, cell)
|
||||
|
||||
for _, r := range *b.Routes {
|
||||
sb_i := strings.Builder{}
|
||||
writeRouteHead(&sb_i, r)
|
||||
cell := query.TableCell{
|
||||
Name: sb_i.String(),
|
||||
Value: fmt.Sprintf("%s %s", r.RouteId, (*b.Name)[this.Lang]),
|
||||
}
|
||||
row = append(row, cell)
|
||||
}
|
||||
|
||||
table = append(table, row)
|
||||
}
|
||||
this.tableData = table
|
||||
|
||||
} else {
|
||||
busStops := map[string]*BusStop{}
|
||||
for _, item := range *this.Query.Results {
|
||||
var r *RouteStop
|
||||
r = any(item).(*RouteStop)
|
||||
|
||||
b := r.BusStop
|
||||
if b.Routes == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
busStops[b.BusStopId] = b
|
||||
}
|
||||
|
||||
busStops[b.BusStopId] = b
|
||||
}
|
||||
|
||||
for _, b := range busStops {
|
||||
utils.WriteMDv2Text(&sb, (*b.Name)[this.Lang])
|
||||
sb.WriteString("\n ")
|
||||
for _, route := range *b.Routes {
|
||||
writeRouteHead(&sb, route)
|
||||
sb.WriteString(" ")
|
||||
for _, b := range busStops {
|
||||
utils.WriteMDv2Text(&sb, (*b.Name)[this.Lang])
|
||||
sb.WriteString("\n ")
|
||||
for _, route := range *b.Routes {
|
||||
writeRouteHead(&sb, route)
|
||||
sb.WriteString(" ")
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
// We got a route key
|
||||
|
||||
@@ -40,13 +40,8 @@ func (routeStop RouteStop) NextStop() *RouteStop {
|
||||
|
||||
func (this *RouteStop) Reload() {
|
||||
|
||||
searchData := []*string{}
|
||||
busStop := *this.BusStop
|
||||
searchData = append(searchData, &busStop.Name_en)
|
||||
searchData = append(searchData, &busStop.Name_tc)
|
||||
|
||||
this.Key = &this.RouteId
|
||||
this.SearchData = &searchData
|
||||
this.SearchData = this.BusStop.SearchData
|
||||
}
|
||||
|
||||
type ByRoute []*RouteStop
|
||||
|
||||
@@ -9,19 +9,39 @@ import (
|
||||
func Query(q query.QueryMessage) query.IQueryResult {
|
||||
|
||||
lang := q.Lang
|
||||
message := q.Text
|
||||
|
||||
var qo *query.QueryObject
|
||||
var err error
|
||||
var routeStops *[]query.ISearchable
|
||||
|
||||
qr := QueryResult{Lang: lang}
|
||||
routeStops, err := getRouteStops()
|
||||
|
||||
busStops, err := readBusStopsData()
|
||||
if err != nil {
|
||||
qr.Error = err
|
||||
goto qrReturn
|
||||
}
|
||||
|
||||
qo, err = query.MatchKeys(strings.ToUpper(message), routeStops)
|
||||
routeStops, err = getRouteStops(busStops)
|
||||
if err != nil {
|
||||
qr.Error = err
|
||||
goto qrReturn
|
||||
}
|
||||
|
||||
if q.Text != "" {
|
||||
qo, err = query.MatchKeys(strings.ToUpper(q.Text), routeStops)
|
||||
} else if q.Location != nil {
|
||||
bList := []query.ISearchable{}
|
||||
|
||||
for _, b := range *busStops {
|
||||
bList = append(bList, b)
|
||||
}
|
||||
|
||||
qo, err = query.MatchNearest(*q.Location, &bList, 100, 3)
|
||||
}
|
||||
|
||||
qo.Message = &q
|
||||
|
||||
if err != nil {
|
||||
qr.Error = err
|
||||
goto qrReturn
|
||||
|
||||
@@ -32,7 +32,6 @@ func readRouteStopsData(busStops *map[string]*BusStop, buff *bytes.Buffer) (*[]*
|
||||
if busStop == nil {
|
||||
busStop = &BusStop{
|
||||
BusStopId: entry.StationId,
|
||||
Name_en: "???", Name_tc: "???", Name_sc: "???",
|
||||
}
|
||||
busStop.Reload()
|
||||
|
||||
@@ -76,37 +75,15 @@ func readRouteStopsData(busStops *map[string]*BusStop, buff *bytes.Buffer) (*[]*
|
||||
return &allRouteStops, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func getRouteStops() (*[]query.ISearchable, error) {
|
||||
var busStopMap *map[string]*BusStop
|
||||
func readBusStopsData() (*map[string]*BusStop, error) {
|
||||
busStopsData := BusStopsJson{}
|
||||
|
||||
QUERY_FUNC := func() (io.ReadCloser, error) {
|
||||
return utils.HttpGet("https://data.etabus.gov.hk/v1/transport/kmb/stop")
|
||||
}
|
||||
|
||||
PARSE_FUNC := func(buff *bytes.Buffer) error {
|
||||
var err error
|
||||
busStopMap, err = readBusStopsData(buff)
|
||||
err := json.Unmarshal(buff.Bytes(), &busStopsData)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -120,18 +97,49 @@ func getRouteStops() (*[]query.ISearchable, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
busStopMap := map[string]*BusStop{}
|
||||
for _, entry := range busStopsData.BusStops {
|
||||
|
||||
b := BusStop{
|
||||
BusStopId: entry.BusStopId,
|
||||
}
|
||||
|
||||
b.Latitude = entry.Latitude
|
||||
b.Longitude = entry.Longitude
|
||||
|
||||
n := map[string]string{
|
||||
"en": entry.Name_en,
|
||||
"zh-Hant": entry.Name_tc,
|
||||
"zh-Hans": entry.Name_sc,
|
||||
}
|
||||
|
||||
b.Name = &n
|
||||
|
||||
b.Reload()
|
||||
|
||||
if _, ok := busStopMap[b.BusStopId]; ok {
|
||||
return nil, fmt.Errorf("Duplicated BusStop: %s", b.BusStopId)
|
||||
}
|
||||
|
||||
busStopMap[b.BusStopId] = &b
|
||||
}
|
||||
return &busStopMap, nil
|
||||
}
|
||||
|
||||
func getRouteStops(busStopMap *map[string]*BusStop) (*[]query.ISearchable, error) {
|
||||
var routeStops *[]*RouteStop
|
||||
QUERY_FUNC = func() (io.ReadCloser, error) {
|
||||
|
||||
QUERY_FUNC := func() (io.ReadCloser, error) {
|
||||
return utils.HttpGet("https://data.etabus.gov.hk/v1/transport/kmb/route-stop")
|
||||
}
|
||||
|
||||
PARSE_FUNC = func(buff *bytes.Buffer) error {
|
||||
PARSE_FUNC := func(buff *bytes.Buffer) error {
|
||||
var err error
|
||||
routeStops, err = readRouteStopsData(busStopMap, buff)
|
||||
return err
|
||||
}
|
||||
|
||||
cs, err = utils.CacheStreamEx(JSON_ROUTESTOPS, QUERY_FUNC)
|
||||
cs, err := utils.CacheStreamEx(JSON_ROUTESTOPS, QUERY_FUNC)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ type QueryResult struct {
|
||||
Query *query.QueryObject
|
||||
|
||||
isConsumed bool
|
||||
dataType string
|
||||
tableData [][]query.TableCell
|
||||
}
|
||||
|
||||
func writeShortRoute(lang *string, sb *strings.Builder, b *BusStop) {
|
||||
@@ -38,11 +40,13 @@ func writeShortRoute(lang *string, sb *strings.Builder, b *BusStop) {
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
func (this QueryResult) DataType() string { return "MarkdownV2" }
|
||||
func (this QueryResult) Consumed() bool { return this.isConsumed }
|
||||
func (this QueryResult) GetTableData() [][]map[string]string { return nil }
|
||||
func (this QueryResult) DataType() string { return this.dataType }
|
||||
func (this QueryResult) Consumed() bool { return this.isConsumed }
|
||||
func (this QueryResult) GetTableData() [][]query.TableCell { return this.tableData }
|
||||
|
||||
func (this QueryResult) Message() (string, error) {
|
||||
func (this *QueryResult) Message() (string, error) {
|
||||
|
||||
this.dataType = "PlainText"
|
||||
|
||||
if this.Error != nil {
|
||||
return "", this.Error
|
||||
@@ -67,29 +71,40 @@ func (this QueryResult) Message() (string, error) {
|
||||
loc := q.Message.Location
|
||||
if loc != nil {
|
||||
|
||||
// Print nearest bus stops
|
||||
this.dataType = "Table"
|
||||
sb.WriteString("K巴 100m")
|
||||
|
||||
table := [][]query.TableCell{}
|
||||
|
||||
for _, entry := range *q.Results {
|
||||
busStop := any(entry).(*BusStop)
|
||||
|
||||
utils.WriteMDv2Text(&sb, fmt.Sprintf("%.2fm", busStop.Dist(loc.Lat(), loc.Lon())))
|
||||
sb.WriteString(" ")
|
||||
sb.WriteString(" [")
|
||||
utils.WriteMDv2Text(&sb, busStop.RouteId)
|
||||
sb_i := strings.Builder{}
|
||||
sb_i.WriteString(fmt.Sprintf("%.2fm", busStop.Dist(loc.Lat(), loc.Lon())))
|
||||
sb_i.WriteString(" ")
|
||||
utils.WriteMDv2Text(&sb_i, busStop.RouteId)
|
||||
d := busStop.Direction
|
||||
if d == "O" {
|
||||
sb.WriteString("↑")
|
||||
sb_i.WriteString("↑")
|
||||
} else if d == "I" {
|
||||
sb.WriteString("↓")
|
||||
sb_i.WriteString("↓")
|
||||
} else {
|
||||
sb.WriteString("\\?")
|
||||
sb_i.WriteString("\\?")
|
||||
}
|
||||
utils.WriteMDv2Text(&sb, (*busStop.Name)[this.Lang])
|
||||
utils.WriteMDv2Text(&sb, busStop.RouteId)
|
||||
utils.WriteMDv2Text(&sb, (*busStop.Name)[this.Lang])
|
||||
sb.WriteString(")")
|
||||
sb.WriteString("\n")
|
||||
sb_i.WriteString(" ")
|
||||
utils.WriteMDv2Text(&sb_i, (*busStop.Name)[this.Lang])
|
||||
|
||||
row := []query.TableCell{
|
||||
query.TableCell{
|
||||
Name: sb_i.String(),
|
||||
Value: fmt.Sprintf("%s %s", busStop.RouteId, (*busStop.Name)[this.Lang]),
|
||||
},
|
||||
}
|
||||
|
||||
table = append(table, row)
|
||||
}
|
||||
|
||||
this.tableData = table
|
||||
} else {
|
||||
sort.Sort(query.ByKey(*q.Results))
|
||||
for _, entry := range *q.Results {
|
||||
|
||||
Reference in New Issue
Block a user