~handlerug/handlebot

ref: f4383b0c2a75beb6b8607536aebba6269868592c handlebot/weather/weather.go -rw-r--r-- 4.8 KiB
f4383b0cUmar Getagazov weather: drop duplicate location string stems a month ago
                                                                                
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package weather

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
)

var ErrQuota = errors.New("Sorry, the API key seems to have reached its max quota.")

var locFilterRegexp = regexp.MustCompile(` \([^)]+\)`)

type Forecaster struct {
	Credentials APICredentials
	HTTPClient  *http.Client
}

type APICredentials struct {
	OpenWeatherMap string
	MapQuest       string
}

type WeatherInfo struct {
	Current struct {
		Temperature float64 `json:"temp"`
		CloudCover  int     `json:"clouds"`
		Humidity    int     `json:"humidity"`
		WindSpeed   float64 `json:"wind_speed"`
		Conditions  []struct {
			Description string `json:"description"`
		} `json:"weather"`
	} `json:"current"`

	Hourly []struct {
		PrecipProb float64 `json:"pop"`
	} `json:"hourly"`
}

type GeocodeResult struct {
	Info struct {
		StatusCode int      `json:"statuscode"`
		Messages   []string `json:"messages"`
	} `json:"info"`
	Results []struct {
		Locations []struct {
			Street             string `json:"street"`
			AdminArea6         string `json:"adminArea6"`
			AdminArea6Type     string `json:"adminArea6Type"`
			AdminArea5         string `json:"adminArea5"`
			AdminArea5Type     string `json:"adminArea5Type"`
			AdminArea4         string `json:"adminArea4"`
			AdminArea4Type     string `json:"adminArea4Type"`
			AdminArea3         string `json:"adminArea3"`
			AdminArea3Type     string `json:"adminArea3Type"`
			AdminArea1         string `json:"adminArea1"`
			AdminArea1Type     string `json:"adminArea1Type"`
			PostalCode         string `json:"postalCode"`
			GeocodeQualityCode string `json:"geocodeQualityCode"`
			GeocodeQuality     string `json:"geocodeQuality"`
			DragPoint          bool   `json:"dragPoint"`
			SideOfStreet       string `json:"sideOfStreet"`
			LinkID             string `json:"linkId"`
			UnknownInput       string `json:"unknownInput"`
			Type               string `json:"type"`
			LatLng             struct {
				Lat float64 `json:"lat"`
				Lng float64 `json:"lng"`
			} `json:"latLng"`
			DisplayLatLng struct {
				Lat float64 `json:"lat"`
				Lng float64 `json:"lng"`
			} `json:"displayLatLng"`
			MapURL string `json:"mapUrl"`
		} `json:"locations"`
	} `json:"results"`
}

func (f *Forecaster) Weather(ctx context.Context, lat float64, lng float64) (*WeatherInfo, error) {
	q := url.Values{}
	q.Set("lat", strconv.FormatFloat(lat, 'f', 6, 64))
	q.Set("lon", strconv.FormatFloat(lng, 'f', 6, 64))
	q.Set("units", "metric")
	q.Set("exclude", "minutely,daily,alerts")
	q.Set("appid", f.Credentials.OpenWeatherMap)

	req, _ := http.NewRequestWithContext(ctx, "GET", "https://api.openweathermap.org/data/2.5/onecall", nil)
	req.URL.RawQuery = q.Encode()

	resp, err := f.httpClient().Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("%d status code from the API", resp.StatusCode)
	}

	var w WeatherInfo
	if err := json.NewDecoder(resp.Body).Decode(&w); err != nil {
		return nil, fmt.Errorf("failed to parse JSON: %w", err)
	}
	return &w, nil
}

func (f *Forecaster) Geocode(ctx context.Context, query string) (
	lat float64,
	lng float64,
	location string,
	err error,
) {
	q := url.Values{}
	q.Set("location", query)
	q.Set("key", f.Credentials.MapQuest)

	req, _ := http.NewRequestWithContext(ctx, "GET", "https://www.mapquestapi.com/geocoding/v1/address", nil)
	req.URL.RawQuery = q.Encode()

	resp, err := f.httpClient().Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		err = fmt.Errorf("%d status code from the API", resp.StatusCode)
		return
	}

	var result *GeocodeResult
	if err = json.NewDecoder(resp.Body).Decode(&result); err != nil {
		err = fmt.Errorf("failed to parse JSON: %w", err)
		return
	}

	if result.Info.StatusCode == 403 {
		err = &QuotaReachedError{"MapQuest", result.Info.Messages}
		return
	} else if result.Info.StatusCode != 0 {
		err = &GeocodingError{result.Info.StatusCode, result.Info.Messages}
		return
	}

	if len(result.Results) < 1 || len(result.Results[0].Locations) < 1 {
		err = &NoGeocodeResults{}
		return
	}

	var (
		b   strings.Builder
		loc = result.Results[0].Locations[0]

		city     = loc.AdminArea5
		state    = loc.AdminArea3
		country  = loc.AdminArea1
		locStems = map[string]bool{}
	)

	stemKey := func(s string) string {
		return locFilterRegexp.ReplaceAllString(strings.ToLower(s), "")
	}

	lat = loc.LatLng.Lat
	lng = loc.LatLng.Lng
	if city != "" {
		locStems[stemKey(city)] = true
		fmt.Fprintf(&b, "%s, ", city)
	}
	if state != "" && !locStems[stemKey(state)] {
		locStems[stemKey(state)] = true
		fmt.Fprintf(&b, "%s, ", state)
	}
	if !locStems[stemKey(country)] {
		b.WriteString(country)
	}
	location = b.String()
	err = nil
	return
}

func (f *Forecaster) httpClient() *http.Client {
	if f.HTTPClient != nil {
		return f.HTTPClient
	}
	return http.DefaultClient
}