Added metrics route

This commit is contained in:
2025-06-17 12:05:58 +02:00
parent 4844e946d3
commit cbafd785b9
8 changed files with 173 additions and 56 deletions

View File

@@ -2,7 +2,6 @@ package model
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
@@ -12,11 +11,6 @@ import (
"github.com/ceticamarco/zephyr/types"
)
const (
GEO_URL = "https://api.openweathermap.org/geo/1.0/direct"
WTR_URL = "https://api.openweathermap.org/data/3.0/onecall"
)
func getEmoji(condition string, isNight bool) string {
switch condition {
case "Thunderstorm":
@@ -27,8 +21,8 @@ func getEmoji(condition string, isNight bool) string {
return "🌧️"
case "Snow":
return "☃️"
case "Mist", "Smoke", "Haze", "Dust", "Fog", "Sand", "Ash", "Squall":
return "🌫"
case "Mist", "Smoke", "Haze", "Dust", "Fog", "Sand", "Ash", "Squall", "Clouds":
return ""
case "Tornado":
return "🌪️"
case "Clear":
@@ -39,8 +33,6 @@ func getEmoji(condition string, isNight bool) string {
return "☀️"
}
}
case "Clouds":
return "☁️"
case "SunWithCloud":
return "🌤️"
case "CloudWithSun":
@@ -50,41 +42,6 @@ func getEmoji(condition string, isNight bool) string {
return "❓"
}
func GetCoordinates(cityName string, apiKey string) (types.City, error) {
url, err := url.Parse(GEO_URL)
if err != nil {
return types.City{}, err
}
params := url.Query()
params.Set("q", cityName)
params.Set("limit", "1")
params.Set("appid", apiKey)
url.RawQuery = params.Encode()
res, err := http.Get(url.String())
if err != nil {
return types.City{}, err
}
defer res.Body.Close()
var geoArr []types.City
if err := json.NewDecoder(res.Body).Decode(&geoArr); err != nil {
return types.City{}, err
}
if len(geoArr) == 0 {
return types.City{}, errors.New("Cannot find this city")
}
return types.City{
Name: geoArr[0].Name,
Lat: geoArr[0].Lat,
Lon: geoArr[0].Lon,
}, nil
}
func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
url, err := url.Parse(WTR_URL)
if err != nil {