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
.config.json
poly-weather-script-go
>>>>>>> 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"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
@@ -13,31 +15,33 @@ import (
)
// Settings
var (
// optional
city = ""
location_lat = ""
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"
//
// Please set your API_KEY in the 'config.json' file
// You can get your API_KEY on https://openweathermap.org
// Your optional lat and lon values go there as well
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
}
const (
// optional
CITY = ""
// required
BASE_URL = "https://api.openweathermap.org/data/2.5" // request-url
UNITS = "metric" // standard (K), metric (C), imperial (F)
TEMP_UNIT = "C" // K (Kelvin), C (Celsius), F (Fahrenheit)
)
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) {
colorIcons := map[string]map[string]string{
"#fdd835": { // yellow
@@ -50,7 +54,7 @@ func setColor(apiIcon string) (color string, icon string) {
"11d": "",
"11n": "",
},
"$foreground": { // foreground
"-": { // foreground
"03d": "",
"03n": "",
},
@@ -80,7 +84,7 @@ func setColor(apiIcon string) (color string, icon string) {
return
}
func getLocation() {
func getLocation() (lat string, lon string) {
res, err := http.Get("https://location.services.mozilla.com/v1/geolocate?key=geoclue")
if err != nil {
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)
}
location_lat = fmt.Sprint(location.Location.Lat)
location_lon = fmt.Sprint(location.Location.Lon)
lat = fmt.Sprint(location.Location.Lat)
lon = fmt.Sprint(location.Location.Lon)
} else {
// TODO: needs updating if errors occur
log.Warningf("[GL] something went wrong while getting the location: %v", res.StatusCode)
}
return
}
func setUrls(apiKey string) (urls interfaces.Urls) {
if city == "" {
if location_lat == "" && location_lon == "" {
getLocation()
func setUrls(API_KEY string, LAT string, LON string) (urls interfaces.Urls) {
if CITY == "" {
var (
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_forecast = fmt.Sprintf("%s/forecast?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", BASE_URL, location_lat, location_lon, UNITS, API_KEY)
} else {
urls.Url = fmt.Sprintf("%s/weather?q=%s&units=%s&appid=%s", baseUrl, city, units, apiKey)
urls.Url_forecast = fmt.Sprintf("%s/forecast?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", BASE_URL, CITY, UNITS, API_KEY)
}
return
@@ -184,27 +196,43 @@ func getForecast(url string) (forecast interfaces.Forecast) {
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() {
// try to get apiKey and location from environment
// LOOKS LIKE THIS DOESN'T WORK IN POLYBAR
// TODO: Needs fix
// if apiKey == "" {
// apiKey = os.Getenv("OPEN_WEATHER_MAP_API_KEY")
// if apiKey == "" {
// 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"))
// }
// -----------------------------------------------
config := loadConfig()
// check if api key is set
if config.API_KEY == "" {
log.Warningf("[main] you need an API KEY to use this script! Get one on https://openweathermap.org")
os.Exit(1)
}
// api calls
urls := setUrls(apiKey)
urls := setUrls(config.API_KEY, config.LAT, config.LON)
weatherData := getWeather(urls.Url)
forecastData := getForecast(urls.Url_forecast)
@@ -227,10 +255,10 @@ func main() {
// Load other icons for Atmosphere group
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 {
forecast = "勒"
}
@@ -247,18 +275,18 @@ func main() {
// Load other icons for Atmosphere group
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 {
forecast = "勒"
}
// print output
if atmosphere == "" {
fmt.Printf("%s  %s", curr, forecast)
fmt.Printf("%s  %s", curr, forecast)
} else {
fmt.Printf("%s  %s", atmosphere, atmosphereForecast)
fmt.Printf("%s  %s", atmosphere, atmosphereForecast)
}
}