feat: Add regex parsing
This commit is contained in:
parent
8277ade9c6
commit
1be274e62f
@ -3,8 +3,8 @@ package model
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type WeatherData struct {
|
||||
@ -37,32 +37,15 @@ type WeatherData struct {
|
||||
RTFreq int
|
||||
}
|
||||
|
||||
var urlRegex = regexp.MustCompile(`/weatherstation/updateweatherstation\.php\?([^&\s]+(&[^&\s]+)*)`)
|
||||
|
||||
func ParseWeatherData(data string) (*WeatherData, error) {
|
||||
if !strings.Contains(data, "/weatherstation/updateweatherstation.php") {
|
||||
return nil, fmt.Errorf("不是气象站数据")
|
||||
matches := urlRegex.FindStringSubmatch(data)
|
||||
if len(matches) < 2 {
|
||||
return nil, fmt.Errorf("无法找到有效的气象站数据URL")
|
||||
}
|
||||
|
||||
urlStart := strings.Index(data, "/weatherstation/updateweatherstation.php")
|
||||
if urlStart == -1 {
|
||||
return nil, fmt.Errorf("无法找到URL开始位置")
|
||||
}
|
||||
|
||||
queryStart := strings.Index(data[urlStart:], "?")
|
||||
if queryStart == -1 {
|
||||
return nil, fmt.Errorf("无法找到查询参数")
|
||||
}
|
||||
|
||||
fullPath := data[urlStart:]
|
||||
|
||||
queryEnd := len(fullPath)
|
||||
for i := queryStart; i < len(fullPath); i++ {
|
||||
if fullPath[i] == ' ' || fullPath[i] == '.' {
|
||||
queryEnd = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
queryString := fullPath[queryStart+1 : queryEnd]
|
||||
queryString := matches[1]
|
||||
|
||||
values, err := url.ParseQuery(queryString)
|
||||
if err != nil {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user