commit e4275f60b76038cf7d7bfbcb9f33df8f9d346ac2
| author | 斟酌 鵬兄 <tgckpg@gmail.com> |
| date | 2023-06-29T10:45:46Z |
| subject | Fixed mtr new csv format |
commit e4275f60b76038cf7d7bfbcb9f33df8f9d346ac2
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date: 2023-06-29T10:45:46Z
Fixed mtr new csv format
---
datasources/mtr/bus/QueryResult.go | 2 +-
datasources/mtr/bus/busstops.go | 7 +++----
main.go | 2 +-
utils/shortcuts.go | 32 ++++++++++++++++++++++++++++++++
4 files changed, 37 insertions(+), 6 deletions(-)
diff --git a/datasources/mtr/bus/QueryResult.go b/datasources/mtr/bus/QueryResult.go
index fa58ed4..ebe9859 100644
--- a/datasources/mtr/bus/QueryResult.go
+++ b/datasources/mtr/bus/QueryResult.go
@@ -87,7 +87,7 @@ func ( this QueryResult ) Message() ( string, error ) {
for _, d := range keys {
b := st[ d ]
- sb.WriteString( q.Key )
+ utils.WriteMDv2Text( &sb, q.Key )
if d == "O" {
sb.WriteString( "↑" )
diff --git a/datasources/mtr/bus/busstops.go b/datasources/mtr/bus/busstops.go
index 8b642ed..f3fa7d8 100644
--- a/datasources/mtr/bus/busstops.go
+++ b/datasources/mtr/bus/busstops.go
@@ -7,7 +7,6 @@ import (
"net/http"
"path/filepath"
"strconv"
- "strings"
query "github.com/tgckpg/golifehk/query"
"github.com/tgckpg/golifehk/utils"
@@ -30,7 +29,6 @@ func readBusStopData( r io.Reader ) ( *map[string] *BusStop, error ) {
for i, line := range entries {
if i == 0 {
headers = line
- line[0] = strings.TrimLeft( line[0], utils.BOM )
continue
}
@@ -43,8 +41,8 @@ func readBusStopData( r io.Reader ) ( *map[string] *BusStop, error ) {
case "DIRECTION":
entry.Direction = value
case "STATION_SEQNO":
- v, _ := strconv.Atoi( value )
- entry.StationSeq = v
+ v, _ := strconv.ParseFloat( value, 64 )
+ entry.StationSeq = int( v )
case "STATION_ID":
entry.StationId = value
case "STATION_LATITUDE":
@@ -106,6 +104,7 @@ func getBusStops() (*[] query.ISearchable, error) {
return nil, err
}
+ utils.ReadBOM( buff )
busStopMap, err := readBusStopData( buff )
if err != nil {
return nil, err
diff --git a/main.go b/main.go
index 525c91d..928c1e6 100644
--- a/main.go
+++ b/main.go
@@ -71,7 +71,7 @@ func main() {
}
if !isGroup && !f_sent && f_err != nil {
- mesg := fmt.Sprintf( "%s", f_err )
+ mesg := utils.MDv2Text( fmt.Sprintf( "%s", f_err ) )
botsend( bot, &update, &mesg )
}
}
diff --git a/utils/shortcuts.go b/utils/shortcuts.go
index bb9b23b..07c8dc8 100644
--- a/utils/shortcuts.go
+++ b/utils/shortcuts.go
@@ -1,6 +1,7 @@
package utils
import (
+ "bytes"
"os"
"strconv"
"strings"
@@ -16,6 +17,17 @@ func WriteMDv2Text( sb *strings.Builder, t string ) {
sb.WriteString( t )
}
+func MDv2Text( t string ) string {
+ sb := strings.Builder{}
+
+ for _, c := range MARKDOWN_ESC {
+ t = strings.Replace( t, c, "\\" + c, -1 )
+ }
+
+ sb.WriteString( t )
+ return sb.String()
+}
+
func ToPower( t string ) string {
for s, r := range POWER_NUMBERS {
t = strings.ReplaceAll( t, s, r )
@@ -23,6 +35,26 @@ func ToPower( t string ) string {
return t
}
+func ReadBOM( buff *bytes.Buffer ) {
+ b, _ := buff.ReadByte()
+ if b != 0xef {
+ buff.UnreadByte()
+ return
+ }
+ b, _ = buff.ReadByte()
+ if b != 0xbb {
+ buff.UnreadByte()
+ buff.UnreadByte()
+ return
+ }
+ b, _ = buff.ReadByte()
+ if b != 0xbf {
+ buff.UnreadByte()
+ buff.UnreadByte()
+ buff.UnreadByte()
+ }
+}
+
func TryGetEnv[T any]( name string, fallback T ) T {
v := os.Getenv( name )