where

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

commit 4e59772809ec80e2b517c4ca6757fe98c6c24449
parent a3c80e4d1acb73299315f5657c15c769dac079d0
Author: aabacchus <bvnfuller@gmail.com>
Date:   Sat, 27 Mar 2021 03:17:51 +0000

fix: merge cache and new results correctly; don't overwrite with 0,0

Diffstat:
MTODO | 2+-
Mwhere.go | 38+++++++++++++++++++++++++++++---------
2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/TODO b/TODO @@ -1,4 +1,4 @@ -* Don't replace cache with 0,0 coords * Only get locations of opted-in users * Anonymize users who wish * Create webpage (interactive map?) +* Read credentials from file? diff --git a/where.go b/where.go @@ -108,18 +108,30 @@ func main() { fmt.Printf("error unmarshalling ips cache: %s\n", err) os.Exit(1) } - // if we have newer data for a user in results, use that - // so remove duplicates from the cache - for _, res := range results { - for i, c := range cache { - if res.Name == c.Name { - // remove the duplicate - cache = append(cache[:i], cache[i+1:]...) + // make some temporary maps in order to merge the two slices + tmpCache := MarkersMakeMap(cache) + tmpResults := MarkersMakeMap(results) + var out []Marker + // remove duplicates, using the one which isn't 0,0 + for k, val := range tmpResults { + if cachedVal, ok := tmpCache[k]; ok { + if val.Lat == 0 && val.Lng == 0 { + out = append(out, cachedVal) + } else { + out = append(out, val) } + } else { + out = append(out, val) + } + } + // add the cached values which aren't in the new lot + for k, cVal := range tmpCache { + if _, ok := tmpResults[k]; !ok { + out = append(out, cVal) } } - // now add the results to the cache (newer results at the bottom) - results = append(cache, results...) + results = out + fmt.Println(results) } err = MarkersSaveJson(results, "ips.json") if err != nil { @@ -137,6 +149,14 @@ func main() { fmt.Printf("saved static map to %s\n", imageFile) } +func MarkersMakeMap(m []Marker) map[string]Marker { + r := make(map[string]Marker) + for _, k := range m { + r[k.Name] = k + } + return r +} + func MarkersSaveJson(m []Marker, fname string) error { f, err := os.Create(fname) if err != nil {