diff --git a/get-weather.go b/get-weather.go index 5a2b2ba..4894f0b 100644 --- a/get-weather.go +++ b/get-weather.go @@ -15,12 +15,13 @@ import ( ) // Settings -var city = "" +var city = "" var apiKey = "" // get your api key on https://openweathermap.org/ var location_lat = "" var location_lon = "" var units = "metric" var temperature_unit = "C" +var baseUrl = "https://api.openweathermap.org/data/2.5" var atmophere_icons_list = map[int]string{ 701: "", // Mist @@ -35,16 +36,7 @@ var atmophere_icons_list = map[int]string{ 781: "", // Tornado } -// PrettyPrint to print struct in a readable way -func PrettyPrint(i interface{}) string { - s, _ := json.MarshalIndent(i, "", "\t") - return string(s) -} - -func setColor(apiIcon string) (string, string) { - var color string - var icon string - +func setColor(apiIcon string) (color string, icon string) { colorIcons := map[string]map[string]string{ "#fdd835": { // yellow "01d": "", @@ -88,28 +80,33 @@ func setColor(apiIcon string) (string, string) { func setUrls(apiKey string) lib.Urls { urls := lib.Urls{} - baseUrl := "https://api.openweathermap.org/data/2.5" - if city == "" { + if city == "" { if location_lat == "" && location_lon == "" { res, err := http.Get("https://location.services.mozilla.com/v1/geolocate?key=geoclue") if err != nil { log.Fatal(err) } - defer res.Body.Close() + if res.StatusCode >= 200 && res.StatusCode < 300 { + defer res.Body.Close() - body, err := ioutil.ReadAll(res.Body) - if err != nil { - log.Fatal(err) + body, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatal(err) + } + + location := lib.GeoLocation{} + if err := json.Unmarshal(body, &location); err != nil { + log.Fatal(err) + } + + // TODO: Needs better casting to string + location_lat = fmt.Sprintf("%v", location.Location.Lat) + location_lon = fmt.Sprintf("%v", location.Location.Lon) + // ------------------------------------ + } else { + log.Fatal(res.StatusCode) } - - location := lib.GeoLocation{} - if err := json.Unmarshal(body, &location); err != nil { - log.Fatal(err) - } - - location_lat = fmt.Sprintf("%v", location.Location.Lat) - location_lon = fmt.Sprintf("%v", location.Location.Lon) } urls.Url = fmt.Sprintf("%s/weather?lat=%s&lon=%s&units=%s&appid=%s", baseUrl, location_lat, location_lon, units, apiKey) @@ -122,6 +119,7 @@ func setUrls(apiKey string) lib.Urls { return urls } +// TODO: Error handling func getWeather(url string) (lib.Weather, error) { weather := lib.Weather{} @@ -144,6 +142,7 @@ func getWeather(url string) (lib.Weather, error) { return weather, err } +// TODO: Error handling func getForecast(url string) (lib.Forecast, error) { forecast := lib.Forecast{} @@ -184,10 +183,11 @@ func main() { location_lon = os.Getenv("LNG") } + // TODO: Better error handling urls := setUrls(apiKey) weatherData, wErr := getWeather(urls.Url) forecastData, fErr := getForecast(urls.Url_forecast) - + // --------------------------- var curr string var forecast string var atmosphere string