all repos — paprika @ ed37b272f5791d139f3031bd35c8b3c41592f356

go rewrite of taigabot

plugins/location/geocode.go (view raw)

 1package location
 2
 3import (
 4	"encoding/json"
 5	"fmt"
 6	"net/http"
 7)
 8
 9type LocationInfo struct {
10	Features []struct {
11		Properties struct {
12			Geocoding struct {
13				Label string `json:"label"`
14			} `json:"geocoding"`
15		} `json:"properties"`
16		Geometry struct {
17			Coordinates [2]float64 `json:"coordinates"`
18		} `json:"geometry"`
19	} `json:"features"`
20}
21
22// Get location data (lat,lon) from a given query.
23func GetLocationInfo(query string) (*LocationInfo, error) {
24	url := "https://nominatim.openstreetmap.org/search?q=%s&format=geocodejson&limit=1"
25	r, err := http.Get(fmt.Sprintf(url, query))
26	if err != nil {
27		return nil, err
28	}
29
30	li := LocationInfo{}
31	json.NewDecoder(r.Body).Decode(&li)
32	defer r.Body.Close()
33
34	return &li, nil
35}