add first weatherbot
This commit is contained in:
parent
6008464684
commit
7a8ee6dd94
30
weatherbot/main.go
Normal file
30
weatherbot/main.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var apiKey = "7501365e9803ca244bf100fb41084538"
|
||||
var lat = "39.004608"
|
||||
var long = "-76.875671"
|
||||
|
||||
func main() {
|
||||
oneCallUrl := fmt.Sprintf("https://api.openweathermap.org/data/2.5/onecall?lat=%v&lon=%v&units=imperial&appid=%v", lat, long, apiKey)
|
||||
response, err := http.Get(oneCallUrl)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("The HTTP request failed with error %s\n", err)
|
||||
}
|
||||
data, _ := ioutil.ReadAll(response.Body)
|
||||
|
||||
var report OneCallReport
|
||||
err = json.Unmarshal(data, &report)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(TodayToReport(report.Daily[0]))
|
||||
}
|
37
weatherbot/today.go
Normal file
37
weatherbot/today.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
const todayTemplate = `Today's Weather:
|
||||
Conditions: {{range .Weather}} {{.Description}} {{end}}
|
||||
Morning Temp: {{.Temp.Morn}} F, Feels like {{.FeelsLike.Morn}} F
|
||||
Daytime Temp: {{.Temp.Day}} F, Feels like {{.FeelsLike.Day}} F
|
||||
Evening Temp: {{.Temp.Eve}} F, Feels like {{.FeelsLike.Eve}} F
|
||||
LateNight Temp: {{.Temp.Night}} F, Feels like {{.FeelsLike.Night}} F
|
||||
`
|
||||
|
||||
func TodayToReport(today Daily) string {
|
||||
var report strings.Builder
|
||||
tmpl, err := template.New("today").Parse(todayTemplate)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if today.Rain > 0 {
|
||||
fmt.Fprintf(&report, "Might rain today (Rain == %v)\n", today.Rain)
|
||||
}
|
||||
if today.WindSpeed > 12.00 {
|
||||
fmt.Fprintf(&report, "Seems pretty windy today (WindSpeed == %v)\n", today.WindSpeed)
|
||||
}
|
||||
if today.Humidity > 80.0 {
|
||||
fmt.Fprintf(&report, "High humidity today! (Humidity == %v)\n", today.Humidity)
|
||||
}
|
||||
if err := tmpl.Execute(&report, today); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return report.String()
|
||||
}
|
80
weatherbot/types.go
Normal file
80
weatherbot/types.go
Normal file
@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
type OneCallReport struct {
|
||||
Lat int `json:"lat"`
|
||||
Lon float64 `json:"lon"`
|
||||
Timezone string `json:"timezone"`
|
||||
Current Current `json:"current"`
|
||||
Hourly []Hourly `json:"hourly"`
|
||||
Daily []Daily `json:"daily"`
|
||||
}
|
||||
type Weather struct {
|
||||
ID int `json:"id"`
|
||||
Main string `json:"main"`
|
||||
Description string `json:"description"`
|
||||
Icon string `json:"icon"`
|
||||
}
|
||||
type Current struct {
|
||||
Dt int `json:"dt"`
|
||||
Sunrise int `json:"sunrise"`
|
||||
Sunset int `json:"sunset"`
|
||||
Temp float64 `json:"temp"`
|
||||
FeelsLike float64 `json:"feels_like"`
|
||||
Pressure int `json:"pressure"`
|
||||
Humidity int `json:"humidity"`
|
||||
DewPoint float64 `json:"dew_point"`
|
||||
Uvi float64 `json:"uvi"`
|
||||
Clouds int `json:"clouds"`
|
||||
Visibility int `json:"visibility"`
|
||||
WindSpeed float64 `json:"wind_speed"`
|
||||
WindDeg int `json:"wind_deg"`
|
||||
WindGust float64 `json:"wind_gust"`
|
||||
Weather []Weather `json:"weather"`
|
||||
Rain Rain `json:"rain"`
|
||||
}
|
||||
type Rain struct {
|
||||
OneH float64 `json:"1h"`
|
||||
}
|
||||
type Hourly struct {
|
||||
Dt int `json:"dt"`
|
||||
Temp float64 `json:"temp"`
|
||||
FeelsLike float64 `json:"feels_like"`
|
||||
Pressure int `json:"pressure"`
|
||||
Humidity int `json:"humidity"`
|
||||
DewPoint float64 `json:"dew_point"`
|
||||
Clouds int `json:"clouds"`
|
||||
WindSpeed float64 `json:"wind_speed"`
|
||||
WindDeg int `json:"wind_deg"`
|
||||
Weather []Weather `json:"weather"`
|
||||
Rain Rain `json:"rain,omitempty"`
|
||||
}
|
||||
type Temp struct {
|
||||
Day float64 `json:"day"`
|
||||
Min float64 `json:"min"`
|
||||
Max float64 `json:"max"`
|
||||
Night float64 `json:"night"`
|
||||
Eve float64 `json:"eve"`
|
||||
Morn float64 `json:"morn"`
|
||||
}
|
||||
type FeelsLike struct {
|
||||
Day float64 `json:"day"`
|
||||
Night float64 `json:"night"`
|
||||
Eve float64 `json:"eve"`
|
||||
Morn float64 `json:"morn"`
|
||||
}
|
||||
type Daily struct {
|
||||
Dt int `json:"dt"`
|
||||
Sunrise int `json:"sunrise"`
|
||||
Sunset int `json:"sunset"`
|
||||
Temp Temp `json:"temp"`
|
||||
FeelsLike FeelsLike `json:"feels_like"`
|
||||
Pressure int `json:"pressure"`
|
||||
Humidity int `json:"humidity"`
|
||||
DewPoint float64 `json:"dew_point"`
|
||||
WindSpeed float64 `json:"wind_speed"`
|
||||
WindDeg int `json:"wind_deg"`
|
||||
Weather []Weather `json:"weather"`
|
||||
Clouds int `json:"clouds"`
|
||||
Rain float64 `json:"rain,omitempty"`
|
||||
Uvi float64 `json:"uvi"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user