Fixed mtr new csv format

This commit is contained in:
斟酌 鵬兄 2023-06-29 18:45:46 +08:00
parent 726916ed45
commit e4275f60b7
4 changed files with 37 additions and 6 deletions

View File

@ -87,7 +87,7 @@ func ( this QueryResult ) Message() ( string, error ) {
for _, d := range keys { for _, d := range keys {
b := st[ d ] b := st[ d ]
sb.WriteString( q.Key ) utils.WriteMDv2Text( &sb, q.Key )
if d == "O" { if d == "O" {
sb.WriteString( "↑" ) sb.WriteString( "↑" )

View File

@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
query "github.com/tgckpg/golifehk/query" query "github.com/tgckpg/golifehk/query"
"github.com/tgckpg/golifehk/utils" "github.com/tgckpg/golifehk/utils"
@ -30,7 +29,6 @@ func readBusStopData( r io.Reader ) ( *map[string] *BusStop, error ) {
for i, line := range entries { for i, line := range entries {
if i == 0 { if i == 0 {
headers = line headers = line
line[0] = strings.TrimLeft( line[0], utils.BOM )
continue continue
} }
@ -43,8 +41,8 @@ func readBusStopData( r io.Reader ) ( *map[string] *BusStop, error ) {
case "DIRECTION": case "DIRECTION":
entry.Direction = value entry.Direction = value
case "STATION_SEQNO": case "STATION_SEQNO":
v, _ := strconv.Atoi( value ) v, _ := strconv.ParseFloat( value, 64 )
entry.StationSeq = v entry.StationSeq = int( v )
case "STATION_ID": case "STATION_ID":
entry.StationId = value entry.StationId = value
case "STATION_LATITUDE": case "STATION_LATITUDE":
@ -106,6 +104,7 @@ func getBusStops() (*[] query.ISearchable, error) {
return nil, err return nil, err
} }
utils.ReadBOM( buff )
busStopMap, err := readBusStopData( buff ) busStopMap, err := readBusStopData( buff )
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -71,7 +71,7 @@ func main() {
} }
if !isGroup && !f_sent && f_err != nil { 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 ) botsend( bot, &update, &mesg )
} }
} }

View File

@ -1,6 +1,7 @@
package utils package utils
import ( import (
"bytes"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -16,6 +17,17 @@ func WriteMDv2Text( sb *strings.Builder, t string ) {
sb.WriteString( t ) 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 { func ToPower( t string ) string {
for s, r := range POWER_NUMBERS { for s, r := range POWER_NUMBERS {
t = strings.ReplaceAll( t, s, r ) t = strings.ReplaceAll( t, s, r )
@ -23,6 +35,26 @@ func ToPower( t string ) string {
return t 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 { func TryGetEnv[T any]( name string, fallback T ) T {
v := os.Getenv( name ) v := os.Getenv( name )