feat: Add regex parsing

This commit is contained in:
yarnom 2025-07-25 18:39:59 +08:00
parent 8277ade9c6
commit 1be274e62f

View File

@ -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 {