Fixed bugs and added hourly forecast
This commit is contained in:
66
README.md
66
README.md
@@ -36,10 +36,12 @@ which yield the following:
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"date": "Thursday, 2025/06/19",
|
"date": "Thursday, 2025/07/31",
|
||||||
"temperature": "33°C",
|
"temperature": "29°C",
|
||||||
|
"min": "19°C",
|
||||||
|
"max": "29°C",
|
||||||
"condition": "Clear",
|
"condition": "Clear",
|
||||||
"feelsLike": "36°C",
|
"feelsLike": "29°C",
|
||||||
"emoji": "☀️"
|
"emoji": "☀️"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -55,10 +57,12 @@ which yields:
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"date": "Thursday, 2025/06/19",
|
"date": "Thursday, 2025/07/31",
|
||||||
"temperature": "65°F",
|
"temperature": "61°F",
|
||||||
|
"min": "51°F",
|
||||||
|
"max": "61°F",
|
||||||
"condition": "Clear",
|
"condition": "Clear",
|
||||||
"feelsLike": "68°F",
|
"feelsLike": "61°F",
|
||||||
"emoji": "☀️"
|
"emoji": "☀️"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -105,6 +109,8 @@ As in the previous examples, you can append the `i` query parameter to get resul
|
|||||||
in imperial units.
|
in imperial units.
|
||||||
|
|
||||||
## Forecast
|
## Forecast
|
||||||
|
|
||||||
|
### Daily
|
||||||
The `/forecast/:city` endpoint allows you to get the weather forecast of the
|
The `/forecast/:city` endpoint allows you to get the weather forecast of the
|
||||||
next 4 days. For example:
|
next 4 days. For example:
|
||||||
|
|
||||||
@@ -128,7 +134,8 @@ which yields:
|
|||||||
"arrow": "↗️",
|
"arrow": "↗️",
|
||||||
"direction": "SSW",
|
"direction": "SSW",
|
||||||
"speed": "14.7 km/h"
|
"speed": "14.7 km/h"
|
||||||
}
|
},
|
||||||
|
"rainProbability": "100%"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"date": "Wednesday, 2025/05/07",
|
"date": "Wednesday, 2025/05/07",
|
||||||
@@ -141,7 +148,8 @@ which yields:
|
|||||||
"arrow": "↘️",
|
"arrow": "↘️",
|
||||||
"direction": "NNW",
|
"direction": "NNW",
|
||||||
"speed": "13.9 km/h"
|
"speed": "13.9 km/h"
|
||||||
}
|
},
|
||||||
|
"rainProbability": "100%"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -150,6 +158,48 @@ which yields:
|
|||||||
As in the previous examples, you can append the `i` query parameter to get results
|
As in the previous examples, you can append the `i` query parameter to get results
|
||||||
in imperial units.
|
in imperial units.
|
||||||
|
|
||||||
|
### Hourly
|
||||||
|
You can also get the hourly forecast of a time window of 9 hours by appending
|
||||||
|
the `h`(hourly) query parameter to the URL:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s 'http://127.0.0.1:3000/forecast/tapei?h' | jq
|
||||||
|
```
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"forecast": [
|
||||||
|
{
|
||||||
|
"time": "2:00 PM",
|
||||||
|
"temperature": "26°C",
|
||||||
|
"condition": "Clouds",
|
||||||
|
"emoji": "☁️",
|
||||||
|
"wind": {
|
||||||
|
"arrow": "↘️",
|
||||||
|
"direction": "NW",
|
||||||
|
"speed": "23.3 km/h"
|
||||||
|
},
|
||||||
|
"rainProbability": "0%"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"time": "3:00 PM",
|
||||||
|
"temperature": "27°C",
|
||||||
|
"condition": "Clouds",
|
||||||
|
"emoji": "☁️",
|
||||||
|
"wind": {
|
||||||
|
"arrow": "↘️",
|
||||||
|
"direction": "NW",
|
||||||
|
"speed": "20.2 km/h"
|
||||||
|
},
|
||||||
|
"rainProbability": "0%"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
As in the previous examples, you can append the `i` query parameter to get results
|
||||||
|
in imperial units(**tip**: you can mix both parameter using `&`).
|
||||||
|
|
||||||
## Moon
|
## Moon
|
||||||
|
|
||||||
The `/moon` endpoint provides the current moon phase and its emoji representation:
|
The `/moon` endpoint provides the current moon phase and its emoji representation:
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ services:
|
|||||||
build: .
|
build: .
|
||||||
container_name: "zephyr"
|
container_name: "zephyr"
|
||||||
environment:
|
environment:
|
||||||
ZEPHYR_ADDR: 127.0.0.1 # Listen address
|
ZEPHYR_ADDR: 0.0.0.0 # Listen address
|
||||||
ZEPHYR_PORT: 3000 # Listen port
|
ZEPHYR_PORT: 3000 # Listen port
|
||||||
ZEPHYR_TOKEN: "" # OpenWeatherMap API Key
|
ZEPHYR_TOKEN: "" # OpenWeatherMap API Key
|
||||||
ZEPHYR_CACHE_TTL: 3 # Cache time-to-live in hour
|
ZEPHYR_CACHE_TTL: 3 # Cache time-to-live in hour
|
||||||
|
|||||||
@@ -58,20 +58,46 @@ func fmtWind(windSpeed string, isImperial bool) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func fmtKey(key string) string {
|
func fmtKey(key string) string {
|
||||||
// Format cache/database keys by replacing whitespaces with '+' token
|
// Cache/database key is formatted by:
|
||||||
// and making them uppercase
|
// 1. Removing leading and trailing whitespaces
|
||||||
return strings.ToUpper(strings.ReplaceAll(key, " ", "+"))
|
// 2. Replacing in-between spaces using the '+' token
|
||||||
|
// 3. Making the key uppercase
|
||||||
|
return strings.ToUpper(strings.ReplaceAll(strings.TrimSpace(key), " ", "+"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func deepCopyForecast(original types.Forecast) types.Forecast {
|
func fmtDailyForecast(forecast *types.DailyForecast, isImperial bool) {
|
||||||
// Copy the outer structure
|
for idx := range forecast.Forecast {
|
||||||
fc_copy := original
|
val := &forecast.Forecast[idx]
|
||||||
|
val.Min = fmtTemperature(val.Min, isImperial)
|
||||||
|
val.Max = fmtTemperature(val.Max, isImperial)
|
||||||
|
val.FeelsLike = fmtTemperature(val.FeelsLike, isImperial)
|
||||||
|
val.Wind.Speed = fmtWind(val.Wind.Speed, isImperial)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Allocate enough space
|
func fmtHourlyForecast(forecast *types.HourlyForecast, isImperial bool) {
|
||||||
fc_copy.Forecast = make([]types.ForecastEntity, len(original.Forecast))
|
for idx := range forecast.Forecast {
|
||||||
|
val := &forecast.Forecast[idx]
|
||||||
|
val.Temperature = fmtTemperature(val.Temperature, isImperial)
|
||||||
|
val.Wind.Speed = fmtWind(val.Wind.Speed, isImperial)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Copy inner structure
|
func deepCopyForecast[T types.DailyForecast | types.HourlyForecast](original T) T {
|
||||||
copy(fc_copy.Forecast, original.Forecast)
|
var fc_copy T
|
||||||
|
|
||||||
|
switch any(original).(type) {
|
||||||
|
case types.DailyForecast:
|
||||||
|
orig := any(original).(types.DailyForecast)
|
||||||
|
fc_copy = any(types.DailyForecast{
|
||||||
|
Forecast: append([]types.DailyForecastEntity(nil), orig.Forecast...),
|
||||||
|
}).(T)
|
||||||
|
case types.HourlyForecast:
|
||||||
|
orig := any(original).(types.HourlyForecast)
|
||||||
|
fc_copy = any(types.HourlyForecast{
|
||||||
|
Forecast: append([]types.HourlyForecastEntity(nil), orig.Forecast...),
|
||||||
|
}).(T)
|
||||||
|
}
|
||||||
|
|
||||||
return fc_copy
|
return fc_copy
|
||||||
}
|
}
|
||||||
@@ -93,6 +119,8 @@ func GetWeather(res http.ResponseWriter, req *http.Request, cache *types.Cache[t
|
|||||||
if found {
|
if found {
|
||||||
// Format weather object and then return it
|
// Format weather object and then return it
|
||||||
cachedValue.Temperature = fmtTemperature(cachedValue.Temperature, isImperial)
|
cachedValue.Temperature = fmtTemperature(cachedValue.Temperature, isImperial)
|
||||||
|
cachedValue.Min = fmtTemperature(cachedValue.Min, isImperial)
|
||||||
|
cachedValue.Max = fmtTemperature(cachedValue.Max, isImperial)
|
||||||
cachedValue.FeelsLike = fmtTemperature(cachedValue.FeelsLike, isImperial)
|
cachedValue.FeelsLike = fmtTemperature(cachedValue.FeelsLike, isImperial)
|
||||||
|
|
||||||
jsonValue(res, cachedValue)
|
jsonValue(res, cachedValue)
|
||||||
@@ -119,6 +147,8 @@ func GetWeather(res http.ResponseWriter, req *http.Request, cache *types.Cache[t
|
|||||||
|
|
||||||
// Format weather object and then return it
|
// Format weather object and then return it
|
||||||
weather.Temperature = fmtTemperature(weather.Temperature, isImperial)
|
weather.Temperature = fmtTemperature(weather.Temperature, isImperial)
|
||||||
|
weather.Min = fmtTemperature(weather.Min, isImperial)
|
||||||
|
weather.Max = fmtTemperature(weather.Max, isImperial)
|
||||||
weather.FeelsLike = fmtTemperature(weather.FeelsLike, isImperial)
|
weather.FeelsLike = fmtTemperature(weather.FeelsLike, isImperial)
|
||||||
|
|
||||||
jsonValue(res, weather)
|
jsonValue(res, weather)
|
||||||
@@ -219,7 +249,13 @@ func GetWind(res http.ResponseWriter, req *http.Request, cache *types.Cache[type
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetForecast(res http.ResponseWriter, req *http.Request, cache *types.Cache[types.Forecast], vars *types.Variables) {
|
func GetForecast(
|
||||||
|
res http.ResponseWriter,
|
||||||
|
req *http.Request,
|
||||||
|
dCache *types.Cache[types.DailyForecast],
|
||||||
|
hCache *types.Cache[types.HourlyForecast],
|
||||||
|
vars *types.Variables,
|
||||||
|
) {
|
||||||
if req.Method != http.MethodGet {
|
if req.Method != http.MethodGet {
|
||||||
jsonError(res, "error", "method not allowed", http.StatusMethodNotAllowed)
|
jsonError(res, "error", "method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
@@ -232,49 +268,54 @@ func GetForecast(res http.ResponseWriter, req *http.Request, cache *types.Cache[
|
|||||||
// Check whether the 'i' parameter(imperial mode) is specified
|
// Check whether the 'i' parameter(imperial mode) is specified
|
||||||
isImperial := req.URL.Query().Has("i")
|
isImperial := req.URL.Query().Has("i")
|
||||||
|
|
||||||
cachedValue, found := cache.GetEntry(fmtKey(cityName), vars.TimeToLive)
|
// Check whether the 'h' parameter(hourly forecast) is specified
|
||||||
|
if req.URL.Query().Has("h") {
|
||||||
|
cachedValue, found := hCache.GetEntry(fmtKey(cityName), vars.TimeToLive)
|
||||||
if found {
|
if found {
|
||||||
forecast := deepCopyForecast(cachedValue)
|
forecast := deepCopyForecast(cachedValue)
|
||||||
|
fmtHourlyForecast(&forecast, isImperial)
|
||||||
// Format forecast object and then return it
|
jsonValue(res, forecast)
|
||||||
for idx := range forecast.Forecast {
|
return
|
||||||
val := &forecast.Forecast[idx]
|
|
||||||
|
|
||||||
val.Min = fmtTemperature(val.Min, isImperial)
|
|
||||||
val.Max = fmtTemperature(val.Max, isImperial)
|
|
||||||
val.FeelsLike = fmtTemperature(val.FeelsLike, isImperial)
|
|
||||||
val.Wind.Speed = fmtWind(val.Wind.Speed, isImperial)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonValue(res, forecast)
|
|
||||||
} else {
|
|
||||||
// Get city coordinates
|
|
||||||
city, err := model.GetCoordinates(cityName, vars.Token)
|
city, err := model.GetCoordinates(cityName, vars.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get city forecast
|
forecast, err := model.GetForecast[types.HourlyForecast](&city, vars.Token, model.HOURLY)
|
||||||
forecast, err := model.GetForecast(&city, vars.Token)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add result to cache
|
hCache.AddEntry(deepCopyForecast(forecast), fmtKey(cityName))
|
||||||
cache.AddEntry(deepCopyForecast(forecast), fmtKey(cityName))
|
fmtHourlyForecast(&forecast, isImperial)
|
||||||
|
jsonValue(res, forecast)
|
||||||
// Format forecast object and then return it
|
} else { // Daily forecast(default)
|
||||||
for idx := range forecast.Forecast {
|
cachedValue, found := dCache.GetEntry(fmtKey(cityName), vars.TimeToLive)
|
||||||
val := &forecast.Forecast[idx]
|
if found {
|
||||||
|
forecast := deepCopyForecast(cachedValue)
|
||||||
val.Min = fmtTemperature(val.Min, isImperial)
|
fmtDailyForecast(&forecast, isImperial)
|
||||||
val.Max = fmtTemperature(val.Max, isImperial)
|
jsonValue(res, forecast)
|
||||||
val.FeelsLike = fmtTemperature(val.FeelsLike, isImperial)
|
return
|
||||||
val.Wind.Speed = fmtWind(val.Wind.Speed, isImperial)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
city, err := model.GetCoordinates(cityName, vars.Token)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
forecast, err := model.GetForecast[types.DailyForecast](&city, vars.Token, model.DAILY)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dCache.AddEntry(deepCopyForecast(forecast), fmtKey(cityName))
|
||||||
|
fmtDailyForecast(&forecast, isImperial)
|
||||||
jsonValue(res, forecast)
|
jsonValue(res, forecast)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -46,7 +46,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/forecast/", func(res http.ResponseWriter, req *http.Request) {
|
http.HandleFunc("/forecast/", func(res http.ResponseWriter, req *http.Request) {
|
||||||
controller.GetForecast(res, req, &cache.ForecastCache, &vars)
|
controller.GetForecast(res, req, &cache.DailyForecastCache, &cache.HourlyForecastCache, &vars)
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/moon", func(res http.ResponseWriter, req *http.Request) {
|
http.HandleFunc("/moon", func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -11,7 +12,14 @@ import (
|
|||||||
"github.com/ceticamarco/zephyr/types"
|
"github.com/ceticamarco/zephyr/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Structures representing the JSON response
|
type FCType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
DAILY FCType = iota
|
||||||
|
HOURLY
|
||||||
|
)
|
||||||
|
|
||||||
|
// Structures representing the daily forecast
|
||||||
type dailyRes struct {
|
type dailyRes struct {
|
||||||
Temp struct {
|
Temp struct {
|
||||||
Min float64 `json:"min"`
|
Min float64 `json:"min"`
|
||||||
@@ -27,85 +35,182 @@ type dailyRes struct {
|
|||||||
} `json:"weather"`
|
} `json:"weather"`
|
||||||
WindSpeed float64 `json:"wind_speed"`
|
WindSpeed float64 `json:"wind_speed"`
|
||||||
WindDeg float64 `json:"wind_deg"`
|
WindDeg float64 `json:"wind_deg"`
|
||||||
|
RainProb float64 `json:"pop"`
|
||||||
Timestamp int64 `json:"dt"`
|
Timestamp int64 `json:"dt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type forecastRes struct {
|
type dailyForecastRes struct {
|
||||||
Daily []dailyRes `json:"daily"`
|
Daily []dailyRes `json:"daily"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getForecastEntity(dailyForecast dailyRes) types.ForecastEntity {
|
// Structure representing the hourly forecast
|
||||||
|
type hourlyRes struct {
|
||||||
|
Temperature float64 `json:"temp"`
|
||||||
|
Weather []struct {
|
||||||
|
Title string `json:"main"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Icon string `json:"icon"`
|
||||||
|
} `json:"weather"`
|
||||||
|
WindSpeed float64 `json:"wind_speed"`
|
||||||
|
WindDeg float64 `json:"wind_deg"`
|
||||||
|
RainProb float64 `json:"pop"`
|
||||||
|
Timestamp int64 `json:"dt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type hourlyForecastRes struct {
|
||||||
|
Hourly []hourlyRes `json:"hourly"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getForecastEntity[T types.DailyForecastEntity | types.HourlyForecastEntity, K dailyRes | hourlyRes](forecast K) T {
|
||||||
|
switch fc := any(forecast).(type) {
|
||||||
|
case dailyRes:
|
||||||
// Format UNIX timestamp as 'YYYY-MM-DD'
|
// Format UNIX timestamp as 'YYYY-MM-DD'
|
||||||
utcTime := time.Unix(int64(dailyForecast.Timestamp), 0)
|
utcTime := time.Unix(int64(fc.Timestamp), 0)
|
||||||
weatherDate := types.ZephyrDate{Date: utcTime.UTC()}
|
weatherDate := types.ZephyrDate{Date: utcTime.UTC()}
|
||||||
|
|
||||||
// Set condition accordingly to weather description
|
// Set condition accordingly to weather description
|
||||||
var condition string
|
var condition string
|
||||||
switch dailyForecast.Weather[0].Description {
|
switch fc.Weather[0].Description {
|
||||||
case "few clouds":
|
case "few clouds":
|
||||||
condition = "SunWithCloud"
|
condition = "SunWithCloud"
|
||||||
case "broken clouds":
|
case "broken clouds":
|
||||||
condition = "CloudWithSun"
|
condition = "CloudWithSun"
|
||||||
default:
|
default:
|
||||||
condition = dailyForecast.Weather[0].Title
|
condition = fc.Weather[0].Title
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get emoji from weather condition
|
// Get emoji from weather condition
|
||||||
isNight := strings.HasSuffix(dailyForecast.Weather[0].Icon, "n")
|
emoji := GetEmoji(condition, false)
|
||||||
emoji := GetEmoji(condition, isNight)
|
|
||||||
|
|
||||||
// Get cardinal direction and wind arrow
|
// Get cardinal direction and wind arrow
|
||||||
windDirection, windArrow := GetCardinalDir(dailyForecast.WindDeg)
|
windDirection, windArrow := GetCardinalDir(fc.WindDeg)
|
||||||
|
|
||||||
return types.ForecastEntity{
|
// Round rain probability to the nearest integer
|
||||||
|
rainProb := int64(math.Round(fc.RainProb * 100))
|
||||||
|
|
||||||
|
return any(types.DailyForecastEntity{
|
||||||
Date: weatherDate,
|
Date: weatherDate,
|
||||||
Min: strconv.FormatFloat(dailyForecast.Temp.Min, 'f', -1, 64),
|
Min: strconv.FormatFloat(fc.Temp.Min, 'f', -1, 64),
|
||||||
Max: strconv.FormatFloat(dailyForecast.Temp.Max, 'f', -1, 64),
|
Max: strconv.FormatFloat(fc.Temp.Max, 'f', -1, 64),
|
||||||
Condition: dailyForecast.Weather[0].Title,
|
Condition: fc.Weather[0].Title,
|
||||||
Emoji: emoji,
|
Emoji: emoji,
|
||||||
FeelsLike: strconv.FormatFloat(dailyForecast.FeelsLike.Day, 'f', -1, 64),
|
FeelsLike: strconv.FormatFloat(fc.FeelsLike.Day, 'f', -1, 64),
|
||||||
Wind: types.Wind{
|
Wind: types.Wind{
|
||||||
Arrow: windArrow,
|
Arrow: windArrow,
|
||||||
Direction: windDirection,
|
Direction: windDirection,
|
||||||
Speed: strconv.FormatFloat(dailyForecast.WindSpeed, 'f', 2, 64),
|
Speed: strconv.FormatFloat(fc.WindSpeed, 'f', 2, 64),
|
||||||
},
|
},
|
||||||
|
RainProb: strconv.FormatInt(rainProb, 10) + "%",
|
||||||
|
}).(T)
|
||||||
|
case hourlyRes:
|
||||||
|
// Format UNIX timestamp as 'YYYY-MM-DD'
|
||||||
|
utcTime := time.Unix(int64(fc.Timestamp), 0)
|
||||||
|
weatherTime := types.ZephyrTime{Time: utcTime.UTC()}
|
||||||
|
|
||||||
|
// Set condition accordingly to weather condition
|
||||||
|
var condition string
|
||||||
|
switch fc.Weather[0].Description {
|
||||||
|
case "few clouds":
|
||||||
|
condition = "SunWithCloud"
|
||||||
|
case "broken clouds":
|
||||||
|
condition = "CloudWithSun"
|
||||||
|
default:
|
||||||
|
condition = fc.Weather[0].Title
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get emoji from weather condition
|
||||||
|
isNight := strings.HasSuffix(fc.Weather[0].Icon, "n")
|
||||||
|
emoji := GetEmoji(condition, isNight)
|
||||||
|
|
||||||
|
// Get cardinal direction and wind arrow
|
||||||
|
windDirection, windArrow := GetCardinalDir(fc.WindDeg)
|
||||||
|
|
||||||
|
// Round rain probability to the nearest integer
|
||||||
|
rainProb := int64(math.Round(fc.RainProb * 100))
|
||||||
|
|
||||||
|
return any(types.HourlyForecastEntity{
|
||||||
|
Time: weatherTime,
|
||||||
|
Temperature: strconv.FormatFloat(fc.Temperature, 'f', -1, 64),
|
||||||
|
Condition: fc.Weather[0].Title,
|
||||||
|
Emoji: emoji,
|
||||||
|
Wind: types.Wind{
|
||||||
|
Arrow: windArrow,
|
||||||
|
Direction: windDirection,
|
||||||
|
Speed: strconv.FormatFloat(fc.WindSpeed, 'f', 2, 64),
|
||||||
|
},
|
||||||
|
RainProb: strconv.FormatInt(rainProb, 10) + "%",
|
||||||
|
}).(T)
|
||||||
|
default:
|
||||||
|
var zero T
|
||||||
|
return zero
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetForecast(city *types.City, apiKey string) (types.Forecast, error) {
|
func GetForecast[T types.DailyForecast | types.HourlyForecast](city *types.City, apiKey string, fcType FCType) (T, error) {
|
||||||
url, err := url.Parse(WTR_URL)
|
var forecast T
|
||||||
|
|
||||||
|
baseURL, err := url.Parse(WTR_URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.Forecast{}, err
|
var zero T
|
||||||
|
return zero, err
|
||||||
}
|
}
|
||||||
|
|
||||||
params := url.Query()
|
params := baseURL.Query()
|
||||||
params.Set("lat", strconv.FormatFloat(city.Lat, 'f', -1, 64))
|
params.Set("lat", strconv.FormatFloat(city.Lat, 'f', -1, 64))
|
||||||
params.Set("lon", strconv.FormatFloat(city.Lon, 'f', -1, 64))
|
params.Set("lon", strconv.FormatFloat(city.Lon, 'f', -1, 64))
|
||||||
params.Set("appid", apiKey)
|
params.Set("appid", apiKey)
|
||||||
params.Set("units", "metric")
|
params.Set("units", "metric")
|
||||||
|
|
||||||
|
switch fcType {
|
||||||
|
case DAILY:
|
||||||
params.Set("exclude", "current,minutely,hourly,alerts")
|
params.Set("exclude", "current,minutely,hourly,alerts")
|
||||||
|
baseURL.RawQuery = params.Encode()
|
||||||
|
|
||||||
url.RawQuery = params.Encode()
|
res, err := http.Get(baseURL.String())
|
||||||
|
|
||||||
res, err := http.Get(url.String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.Forecast{}, err
|
var zero T
|
||||||
|
return zero, err
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
var forecastRes forecastRes
|
var dailyRes dailyForecastRes
|
||||||
if err := json.NewDecoder(res.Body).Decode(&forecastRes); err != nil {
|
if err := json.NewDecoder(res.Body).Decode(&dailyRes); err != nil {
|
||||||
return types.Forecast{}, err
|
var zero T
|
||||||
|
return zero, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// We skip the first element since it represents the current day
|
// We skip the first element since it represents the current day
|
||||||
// We ignore forecasts after the fourth day
|
// We also ignore forecasts after the fourth day
|
||||||
var forecast []types.ForecastEntity
|
var forecastEntities []types.DailyForecastEntity
|
||||||
for _, val := range forecastRes.Daily[1:5] {
|
for _, val := range dailyRes.Daily[1:5] {
|
||||||
forecast = append(forecast, getForecastEntity(val))
|
forecastEntities = append(forecastEntities, getForecastEntity[types.DailyForecastEntity](val))
|
||||||
|
}
|
||||||
|
forecast = any(types.DailyForecast{Forecast: forecastEntities}).(T)
|
||||||
|
|
||||||
|
case HOURLY:
|
||||||
|
params.Set("exclude", "current,minutely,daily,alerts")
|
||||||
|
baseURL.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
res, err := http.Get(baseURL.String())
|
||||||
|
if err != nil {
|
||||||
|
var zero T
|
||||||
|
return zero, err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
var hourlyRes hourlyForecastRes
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&hourlyRes); err != nil {
|
||||||
|
var zero T
|
||||||
|
return zero, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return types.Forecast{
|
// Get hourly forecast of a time window of 9 hours
|
||||||
Forecast: forecast,
|
var forecastEntries []types.HourlyForecastEntity
|
||||||
}, nil
|
for _, val := range hourlyRes.Hourly[:9] {
|
||||||
|
forecastEntries = append(forecastEntries, getForecastEntity[types.HourlyForecastEntity](val))
|
||||||
|
}
|
||||||
|
forecast = any(types.HourlyForecast{Forecast: forecastEntries}).(T)
|
||||||
|
}
|
||||||
|
|
||||||
|
return any(forecast).(T), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func GetCoordinates(cityName string, apiKey string) (types.City, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(geoArr) == 0 {
|
if len(geoArr) == 0 {
|
||||||
return types.City{}, errors.New("Cannot find this city")
|
return types.City{}, errors.New("cannot find this city")
|
||||||
}
|
}
|
||||||
|
|
||||||
return types.City{
|
return types.City{
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
func GetStatistics(cityName string, statDB *types.StatDB) (types.StatResult, error) {
|
func GetStatistics(cityName string, statDB *types.StatDB) (types.StatResult, error) {
|
||||||
// Check whether there are sufficient and updated records for the given location
|
// Check whether there are sufficient and updated records for the given location
|
||||||
if statDB.IsKeyInvalid(cityName) {
|
if statDB.IsKeyInvalid(cityName) {
|
||||||
return types.StatResult{}, errors.New("Insufficient or outdated data to perform statistical analysis")
|
return types.StatResult{}, errors.New("insufficient or outdated data to perform statistical analysis")
|
||||||
}
|
}
|
||||||
|
|
||||||
extractTemps := func(weatherArr []types.Weather) ([]float64, error) {
|
extractTemps := func(weatherArr []types.Weather) ([]float64, error) {
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
|
|||||||
params.Set("lon", strconv.FormatFloat(city.Lon, 'f', -1, 64))
|
params.Set("lon", strconv.FormatFloat(city.Lon, 'f', -1, 64))
|
||||||
params.Set("appid", apiKey)
|
params.Set("appid", apiKey)
|
||||||
params.Set("units", "metric")
|
params.Set("units", "metric")
|
||||||
params.Set("exclude", "minutely,hourly,daily,alerts")
|
params.Set("exclude", "minutely,hourly,alerts")
|
||||||
|
|
||||||
url.RawQuery = params.Encode()
|
url.RawQuery = params.Encode()
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
|
|||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
// Structure representing the JSON response
|
// Structure representing the *current* weather
|
||||||
type WeatherRes struct {
|
type WeatherRes struct {
|
||||||
Current struct {
|
Current struct {
|
||||||
FeelsLike float64 `json:"feels_like"`
|
FeelsLike float64 `json:"feels_like"`
|
||||||
@@ -75,6 +75,12 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
|
|||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
} `json:"weather"`
|
} `json:"weather"`
|
||||||
} `json:"current"`
|
} `json:"current"`
|
||||||
|
Daily []struct {
|
||||||
|
Temp struct {
|
||||||
|
Min float64 `json:"min"`
|
||||||
|
Max float64 `json:"max"`
|
||||||
|
} `json:"temp"`
|
||||||
|
} `json:"daily"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var weather WeatherRes
|
var weather WeatherRes
|
||||||
@@ -104,6 +110,8 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
|
|||||||
return types.Weather{
|
return types.Weather{
|
||||||
Date: weatherDate,
|
Date: weatherDate,
|
||||||
Temperature: strconv.FormatFloat(weather.Current.Temperature, 'f', -1, 64),
|
Temperature: strconv.FormatFloat(weather.Current.Temperature, 'f', -1, 64),
|
||||||
|
Min: strconv.FormatFloat(weather.Daily[0].Temp.Min, 'f', -1, 64),
|
||||||
|
Max: strconv.FormatFloat(weather.Daily[0].Temp.Max, 'f', -1, 64),
|
||||||
FeelsLike: strconv.FormatFloat(weather.Current.FeelsLike, 'f', -1, 64),
|
FeelsLike: strconv.FormatFloat(weather.Current.FeelsLike, 'f', -1, 64),
|
||||||
Condition: weather.Current.Weather[0].Title,
|
Condition: weather.Current.Weather[0].Title,
|
||||||
Emoji: emoji,
|
Emoji: emoji,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
// cacheType, representing the abstract value of a CacheEntity
|
// cacheType, representing the abstract value of a CacheEntity
|
||||||
type cacheType interface {
|
type cacheType interface {
|
||||||
Weather | Metrics | Wind | Forecast | Moon
|
Weather | Metrics | Wind | DailyForecast | HourlyForecast | Moon
|
||||||
}
|
}
|
||||||
|
|
||||||
// CacheEntity, representing the value of the cache
|
// CacheEntity, representing the value of the cache
|
||||||
@@ -26,7 +26,8 @@ type Caches struct {
|
|||||||
WeatherCache Cache[Weather]
|
WeatherCache Cache[Weather]
|
||||||
MetricsCache Cache[Metrics]
|
MetricsCache Cache[Metrics]
|
||||||
WindCache Cache[Wind]
|
WindCache Cache[Wind]
|
||||||
ForecastCache Cache[Forecast]
|
DailyForecastCache Cache[DailyForecast]
|
||||||
|
HourlyForecastCache Cache[HourlyForecast]
|
||||||
MoonCache CacheEntity[Moon]
|
MoonCache CacheEntity[Moon]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +36,8 @@ func InitCache() *Caches {
|
|||||||
WeatherCache: Cache[Weather]{Data: make(map[string]CacheEntity[Weather])},
|
WeatherCache: Cache[Weather]{Data: make(map[string]CacheEntity[Weather])},
|
||||||
MetricsCache: Cache[Metrics]{Data: make(map[string]CacheEntity[Metrics])},
|
MetricsCache: Cache[Metrics]{Data: make(map[string]CacheEntity[Metrics])},
|
||||||
WindCache: Cache[Wind]{Data: make(map[string]CacheEntity[Wind])},
|
WindCache: Cache[Wind]{Data: make(map[string]CacheEntity[Wind])},
|
||||||
ForecastCache: Cache[Forecast]{Data: make(map[string]CacheEntity[Forecast])},
|
DailyForecastCache: Cache[DailyForecast]{Data: make(map[string]CacheEntity[DailyForecast])},
|
||||||
|
HourlyForecastCache: Cache[HourlyForecast]{Data: make(map[string]CacheEntity[HourlyForecast])},
|
||||||
MoonCache: CacheEntity[Moon]{element: Moon{}, timestamp: time.Time{}},
|
MoonCache: CacheEntity[Moon]{element: Moon{}, timestamp: time.Time{}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,3 +33,32 @@ func (date ZephyrDate) MarshalJSON() ([]byte, error) {
|
|||||||
|
|
||||||
return []byte("\"" + fmtDate + "\""), nil
|
return []byte("\"" + fmtDate + "\""), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ZephyrTime struct {
|
||||||
|
Time time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *ZephyrTime) UnmarshalJSON(b []byte) error {
|
||||||
|
s := strings.Trim(string(b), "\"")
|
||||||
|
if s == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
t.Time, err = time.Parse("15:04", s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t ZephyrTime) MarshalJSON() ([]byte, error) {
|
||||||
|
if t.Time.IsZero() {
|
||||||
|
return []byte("\"\""), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fmtTime := t.Time.Format("3:04 PM")
|
||||||
|
|
||||||
|
return []byte("\"" + fmtTime + "\""), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
// The ForecastEntity data type, representing the weather forecast
|
// The DailyForecastEntity data type, representing the weather forecast
|
||||||
// of a single day
|
// of a single day
|
||||||
type ForecastEntity struct {
|
type DailyForecastEntity struct {
|
||||||
Date ZephyrDate `json:"date"`
|
Date ZephyrDate `json:"date"`
|
||||||
Min string `json:"min"`
|
Min string `json:"min"`
|
||||||
Max string `json:"max"`
|
Max string `json:"max"`
|
||||||
@@ -10,9 +10,26 @@ type ForecastEntity struct {
|
|||||||
Emoji string `json:"emoji"`
|
Emoji string `json:"emoji"`
|
||||||
FeelsLike string `json:"feelsLike"`
|
FeelsLike string `json:"feelsLike"`
|
||||||
Wind Wind `json:"wind"`
|
Wind Wind `json:"wind"`
|
||||||
|
RainProb string `json:"rainProbability"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Forecast data type, representing a set of ForecastEntity
|
// The DailyForecast data type, representing a set of DailyForecastEntity
|
||||||
type Forecast struct {
|
type DailyForecast struct {
|
||||||
Forecast []ForecastEntity `json:"forecast"`
|
Forecast []DailyForecastEntity `json:"forecast"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// The HourlyForecastEntity data type, representing the weather forecast
|
||||||
|
// of a single hour
|
||||||
|
type HourlyForecastEntity struct {
|
||||||
|
Time ZephyrTime `json:"time"`
|
||||||
|
Temperature string `json:"temperature"`
|
||||||
|
Condition string `json:"condition"`
|
||||||
|
Emoji string `json:"emoji"`
|
||||||
|
Wind Wind `json:"wind"`
|
||||||
|
RainProb string `json:"rainProbability"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// The HourlyForecast data type, representing a set of HourlyForecastEntity
|
||||||
|
type HourlyForecast struct {
|
||||||
|
Forecast []HourlyForecastEntity `json:"forecast"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ package types
|
|||||||
type Weather struct {
|
type Weather struct {
|
||||||
Date ZephyrDate `json:"date"`
|
Date ZephyrDate `json:"date"`
|
||||||
Temperature string `json:"temperature"`
|
Temperature string `json:"temperature"`
|
||||||
|
Min string `json:"min"`
|
||||||
|
Max string `json:"max"`
|
||||||
Condition string `json:"condition"`
|
Condition string `json:"condition"`
|
||||||
FeelsLike string `json:"feelsLike"`
|
FeelsLike string `json:"feelsLike"`
|
||||||
Emoji string `json:"emoji"`
|
Emoji string `json:"emoji"`
|
||||||
|
|||||||
Reference in New Issue
Block a user