updated come code + added config file

This commit is contained in:
2022-03-09 08:56:21 +01:00
parent 7e19a8e9af
commit 540b54102d
3 changed files with 92 additions and 58 deletions

1
.gitignore vendored
View File

@@ -33,5 +33,6 @@ go.work
======= =======
.env .env
.config.json
poly-weather-script-go poly-weather-script-go
>>>>>>> 1f5ce74 (updated gitignore) >>>>>>> 1f5ce74 (updated gitignore)

5
config.json Normal file
View File

@@ -0,0 +1,5 @@
{
"apikey": "",
"lat": "",
"lon": ""
}

144
main.go
View File

@@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"path/filepath"
"strings" "strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -13,31 +15,33 @@ import (
) )
// Settings // Settings
var ( //
// optional // Please set your API_KEY in the 'config.json' file
city = "" // You can get your API_KEY on https://openweathermap.org
location_lat = "" // Your optional lat and lon values go there as well
location_lon = ""
// required
apiKey = "" // get your api key on https://openweathermap.org/
units = "metric"
temperature_unit = "C"
baseUrl = "https://api.openweathermap.org/data/2.5"
atmophere_icons_list = map[int]string{ const (
701: "", // Mist // optional
711: "", // Smoke CITY = ""
721: "", // Haze // required
731: "", // Dust (Sand / dust whirls) BASE_URL = "https://api.openweathermap.org/data/2.5" // request-url
741: "", // Fog UNITS = "metric" // standard (K), metric (C), imperial (F)
751: "", // Sand TEMP_UNIT = "C" // K (Kelvin), C (Celsius), F (Fahrenheit)
761: "", // Dust
762: "", // Ash
771: "", // Squalls
781: "", // Tornado
}
) )
var atmophere_icons_list = map[int]string{
701: "", // Mist
711: "", // Smoke
721: "", // Haze
731: "", // Dust (Sand / dust whirls)
741: "", // Fog
751: "", // Sand
761: "", // Dust
762: "", // Ash
771: "", // Squalls
781: "", // Tornado
}
func setColor(apiIcon string) (color string, icon string) { func setColor(apiIcon string) (color string, icon string) {
colorIcons := map[string]map[string]string{ colorIcons := map[string]map[string]string{
"#fdd835": { // yellow "#fdd835": { // yellow
@@ -50,7 +54,7 @@ func setColor(apiIcon string) (color string, icon string) {
"11d": "", "11d": "",
"11n": "", "11n": "",
}, },
"$foreground": { // foreground "-": { // foreground
"03d": "", "03d": "",
"03n": "", "03n": "",
}, },
@@ -80,7 +84,7 @@ func setColor(apiIcon string) (color string, icon string) {
return return
} }
func getLocation() { func getLocation() (lat string, lon string) {
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.Warningf("[GL] unable to get location: %v", err) log.Warningf("[GL] unable to get location: %v", err)
@@ -99,26 +103,34 @@ func getLocation() {
log.Warningf("[GL] unable to marshal location data: %v", err) log.Warningf("[GL] unable to marshal location data: %v", err)
} }
location_lat = fmt.Sprint(location.Location.Lat) lat = fmt.Sprint(location.Location.Lat)
location_lon = fmt.Sprint(location.Location.Lon) lon = fmt.Sprint(location.Location.Lon)
} else { } else {
// TODO: needs updating if errors occur // TODO: needs updating if errors occur
log.Warningf("[GL] something went wrong while getting the location: %v", res.StatusCode) log.Warningf("[GL] something went wrong while getting the location: %v", res.StatusCode)
} }
return
} }
func setUrls(apiKey string) (urls interfaces.Urls) { func setUrls(API_KEY string, LAT string, LON string) (urls interfaces.Urls) {
if city == "" { if CITY == "" {
if location_lat == "" && location_lon == "" { var (
getLocation() location_lat string
location_lon string
)
if LAT == "" || LON == "" {
location_lat, location_lon = getLocation()
} else {
location_lat = LAT
location_lon = 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", BASE_URL, location_lat, location_lon, UNITS, API_KEY)
urls.Url_forecast = fmt.Sprintf("%s/forecast?lat=%s&lon=%s&units=%s&appid=%s", baseUrl, location_lat, location_lon, units, apiKey) urls.Url_forecast = fmt.Sprintf("%s/forecast?lat=%s&lon=%s&units=%s&appid=%s", BASE_URL, location_lat, location_lon, UNITS, API_KEY)
} else { } else {
urls.Url = fmt.Sprintf("%s/weather?q=%s&units=%s&appid=%s", baseUrl, city, units, apiKey) urls.Url = fmt.Sprintf("%s/weather?q=%s&units=%s&appid=%s", BASE_URL, CITY, UNITS, API_KEY)
urls.Url_forecast = fmt.Sprintf("%s/forecast?q=%s&units=%s&appid=%s", baseUrl, city, units, apiKey) urls.Url_forecast = fmt.Sprintf("%s/forecast?q=%s&units=%s&appid=%s", BASE_URL, CITY, UNITS, API_KEY)
} }
return return
@@ -184,27 +196,43 @@ func getForecast(url string) (forecast interfaces.Forecast) {
return return
} }
type Config struct {
API_KEY string `json:"apikey"`
LAT string `json:"lat"`
LON string `json:"lon"`
}
func loadConfig() (config Config) {
path, err := os.Executable()
if err != nil {
log.Warningf("[LC] Can't get executable path: %v", err)
}
dir := filepath.Dir(path)
content, err := ioutil.ReadFile(filepath.Join(dir, "./config.json"))
if err != nil {
log.Warningf("[LC] Unable to read config file: %v", err)
}
err = json.Unmarshal(content, &config)
if err != nil {
log.Warningf("[LC] Error during Unmarshal(): %v", err)
}
return
}
func main() { func main() {
// try to get apiKey and location from environment config := loadConfig()
// LOOKS LIKE THIS DOESN'T WORK IN POLYBAR
// TODO: Needs fix // check if api key is set
// if apiKey == "" { if config.API_KEY == "" {
// apiKey = os.Getenv("OPEN_WEATHER_MAP_API_KEY") log.Warningf("[main] you need an API KEY to use this script! Get one on https://openweathermap.org")
// if apiKey == "" { os.Exit(1)
// log.Warning("[main] you need an API KEY to use this script! Get one on https://openweathermap.org/") }
// os.Exit(1)
// }
// }
// if location_lat == "" {
// location_lat = fmt.Sprint(os.Getenv("LAT"))
// }
// if location_lon == "" {
// location_lon = fmt.Sprint(os.Getenv("LNG"))
// }
// -----------------------------------------------
// api calls // api calls
urls := setUrls(apiKey) urls := setUrls(config.API_KEY, config.LAT, config.LON)
weatherData := getWeather(urls.Url) weatherData := getWeather(urls.Url)
forecastData := getForecast(urls.Url_forecast) forecastData := getForecast(urls.Url_forecast)
@@ -227,10 +255,10 @@ func main() {
// Load other icons for Atmosphere group // Load other icons for Atmosphere group
if group == "Atmosphere" { if group == "Atmosphere" {
atmosphere = "%{F#e57c46}" + atmophere_icons_list[id] + "%{F$foreground} " + temp + "°" + temperature_unit + "%{F-}" atmosphere = "%{F#e57c46} " + atmophere_icons_list[id] + " %{F-} " + temp + "°" + TEMP_UNIT + "%{F-}"
} }
curr = "%{F" + color + "}" + icon + "%{F$foreground} " + temp + "°" + temperature_unit + "%{F-}" curr = "%{F" + color + "} " + icon + " %{F-} " + temp + "°" + TEMP_UNIT + "%{F-}"
} else { } else {
forecast = "勒" forecast = "勒"
} }
@@ -247,18 +275,18 @@ func main() {
// Load other icons for Atmosphere group // Load other icons for Atmosphere group
if group == "Atmosphere" { if group == "Atmosphere" {
atmosphereForecast = "%{F#e57c46}" + atmophere_icons_list[id] + "{F$foreground} " + temp + "°" + temperature_unit + "%{F-}" atmosphereForecast = "%{F#e57c46} " + atmophere_icons_list[id] + " {F-} " + temp + "°" + TEMP_UNIT + "%{F-}"
} }
forecast = "%{F" + color + "}" + icon + "%{F$foreground} " + temp + "°" + temperature_unit + "%{F-}" forecast = "%{F" + color + "} " + icon + " %{F-} " + temp + "°" + TEMP_UNIT + "%{F-}"
} else { } else {
forecast = "勒" forecast = "勒"
} }
// print output // print output
if atmosphere == "" { if atmosphere == "" {
fmt.Printf("%s  %s", curr, forecast) fmt.Printf("%s  %s", curr, forecast)
} else { } else {
fmt.Printf("%s  %s", atmosphere, atmosphereForecast) fmt.Printf("%s  %s", atmosphere, atmosphereForecast)
} }
} }