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 // Settings
var city = "<CITY>" var city = ""
var apiKey = "" // get your api key on https://openweathermap.org/ var apiKey = "" // get your api key on https://openweathermap.org/
var location_lat = "" var location_lat = ""
var location_lon = "" var location_lon = ""
var units = "metric" var units = "metric"
var temperature_unit = "C" var temperature_unit = "C"
var baseUrl = "https://api.openweathermap.org/data/2.5"
var atmophere_icons_list = map[int]string{ var atmophere_icons_list = map[int]string{
701: "", // Mist 701: "", // Mist
@@ -35,16 +36,7 @@ var atmophere_icons_list = map[int]string{
781: "", // Tornado 781: "", // Tornado
} }
// PrettyPrint to print struct in a readable way func setColor(apiIcon string) (color string, icon string) {
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
colorIcons := map[string]map[string]string{ colorIcons := map[string]map[string]string{
"#fdd835": { // yellow "#fdd835": { // yellow
"01d": "", "01d": "",
@@ -88,28 +80,33 @@ func setColor(apiIcon string) (string, string) {
func setUrls(apiKey string) lib.Urls { func setUrls(apiKey string) lib.Urls {
urls := lib.Urls{} urls := lib.Urls{}
baseUrl := "https://api.openweathermap.org/data/2.5" if city == "" {
if city == "<CITY>" {
if location_lat == "" && location_lon == "" { if location_lat == "" && location_lon == "" {
res, err := http.Get("https://location.services.mozilla.com/v1/geolocate?key=geoclue") res, err := http.Get("https://location.services.mozilla.com/v1/geolocate?key=geoclue")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 {
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
log.Fatal(err) 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) 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 return urls
} }
// TODO: Error handling
func getWeather(url string) (lib.Weather, error) { func getWeather(url string) (lib.Weather, error) {
weather := lib.Weather{} weather := lib.Weather{}
@@ -144,6 +142,7 @@ func getWeather(url string) (lib.Weather, error) {
return weather, err return weather, err
} }
// TODO: Error handling
func getForecast(url string) (lib.Forecast, error) { func getForecast(url string) (lib.Forecast, error) {
forecast := lib.Forecast{} forecast := lib.Forecast{}
@@ -184,10 +183,11 @@ func main() {
location_lon = os.Getenv("LNG") location_lon = os.Getenv("LNG")
} }
// TODO: Better error handling
urls := setUrls(apiKey) urls := setUrls(apiKey)
weatherData, wErr := getWeather(urls.Url) weatherData, wErr := getWeather(urls.Url)
forecastData, fErr := getForecast(urls.Url_forecast) forecastData, fErr := getForecast(urls.Url_forecast)
// ---------------------------
var curr string var curr string
var forecast string var forecast string
var atmosphere string var atmosphere string