forked from briandowns/openweathermap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast.go
153 lines (131 loc) · 4.14 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2015 Brian J. Downs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openweathermap
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
)
// ForecastSys area population
type ForecastSys struct {
Population int `json:"population"`
}
// Temperature holds returned termperate sure stats
type Temperature struct {
Day float64 `json:"day"`
Min float64 `json:"min"`
Max float64 `json:"max"`
Night float64 `json:"night"`
Eve float64 `json:"eve"`
Morn float64 `json:"morn"`
}
// City data for given location
type City struct {
ID int `json:"id"`
Name string `json:"name"`
Coord Coordinates `json:"coord"`
Country string `json:"country"`
Population int `json:"population"`
Sys ForecastSys `json:"sys"`
}
// ForecastWeatherList holds specific query data
type ForecastWeatherList struct {
Dt int `json:"dt"`
Temp Temperature `json:"temp"`
Pressure float64 `json:"pressure"`
Humidity int `json:"humidity"`
Weather []Weather `json:"weather"`
Speed float64 `json:"speed"`
Deg int `json:"deg"`
Clouds int `json:"clouds"`
Snow float64 `json:"snow"`
Rain float64 `json:"rain"`
}
// ForecastWeatherData will hold returned data from queries
type ForecastWeatherData struct {
COD int `json:"cod"`
Message string `json:"message"`
City City `json:"city"`
Cnt int `json:"cnt"`
List []ForecastWeatherList `json:"list"`
Unit string
Lang string
Key string
*Settings
}
// NewForecast returns a new HistoricalWeatherData pointer with
// the supplied arguments.
func NewForecast(unit, lang, key string, options ...Option) (*ForecastWeatherData, error) {
unitChoice := strings.ToUpper(unit)
langChoice := strings.ToUpper(lang)
f := &ForecastWeatherData{
Settings: NewSettings(),
}
if ValidDataUnit(unitChoice) {
f.Unit = DataUnits[unitChoice]
} else {
return nil, errUnitUnavailable
}
if ValidLangCode(langChoice) {
f.Lang = langChoice
} else {
return nil, errLangUnavailable
}
f.Key = setKey(key)
if err := setOptions(f.Settings, options); err != nil {
return nil, err
}
return f, nil
}
// DailyByName will provide a forecast for the location given for the
// number of days given.
func (f *ForecastWeatherData) DailyByName(location string, days int) error {
response, err := f.client.Get(fmt.Sprintf(forecastBase, f.Key, fmt.Sprintf("%s=%s", "q", url.QueryEscape(location)), f.Unit, f.Lang, days))
if err != nil {
return err
}
defer response.Body.Close()
if err = json.NewDecoder(response.Body).Decode(&f); err != nil {
return err
}
return nil
}
// DailyByCoordinates will provide a forecast for the coordinates ID give
// for the number of days given.
func (f *ForecastWeatherData) DailyByCoordinates(location *Coordinates, days int) error {
response, err := f.client.Get(fmt.Sprintf(forecastBase, f.Key, fmt.Sprintf("lat=%f&lon=%f", location.Latitude, location.Longitude), f.Unit, f.Lang, days))
if err != nil {
return err
}
defer response.Body.Close()
if err = json.NewDecoder(response.Body).Decode(&f); err != nil {
return err
}
return nil
}
// DailyByID will provide a forecast for the location ID give for the
// number of days given.
func (f *ForecastWeatherData) DailyByID(id, days int) error {
response, err := f.client.Get(fmt.Sprintf(forecastBase, f.Key, fmt.Sprintf("%s=%s", "id", strconv.Itoa(id)), f.Unit, f.Lang, days))
if err != nil {
return err
}
defer response.Body.Close()
if err = json.NewDecoder(response.Body).Decode(&f); err != nil {
return err
}
return nil
}