Skip to content

Commit 11625ac

Browse files
committed
feat: update SCEA datasource to Wolfx API
1 parent dd22c16 commit 11625ac

1 file changed

Lines changed: 42 additions & 78 deletions

File tree

pkg/seisevent/scea_impl.go

Lines changed: 42 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package seisevent
22

33
import (
4-
"crypto/md5"
5-
"encoding/hex"
64
"encoding/json"
75
"errors"
86
"fmt"
9-
"net/url"
10-
"sort"
11-
"strconv"
127
"time"
138

149
"github.com/anyshake/observer/pkg/cache"
@@ -20,6 +15,7 @@ import (
2015
)
2116

2217
const SCEA_ID = "scea"
18+
const SCEAListURL = "https://api.wolfx.jp/sc_eew_list.json"
2319

2420
type SCEA struct {
2521
travelTimeTable *travel.AK135
@@ -28,65 +24,35 @@ type SCEA struct {
2824
timeSource *timesource.Source
2925
}
3026

27+
type sceaListResponse struct {
28+
Code int `json:"code"`
29+
Msg string `json:"msg"`
30+
Data []sceaEvent `json:"data"`
31+
}
32+
33+
type sceaEvent struct {
34+
EventID string `json:"eventId"`
35+
ShockTime int64 `json:"shockTime"`
36+
Longitude float64 `json:"longitude"`
37+
Latitude float64 `json:"latitude"`
38+
PlaceName string `json:"placeName"`
39+
Magnitude float64 `json:"magnitude"`
40+
Depth *float64 `json:"depth"`
41+
InfoTypeName string `json:"infoTypeName"`
42+
}
43+
3144
func (s *SCEA) GetProperty() DataSourceProperty {
3245
return DataSourceProperty{
3346
ID: SCEA_ID,
3447
Country: "CN",
3548
Default: "en-US",
3649
Locales: map[string]string{
37-
"en-US": "Sichuan Earthquake Administration (Early Warning)",
38-
"zh-TW": "四川地震局預警",
50+
"en-US": "Sichuan Earthquake Administration Early Warning (Wolfx)",
51+
"zh-TW": "四川地震局預警(Wolfx)",
3952
},
4053
}
4154
}
4255

43-
func (s *SCEA) buildUrlSign(params map[string]string) string {
44-
const (
45-
SIGN_TOKEN = "OMEoiAuaExMuTjpovqKrhYDkZMkUaoCE"
46-
SLAT = "earthquake_app"
47-
)
48-
49-
keys := make([]string, 0, len(params))
50-
for k := range params {
51-
if k != "sign" && params[k] != "" {
52-
keys = append(keys, k)
53-
}
54-
}
55-
sort.Strings(keys)
56-
57-
raw := ""
58-
for i, k := range keys {
59-
if i > 0 {
60-
raw += "&"
61-
}
62-
raw += k + "=" + params[k]
63-
}
64-
raw += "&token=" + SIGN_TOKEN
65-
66-
sum := md5.Sum([]byte(raw + SLAT))
67-
return hex.EncodeToString(sum[:])
68-
}
69-
70-
func (s *SCEA) getRequestUrl(lat, lon float64) string {
71-
params := map[string]string{
72-
"pageNo": "1",
73-
"pageSize": "50",
74-
"orderType": "1",
75-
"userLng": strconv.FormatFloat(lon, 'f', -1, 64),
76-
"userLat": strconv.FormatFloat(lat, 'f', -1, 64),
77-
"timeStamp": strconv.FormatInt(s.timeSource.Now().UnixMilli(), 10),
78-
}
79-
80-
params["sign"] = s.buildUrlSign(params)
81-
82-
values := url.Values{}
83-
for k, v := range params {
84-
values.Set(k, v)
85-
}
86-
87-
return "http://118.113.105.29:8002/api/earlywarning/jsonPageList?" + values.Encode()
88-
}
89-
9056
func (s *SCEA) GetEvents(latitude, longitude float64) ([]Event, error) {
9157
var baseEvents []Event
9258

@@ -99,49 +65,47 @@ func (s *SCEA) GetEvents(latitude, longitude float64) ([]Event, error) {
9965
}
10066

10167
res, err := request.GET(
102-
s.getRequestUrl(latitude, longitude),
68+
SCEAListURL,
10369
10*time.Second, time.Second, 3, false, nil,
10470
map[string]string{"User-Agent": uarand.GetRandom()},
10571
)
10672
if err != nil {
10773
return nil, err
10874
}
10975

110-
// Parse SCEA_B JSON response
111-
var dataMap map[string]any
112-
err = json.Unmarshal(res, &dataMap)
113-
if err != nil {
76+
var data sceaListResponse
77+
if err := json.Unmarshal(res, &data); err != nil {
11478
return nil, err
11579
}
11680

117-
// Check server response
118-
if dataMap["code"].(float64) != 0 {
119-
return nil, fmt.Errorf("server error: %s", dataMap["msg"])
81+
if data.Code != 0 {
82+
return nil, fmt.Errorf("server error: %s", data.Msg)
12083
}
12184

122-
dataMapEvents, ok := dataMap["data"].([]any)
123-
if !ok {
85+
if len(data.Data) == 0 {
12486
return nil, errors.New("seismic event data is not available")
12587
}
12688

127-
// Ensure the response has the expected keys and values
128-
expectedKeys := []string{"eventId", "shockTime", "longitude", "latitude", "placeName", "magnitude", "depth", "infoTypeName"}
129-
130-
var resultArr []Event
131-
for _, event := range dataMapEvents {
132-
if !isMapHasKeys(event.(map[string]any), expectedKeys) || !isMapKeysEmpty(event.(map[string]any), expectedKeys) {
89+
resultArr := make([]Event, 0, len(data.Data))
90+
for _, event := range data.Data {
91+
if event.EventID == "" || event.ShockTime == 0 || event.PlaceName == "" {
13392
continue
13493
}
13594

95+
depth := -1.0
96+
if event.Depth != nil {
97+
depth = *event.Depth
98+
}
99+
136100
resultArr = append(resultArr, Event{
137-
Depth: -1,
138-
Verfied: event.(map[string]any)["infoTypeName"].(string) == "[正式]",
139-
Event: event.(map[string]any)["eventId"].(string),
140-
Region: event.(map[string]any)["placeName"].(string),
141-
Latitude: event.(map[string]any)["latitude"].(float64),
142-
Longitude: event.(map[string]any)["longitude"].(float64),
143-
Magnitude: s.getMagnitude(event.(map[string]any)["magnitude"].(float64)),
144-
Timestamp: time.UnixMilli(int64(event.(map[string]any)["shockTime"].(float64))).UnixMilli(),
101+
Depth: depth,
102+
Verfied: event.InfoTypeName == "[正式]",
103+
Event: event.EventID,
104+
Region: event.PlaceName,
105+
Latitude: event.Latitude,
106+
Longitude: event.Longitude,
107+
Magnitude: s.getMagnitude(event.Magnitude),
108+
Timestamp: time.UnixMilli(event.ShockTime).UnixMilli(),
145109
})
146110
}
147111

0 commit comments

Comments
 (0)