Skip to content

Commit

Permalink
Added earthquake event source API support of EQZT
Browse files Browse the repository at this point in the history
  • Loading branch information
bclswl0827 committed Sep 1, 2024
1 parent 540f148 commit 0ab7e78
Show file tree
Hide file tree
Showing 26 changed files with 265 additions and 48 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Starting from v2.2.5, all notable changes to this project will be documented in this file.

## v3.2.2

### New Features

- Enable mainline data protocol support for AnyShake Explorer without GNSS module.
- Added earthquake event source API support of EQZT.

## v3.2.1

### Breaking Changes
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v3.2.1
v3.2.2
5 changes: 1 addition & 4 deletions api/v1/station/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ func (e *explorerInfo) get(timeSource *timesource.Source, explorerDeps *explorer
e.Received = explorerDeps.Health.GetReceived()
e.SampleRate = explorerDeps.Health.GetSampleRate()

currentTime, err := timeSource.Get()
if err != nil {
return err
}
currentTime := timeSource.Get()
e.Elapsed = int64(currentTime.Sub(explorerDeps.Health.GetStartTime()).Seconds())

e.Latitude = float64(int(explorerDeps.Config.GetLatitude()*1000)) / 1000
Expand Down
8 changes: 2 additions & 6 deletions api/v1/station/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ func (o *osInfo) get(timeSource *timesource.Source) error {
return err
}

timestamp, err := timeSource.Get()
if err != nil {
return err
}

currentTime := timeSource.Get()
o.Uptime = int64(up.Seconds())
o.OS = runtime.GOOS
o.Arch = runtime.GOARCH
o.Distro = osutil.Name
o.Hostname = hostname
o.Timestamp = timestamp.UnixMilli()
o.Timestamp = currentTime.UnixMilli()
return nil
}
2 changes: 2 additions & 0 deletions api/v1/trace/cea.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
)

