-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add earthquake event source API support of CEA and INGV
- Loading branch information
1 parent
f2b6d43
commit 708759a
Showing
19 changed files
with
411 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v2.11.7 | ||
v2.11.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package trace | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
"github.com/anyshake/observer/utils/duration" | ||
"github.com/anyshake/observer/utils/request" | ||
) | ||
|
||
type CEA_DASE struct { | ||
DataSourceCache | ||
} | ||
|
||
func (c *CEA_DASE) Property() string { | ||
return "Commissariat à l'Energie Atomique" | ||
} | ||
|
||
func (c *CEA_DASE) Fetch() ([]byte, error) { | ||
if duration.Difference(time.Now(), c.Time) <= EXPIRATION { | ||
return c.Cache, nil | ||
} | ||
|
||
res, err := request.GET( | ||
"https://www-dase.cea.fr/evenement/derniers_evenements.php", | ||
10*time.Second, time.Second, 3, false, nil, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.Time = time.Now() | ||
c.Cache = make([]byte, len(res)) | ||
copy(c.Cache, res) | ||
|
||
return res, nil | ||
} | ||
|
||
func (c *CEA_DASE) Parse(data []byte) (map[string]any, error) { | ||
result := make(map[string]any) | ||
result["data"] = make([]any, 0) | ||
|
||
reader := bytes.NewBuffer(data) | ||
doc, err := goquery.NewDocumentFromReader(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
doc.Find(".arial11bleu").Each(func(i int, s *goquery.Selection) { | ||
item := make(map[string]any) | ||
var dateString string | ||
s.Find("td").Each(func(i int, s *goquery.Selection) { | ||
value := strings.TrimSpace(s.Text()) | ||
switch i { | ||
case 0: | ||
item["depth"] = -1 | ||
dateString = value | ||
case 1: | ||
item["timestamp"] = c.getTimestamp(fmt.Sprintf("%s %s", dateString, value)) | ||
case 2: | ||
item["latitude"] = c.getLatitude(value) | ||
item["longitude"] = c.getLongitude(value) | ||
case 3: | ||
item["event"] = value | ||
item["region"] = value | ||
case 4: | ||
item["magnitude"] = c.getMagnitude(value) | ||
} | ||
}) | ||
result["data"] = append(result["data"].([]any), item) | ||
}) | ||
|
||
return result, nil | ||
} | ||
|
||
func (c *CEA_DASE) Format(latitude, longitude float64, data map[string]any) ([]Event, error) { | ||
var list []Event | ||
for _, v := range data["data"].([]any) { | ||
l := Event{ | ||
Verfied: false, | ||
Latitude: v.(map[string]any)["latitude"].(float64), | ||
Longitude: v.(map[string]any)["longitude"].(float64), | ||
Depth: float64(v.(map[string]any)["depth"].(int)), | ||
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 = getEstimation(l.Depth, l.Distance) | ||
|
||
list = append(list, l) | ||
} | ||
|
||
return list, nil | ||
} | ||
|
||
func (c *CEA_DASE) List(latitude, longitude float64) ([]Event, error) { | ||
res, err := c.Fetch() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data, err := c.Parse(res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
list, err := c.Format(latitude, longitude, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return list, nil | ||
} | ||
|
||
func (c *CEA_DASE) getTimestamp(data string) int64 { | ||
t, _ := time.Parse("02/01/2006 15:04:05", data) | ||
return t.Add(-1 * time.Hour).UnixMilli() | ||
} | ||
|
||
func (c *CEA_DASE) getMagnitude(data string) float64 { | ||
m := strings.Split(data, "=") | ||
if len(m) > 1 { | ||
magnitude, _ := strconv.ParseFloat(m[1], 64) | ||
return magnitude | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (c *CEA_DASE) getLatitude(data string) float64 { | ||
pos := strings.Split(data, ",") | ||
if len(pos) > 1 { | ||
latitude, _ := strconv.ParseFloat(pos[0], 64) | ||
return latitude | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (c *CEA_DASE) getLongitude(data string) float64 { | ||
pos := strings.Split(data, ",") | ||
if len(pos) > 1 { | ||
longitude, _ := strconv.ParseFloat(pos[1], 64) | ||
return longitude | ||
} | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package trace | ||
|
||
import ( | ||
"encoding/csv" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/anyshake/observer/utils/duration" | ||
"github.com/anyshake/observer/utils/request" | ||
) | ||
|
||
type INGV struct { | ||
DataSourceCache | ||
} | ||
|
||
func (c *INGV) Property() string { | ||
return "Istituto nazionale di geofisica e vulcanologia" | ||
} | ||
|
||
func (c *INGV) Fetch() ([]byte, error) { | ||
if duration.Difference(time.Now(), c.Time) <= EXPIRATION { | ||
return c.Cache, nil | ||
} | ||
|
||
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, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.Time = time.Now() | ||
c.Cache = make([]byte, len(res)) | ||
copy(c.Cache, res) | ||
|
||
return res, nil | ||
} | ||
|
||
func (c *INGV) Parse(data []byte) (map[string]any, error) { | ||
result := make(map[string]any) | ||
result["data"] = make([]any, 0) | ||
|
||
csvDataStr := strings.ReplaceAll(string(data), "|", ",") | ||
reader := csv.NewReader(strings.NewReader(csvDataStr)) | ||
records, err := reader.ReadAll() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, record := range records[1:] { | ||
item := make(map[string]any) | ||
for i, v := range record { | ||
switch i { | ||
case 1: | ||
item["timestamp"] = c.getTimestamp(v) | ||
case 2: | ||
item["latitude"] = c.getLatitude(v) | ||
case 3: | ||
item["longitude"] = c.getLongitude(v) | ||
case 4: | ||
item["depth"] = c.getDepth(v) | ||
case 10: | ||
item["magnitude"] = c.getMagnitude(v) | ||
case 12: | ||
item["event"] = v | ||
item["region"] = v | ||
} | ||
} | ||
result["data"] = append(result["data"].([]any), item) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (c *INGV) Format(latitude, longitude float64, data map[string]any) ([]Event, error) { | ||
var list []Event | ||
for _, v := range data["data"].([]any) { | ||
l := Event{ | ||
Verfied: true, | ||
Latitude: v.(map[string]any)["latitude"].(float64), | ||
Longitude: v.(map[string]any)["longitude"].(float64), | ||
Depth: v.(map[string]any)["depth"].(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 = getEstimation(l.Depth, l.Distance) | ||
|
||
list = append(list, l) | ||
} | ||
|
||
return list, nil | ||
} | ||
|
||
func (c *INGV) List(latitude, longitude float64) ([]Event, error) { | ||
res, err := c.Fetch() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data, err := c.Parse(res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
list, err := c.Format(latitude, longitude, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return list, nil | ||
} | ||
|
||
func (c *INGV) getTimestamp(data string) int64 { | ||
t, _ := time.Parse("2006-01-02T15:04:05.000000", data) | ||
return t.UnixMilli() | ||
} | ||
|
||
func (c *INGV) getMagnitude(data string) float64 { | ||
m, err := strconv.ParseFloat(data, 64) | ||
if err == nil { | ||
return m | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (c *INGV) getDepth(data string) float64 { | ||
d, err := strconv.ParseFloat(data, 64) | ||
if err == nil { | ||
return d | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (c *INGV) getLatitude(data string) float64 { | ||
lat, err := strconv.ParseFloat(data, 64) | ||
if err == nil { | ||
return lat | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (c *INGV) getLongitude(data string) float64 { | ||
lng, err := strconv.ParseFloat(data, 64) | ||
if err == nil { | ||
return lng | ||
} | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package dao | ||
|
||
import ( | ||
"github.com/anyshake/observer/publisher" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func Insert(db *gorm.DB, gp *publisher.Geophone) error { | ||
return db.Table(DB_TABLENAME).Create(&dbRecord{Geophone: *gp}).Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dao | ||
|
||
import "gorm.io/gorm" | ||
|
||
func Migrate(db *gorm.DB) error { | ||
return db.Table(DB_TABLENAME).AutoMigrate(&dbRecord{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.