weather-cli/cmd/weather.go

62 lines
1.2 KiB
Go
Raw Normal View History

2022-07-07 23:43:16 -04:00
package main
2022-07-08 22:12:21 -04:00
import (
"encoding/json"
"fmt"
2022-07-08 22:12:21 -04:00
"io/ioutil"
"log"
"net/http"
)
2022-07-07 23:43:16 -04:00
2022-07-26 22:22:05 -04:00
// Get and store the current forecast
2022-08-02 23:26:28 -04:00
func getCurrent(app *Application) {
2022-07-07 23:43:16 -04:00
2022-07-08 22:12:21 -04:00
lat := "lat=" + app.Config.Latitude
lon := "&lon=" + app.Config.Longitude
units := "&units=" + app.Config.Units
key := "&appid=" + app.Config.ApiKey
url := "https://pro.openweathermap.org/data/2.5/weather?" + lat + lon + units + key
2022-07-07 23:43:16 -04:00
res, err := http.Get(url)
if err != nil {
log.Println(err)
}
defer res.Body.Close()
fmt.Println(res.Body)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
}
err = json.Unmarshal(body, &app.Forecast)
if err != nil {
log.Println(err)
}
}
// Query forecast by zip code
func getCurrentByLoc(app *Application) {
zip := app.Config.Location
units := "&units=" + app.Config.Units
key := "&appid=" + app.Config.ApiKey
url := "https://pro.openweathermap.org/data/2.5/weather?q=" + zip + units + key
2022-07-08 22:12:21 -04:00
res, err := http.Get(url)
if err != nil {
log.Println(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
}
err = json.Unmarshal(body, &app.Forecast)
if err != nil {
log.Println(err)
}
2022-07-07 23:43:16 -04:00
}