type CEA_DASE struct {
Expand All @@ -27,6 +28,7 @@ func (c *CEA_DASE) Fetch() ([]byte, error) {
res, err := request.GET(
"https://www-dase.cea.fr/evenement/derniers_evenements.php",
10*time.Second, time.Second, 3, false, nil,
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions api/v1/trace/ceic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
)

type CEIC struct {
Expand All @@ -23,6 +24,7 @@ func (c *CEIC) Fetch() ([]byte, error) {
res, err := request.GET(
"https://news.ceic.ac.cn/ajax/google",
10*time.Second, time.Second, 3, false, nil,
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions api/v1/trace/cwa.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
)

const HOST_IP_TO_BYPASS_GFW = "168.95.246.1:443"
Expand Down Expand Up @@ -41,6 +42,7 @@ func (c *CWA) Fetch() ([]byte, error) {
"https://www.cwa.gov.tw/V8/C/E/MOD/MAP_LIST.html",
10*time.Second, time.Second, 3, false,
c.createGFWBypasser(),
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion api/v1/trace/data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package trace

import "strconv"
import (
"strconv"
)

func string2Float(num string) float64 {
r, err := strconv.ParseFloat(num, 64)
Expand Down
189 changes: 189 additions & 0 deletions api/v1/trace/eqzt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package trace

import (
"bytes"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)

type EQZT struct {
dataSourceCache
}

func (j *EQZT) Property() string {
return "昭通市地震信息系统"
}

func (j *EQZT) Fetch() ([]byte, error) {
if time.Since(j.Time) <= EXPIRATION {
return j.Cache, nil
}

res, err := request.GET(
"http://www.eqzt.com/eqzt/seis/view_sbml.php?dzml=sbml",
10*time.Second, time.Second, 3, false, nil,
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
}

j.Time = time.Now()
j.Cache = make([]byte, len(res))
copy(j.Cache, res)

return res, nil
}

func (j *EQZT) Parse(data []byte) (map[string]any, error) {
result := make(map[string]any)
result["data"] = make([]any, 0)

reader := transform.NewReader(bytes.NewReader(data), simplifiedchinese.GB18030.NewDecoder())
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}

htmlReader := bytes.NewBuffer(data)
doc, err := goquery.NewDocumentFromReader(htmlReader)
if err != nil {
return nil, err
}
doc.Find("table").Each(func(i int, s *goquery.Selection) {
if val, ok := s.Attr("align"); !ok || val != "center" {
return
}
s.Find("tr").Each(func(i int, s *goquery.Selection) {
if i == 0 {
return
}
item := make(map[string]any)
s.Find("td").Each(func(i int, s *goquery.Selection) {
if val, ok := s.Attr("colspan"); ok && val == "8" {
return
}
value := s.Text()
switch i {
case 0:
item["timestamp"] = j.getTimestamp(value)
case 1:
item["latitude"] = j.getLatitude(value)
case 2:
item["longitude"] = j.getLongitude(value)
case 4:
item["magnitude"] = j.getMagnitude(value)
case 5:
trimVal := strings.TrimFunc(value, func(r rune) bool { return r == ' ' })
item["event"] = trimVal
item["region"] = fmt.Sprintf("云南及周边区域 - %s", trimVal)
}
})
result["data"] = append(result["data"].([]any), item)
})
})

return result, nil
}

func (j *EQZT) Format(latitude, longitude float64, data map[string]any) ([]seismicEvent, error) {
var list []seismicEvent
for _, v := range data["data"].([]any) {
_, ok := v.(map[string]any)["timestamp"]
if !ok {
continue
}
l := seismicEvent{
Verfied: true,
Depth: -1,
Latitude: v.(map[string]any)["latitude"].(float64),
Longitude: v.(map[string]any)["longitude"].(float64),
Event: v.(map[string]any)["event"].(string),
Region: v.(map[string]any)["region"].(string),
Timestamp: v.(map[string]any)["timestamp"].(int64),
Magnitude: v.(map[string]any)["magnitude"].(float64),
}
l.Distance = getDistance(latitude, l.Latitude, longitude, l.Longitude)
l.Estimation = getSeismicEstimation(l.Depth, l.Distance)

list = append(list, l)
}

return list, nil
}

func (j *EQZT) List(latitude, longitude float64) ([]seismicEvent, error) {
res, err := j.Fetch()
if err != nil {
return nil, err
}

data, err := j.Parse(res)
if err != nil {
return nil, err
}

list, err := j.Format(latitude, longitude, data)
if err != nil {
return nil, err
}

return list, nil
}

func (j *EQZT) getTimestamp(text string) int64 {
timestamp, _ := time.Parse("2006-01-02 15:04:05", text)
return timestamp.UnixMilli()
}

func (j *EQZT) getLatitude(text string) float64 {
text = strings.ReplaceAll(text, "°", " ")
text = strings.ReplaceAll(text, "′", "")

parts := strings.Fields(text)
if len(parts) != 2 {
return 0.0
}

degrees, err1 := strconv.ParseFloat(parts[0], 64)
minutes, err2 := strconv.ParseFloat(parts[1], 64)
if err1 != nil || err2 != nil {
return 0.0
}

return degrees + minutes/60
}

func (j *EQZT) getLongitude(text string) float64 {
text = strings.ReplaceAll(text, "°", " ")
text = strings.ReplaceAll(text, "′", "")

parts := strings.Fields(text)
if len(parts) != 2 {
return 0.0
}

degrees, err1 := strconv.ParseFloat(parts[0], 64)
minutes, err2 := strconv.ParseFloat(parts[1], 64)
if err1 != nil || err2 != nil {
return 0.0
}

return degrees + minutes/60
}

func (j *EQZT) getMagnitude(text string) float64 {
re := regexp.MustCompile(`\d+(\.\d+)?`)
text = re.FindString(text)
return string2Float(text)
}
2 changes: 2 additions & 0 deletions api/v1/trace/hko.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
"github.com/sbabiv/xml2map"
)

Expand All @@ -25,6 +26,7 @@ func (h *HKO) Fetch() ([]byte, error) {
res, err := request.GET(
"https://www.hko.gov.hk/gts/QEM/eq_app-30d_uc.xml",
10*time.Second, time.Second, 3, false, nil,
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions api/v1/trace/ingv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
)

type INGV struct {
Expand All @@ -25,6 +26,7 @@ func (c *INGV) Fetch() ([]byte, error) {
res, err := request.GET(
"https://webservices.ingv.it/fdsnws/event/1/query?minmag=-1&format=text&timezone=UTC",
10*time.Second, time.Second, 3, false, nil,
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions api/v1/trace/jma.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
)

type JMA struct {
Expand All @@ -24,6 +25,7 @@ func (j *JMA) Fetch() ([]byte, error) {
res, err := request.GET(
"https://www.jma.go.jp/bosai/quake/data/list.json",
10*time.Second, time.Second, 3, false, nil,
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions api/v1/trace/kma.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
)

type KMA struct {
Expand All @@ -26,6 +27,7 @@ func (k *KMA) Fetch() ([]byte, error) {
res, err := request.GET(
"https://www.weather.go.kr/w/eqk-vol/search/korea.do",
10*time.Second, time.Second, 3, false, nil,
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions api/v1/trace/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (t *Trace) Register(rg *gin.RouterGroup, resolver *v1.Resolver) error {
"SCEA_E": &SCEA_E{},
"SCEA_B": &SCEA_B{},
"CEA_DASE": &CEA_DASE{},
"EQZT": &EQZT{},
}

var explorerDeps *explorer.ExplorerDependency
Expand Down
2 changes: 2 additions & 0 deletions api/v1/trace/scea-b.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
)

type SCEA_B struct {
Expand All @@ -24,6 +25,7 @@ func (s *SCEA_B) Fetch() ([]byte, error) {
res, err := request.GET(
"http://118.113.105.29:8002/api/bulletin/jsonPageList?pageSize=100",
10*time.Second, time.Second, 3, false, nil,
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions api/v1/trace/scea-e.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/anyshake/observer/utils/request"
"github.com/corpix/uarand"
)

type SCEA_E struct {
Expand All @@ -23,6 +24,7 @@ func (s *SCEA_E) Fetch() ([]byte, error) {
res, err := request.GET(
"http://118.113.105.29:8002/api/earlywarning/jsonPageList?pageSize=100",
10*time.Second, time.Second, 3, false, nil,
map[string]string{"User-Agent": uarand.GetRandom()},
)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 0ab7e78

Please sign in to comment.