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