~handlerug/handlebot

f4383b0c2a75beb6b8607536aebba6269868592c — Umar Getagazov 15 days ago 5c3342e
weather: drop duplicate location string stems
1 files changed, 17 insertions(+), 5 deletions(-)

M weather/weather.go
M weather/weather.go => weather/weather.go +17 -5
@@ 7,12 7,15 @@ import (
	"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


@@ 153,20 156,29 @@ func (f *Forecaster) Geocode(ctx context.Context, query string) (
		b   strings.Builder
		loc = result.Results[0].Locations[0]

		city    = loc.AdminArea5
		state   = loc.AdminArea3
		country = loc.AdminArea1
		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 != "" {
	if state != "" && !locStems[stemKey(state)] {
		locStems[stemKey(state)] = true
		fmt.Fprintf(&b, "%s, ", state)
	}
	b.WriteString(country)
	if !locStems[stemKey(country)] {
		b.WriteString(country)
	}
	location = b.String()
	err = nil
	return