some minor updates

This commit is contained in:
2022-02-19 10:27:42 +01:00
parent ff4d399551
commit 3b2283a6f3

View File

@@ -15,12 +15,13 @@ import (
)
// Settings
var city = "<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,14 +80,14 @@ 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 == "<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)
}
if res.StatusCode >= 200 && res.StatusCode < 300 {
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
@@ -108,8 +100,13 @@ func setUrls(apiKey string) lib.Urls {
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)
}
}
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