yr/internal/nominatim/nominatim.go
Tim Hårek Andreassen dd25c4e6cb
feat: Add nominatim
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-09-25 22:09:12 +02:00

86 lines
1.4 KiB
Go

package nominatim
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
)
type Nominatim struct {
siteName string
}
func New(siteName string) (*Nominatim, error) {
if siteName == "" {
return nil, fmt.Errorf("`siteName` must be defined.")
}
return &Nominatim{
siteName: siteName,
}, nil
}
func (n *Nominatim) Search(q string) (*SearchResults, error) {
url := fmt.Sprintf("https://nominatim.openstreetmap.org/search?q=%s&format=jsonv2", q)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", n.siteName)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result := SearchResults{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (n *Nominatim) Lookup(q string) (*Coordinates, error) {
r, err := n.Search(q)
if err != nil {
return nil, err
}
if len(*r) == 0 {
return nil, fmt.Errorf("no result")
}
firstResult := (*r)[0]
lon, err := strconv.ParseFloat(*firstResult.Longitude, 64)
if err != nil {
return nil, err
}
lat, err := strconv.ParseFloat(*firstResult.Latitude, 64)
if err != nil {
return nil, err
}
return &Coordinates{
Latitude: lat,
Longitude: lon,
}, nil
}