-
Notifications
You must be signed in to change notification settings - Fork 1
/
forecast.go
60 lines (54 loc) · 1.94 KB
/
forecast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package bbcweather
import (
"fmt"
"time"
"github.com/codehex/bbcweather/api"
)
type ForecastReport struct {
ForecastLocation Location
DayForecasts []DayForecast
}
type DayForecast struct {
ForecastDate time.Time
IssueDate time.Time
LastUpdated time.Time
WeatherDescription string
MaxTempC int
MaxTempF int
MinTempC int
MinTempF int
ChanceOfRainPercent int
WindSpeedKph int
WindSpeedMph int
WindCategory WindType
}
func GetWeatherForecast(loc Location) (ForecastReport, error) {
apiResponse, err := api.ForecastWeather(loc.ID)
if err != nil {
return ForecastReport{}, fmt.Errorf("failed to get query forecast API: %w", err)
}
result := ForecastReport{ForecastLocation: loc}
result.DayForecasts = []DayForecast{}
for _, dayReport := range apiResponse.Forecasts {
forecastDate, err := time.Parse("2006-01-02", dayReport.Summary.Report.LocalDate)
if err != nil {
return ForecastReport{}, fmt.Errorf("unable to convert forecast date '%s' to time: %w", dayReport.Summary.Report.LocalDate, err)
}
forecast := DayForecast{
ForecastDate: forecastDate,
IssueDate: dayReport.Summary.IssueDate,
LastUpdated: dayReport.Summary.LastUpdated,
WeatherDescription: dayReport.Summary.Report.WeatherTypeText,
MaxTempC: dayReport.Summary.Report.MaxTempC,
MaxTempF: dayReport.Summary.Report.MaxTempF,
MinTempC: dayReport.Summary.Report.MinTempC,
MinTempF: dayReport.Summary.Report.MinTempF,
ChanceOfRainPercent: dayReport.Summary.Report.PrecipitationProbabilityInPercent,
WindSpeedKph: dayReport.Summary.Report.WindSpeedKph,
WindSpeedMph: dayReport.Summary.Report.WindSpeedMph,
WindCategory: GetWindTypeFromSpeed(dayReport.Summary.Report.WindSpeedMph),
}
result.DayForecasts = append(result.DayForecasts, forecast)
}
return result, nil
}