where

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit e1f4f79eb2a31440e7db9868feaabb9971fd57e1
parent 3ab76fb7492e5aaec01670bad9d05b4bba89c89d
Author: aabacchus <ben@bvnf.space>
Date:   Tue, 11 May 2021 01:41:25 +0100

use freegeoip.app for ip locations (merge branch)

Diffstat:
Mwhere.go | 29+++++++++++++----------------
1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/where.go b/where.go @@ -25,7 +25,6 @@ import ( "os" "os/exec" "strings" - "strconv" ) var verbose *bool @@ -167,6 +166,7 @@ func main() { fmt.Printf("error reading ips cache: %s\n", err) os.Exit(1) } + log(fmt.Sprintf("found previous results file %s", cacheFname)) var cache []Marker err = json.Unmarshal(bytes, &cache) if err != nil { @@ -310,42 +310,39 @@ func ipLatLng(apikey, name, ip string, ch chan MarkResponse) { ch <- MarkResponse{Marker{Name: name}, errors.New("no IP provided")} return } - // moving to a libre IP service, so apikey is no longer required - _ = apikey - query := fmt.Sprintf("http://api.ipgeolocationapi.com/geolocate/%s", ip) + query := fmt.Sprintf("https://freegeoip.app/json/%s", ip) resp, err := http.Get(query) if err != nil { ch <- MarkResponse{Marker{Name: name}, err} return } defer resp.Body.Close() + // freegeoip.app will give 403 if we've made more than 15,000 queries per hour. + // Unlikely, yes, but good to be careful. + if resp.StatusCode == 403 { + // this is logged to stderr, so it should be picked up by the cron daemon + fmt.Fprintf(os.Stderr, "The request to freegeoip.app returned %s", resp.Status) + } + bytes, err := ioutil.ReadAll(resp.Body) if err != nil { ch <- MarkResponse{Marker{Name: name}, err} return } place := struct { - Geo struct { - Lat string `json:"latitude_dec"` - Lng string `json:"longitude_dec"` - } `json:"geo"` + Lat float64 `json:"latitude"` + Lng float64 `json:"longitude"` }{} - log("response: ", fmt.Sprintf("%s", bytes)) if err := json.Unmarshal(bytes, &place); err != nil { ch <- MarkResponse{Marker{Name: name}, err} return } - lat, err := strconv.ParseFloat(place.Geo.Lat, 64) - if err != nil { fmt.Fprintf(os.Stderr, "error parsing float from %q: %s\n", place.Geo.Lat, err.Error) } - lng, err := strconv.ParseFloat(place.Geo.Lng, 64) - if err != nil { fmt.Fprintf(os.Stderr, "error parsing float from %q: %s\n", place.Geo.Lat, err.Error) } - ch <- MarkResponse{Marker{ Name: name, - Lat: lat, - Lng: lng, + Lat: place.Lat, + Lng: place.Lng, }, nil} return }