forked from mpinheir/eqk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (94 loc) · 2.97 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// This Go program fetches and displas Earthquake data.
// Data source: https://earthquake.usgs.gov/ - USGS
// Author: Marcelo Pinheiro - [Twitter](http://twitter.com/mpinheir)
//---------------------------------------------------------------------------------------
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
)
// EarthquakeAPIURL is the URL for earthquake data.
var EarthquakeAPIURL = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.geojson"
// Metadata contains metadata information.
type Metadata struct {
Generated int64 `json:"generated"`
URL string `json:"url"`
Title string `json:"title"`
Status int `json:"status"`
API string `json:"api"`
Count int `json:"count"`
}
// Earthquake represents earthquake data.
type Earthquake struct {
Type string `json:"type"`
Meta Metadata
Features []struct {
Type string `json:"type"`
Properties struct {
Mag float64 `json:"mag"`
Place string `json:"place"`
Time int64 `json:"time"`
Updated int64 `json:"updated"`
Tz int `json:"tz"`
} `json:"properties"`
} `json:"features"`
}
// Program will display Earthquakes with magnitude > minimumMagnitude
var minimumMagnitude float64
func main() {
minimumMagnitude = 0
if len(os.Args[1:]) > 0 {
if n, err := strconv.ParseFloat(os.Args[1], 64); err == nil {
minimumMagnitude = n
}
}
fmt.Println("Total number of Earthquakes: ", listquakes(minimumMagnitude))
}
func listquakes(minimumMagnitude float64) int {
// Fetch earthquake data from the API
earthquakeData, err := fetchEarthquakeData()
if err != nil {
log.Fatal("Failed to fetch earthquake data:", err)
}
fmt.Println("-------------------------------------------------------------------")
fmt.Printf("Earthquake above %f degrees, in the last 30 day:\n", minimumMagnitude)
fmt.Println("-------------------------------------------------------------------")
totEarthquake := 0
for _, feature := range earthquakeData.Features {
magnitude := feature.Properties.Mag
if magnitude > minimumMagnitude {
fmt.Println("Epicenter =", feature.Properties.Place)
fmt.Println("Magnitude:", magnitude)
t := time.UnixMilli(feature.Properties.Time)
fmt.Println("Time:", t.UTC())
fmt.Println("-------------------------------------------------------------------")
totEarthquake++
}
}
return totEarthquake
}
func fetchEarthquakeData() (Earthquake, error) {
// Build the request
req, err := http.NewRequest("GET", EarthquakeAPIURL, nil)
if err != nil {
return Earthquake{}, err
}
// Create an HTTP client and send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return Earthquake{}, err
}
defer resp.Body.Close()
// Decode the JSON response into the Earthquake struct
var earthquakeData Earthquake
if err := json.NewDecoder(resp.Body).Decode(&earthquakeData); err != nil {
return Earthquake{}, err
}
return earthquakeData, nil
}