From 7a8ee6dd9431297d38f462ce1870f42cf206f4ec Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 9 Apr 2020 18:11:15 -0400 Subject: [PATCH] add first weatherbot --- weatherbot/main.go | 30 +++++++++++++++++ weatherbot/today.go | 37 +++++++++++++++++++++ weatherbot/types.go | 80 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 weatherbot/main.go create mode 100644 weatherbot/today.go create mode 100644 weatherbot/types.go diff --git a/weatherbot/main.go b/weatherbot/main.go new file mode 100644 index 0000000..23cafc7 --- /dev/null +++ b/weatherbot/main.go @@ -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])) +} diff --git a/weatherbot/today.go b/weatherbot/today.go new file mode 100644 index 0000000..c8e9d76 --- /dev/null +++ b/weatherbot/today.go @@ -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() +} diff --git a/weatherbot/types.go b/weatherbot/types.go new file mode 100644 index 0000000..b94aeec --- /dev/null +++ b/weatherbot/types.go @@ -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"` +}