<commit_before>package minimalweather

import (
	"encoding/json"
	"fmt"
	"github.com/elcuervo/geoip"
	"github.com/garyburd/redigo/redis"
	"log"
	"os"
)

const location_prefix = "mw:geolocator:"

var (
	geo_user = os.Getenv("GEOLOCATION_USER")
	geo_key  = os.Getenv("GEOLOCATION_KEY")
)

func ClearGeolocationCache() {
	pattern := fmt.Sprintf("%s*", location_prefix)
	keys, err := redis.Values(c.Do("KEYS", pattern))

	if err != nil {
		log.Println(err)
	}

	for _, key := range keys {
		c.Do("DEL", key)
	}
}

func GetLocation(ip string) chan geoip.Geolocation {
	geo_chann := make(chan geoip.Geolocation)

	key := fmt.Sprintf("%s%s", location_prefix, ip)
	cached_geo, err := c.Do("GET", key)

	if err != nil {
		panic(err)
	}

	go func() {
		if cached_geo != nil {
			var geolocation geoip.Geolocation
			log.Println("Geo from cached")

			str, _ := redis.String(cached_geo, nil)
			bytes := []byte(str)
			json.Unmarshal(bytes, &geolocation)

			geo_chann <- geolocation
		} else {
			log.Println("Geo locating")
			locator := geoip.GeoIP{geo_user, geo_key, true}
			g := locator.FindCity(ip)

			_, err := c.Do("HSET", "mw:stats", "maxmind", g.API.Remaining)
			if err != nil {
				log.Println(err)
			}

			json_response, _ := json.Marshal(g)
			_, err = c.Do("SETEX", key, 200*60, json_response)

			if err != nil {
				panic(err)
			}

			geo_chann <- g

		}<commit_msg>Prevents crashing when the connection is closed... kind of<commit_after>package minimalweather

import (
	"encoding/json"
	"fmt"
	"github.com/elcuervo/geoip"
	"github.com/garyburd/redigo/redis"
	"log"
	"os"
)

const location_prefix = "mw:geolocator:"

var (
	geo_user = os.Getenv("GEOLOCATION_USER")
	geo_key  = os.Getenv("GEOLOCATION_KEY")
)

func ClearGeolocationCache() {
	pattern := fmt.Sprintf("%s*", location_prefix)
	keys, err := redis.Values(c.Do("KEYS", pattern))

	if err != nil {
		log.Println(err)
	}

	for _, key := range keys {
		c.Do("DEL", key)
	}
}

func GetLocation(ip string) chan geoip.Geolocation {
	geo_chann := make(chan geoip.Geolocation)

	key := fmt.Sprintf("%s%s", location_prefix, ip)
	cached_geo, err := c.Do("GET", key)

	if err != nil {
		log.Println(err)
	}

	go func() {
		if cached_geo != nil {
			var geolocation geoip.Geolocation
			log.Println("Geo from cached")

			str, _ := redis.String(cached_geo, nil)
			bytes := []byte(str)
			json.Unmarshal(bytes, &geolocation)

			geo_chann <- geolocation
		} else {
			log.Println("Geo locating")
			locator := geoip.GeoIP{geo_user, geo_key, true}
			g := locator.FindCity(ip)

			_, err := c.Do("HSET", "mw:stats", "maxmind", g.API.Remaining)
			if err != nil {
				log.Println(err)
			}

			json_response, _ := json.Marshal(g)
			_, err = c.Do("SETEX", key, 200*60, json_response)

			if err != nil {
				log.Println(err)
			}

			geo_chann <- g

		}
{'fraction_non_alphanumeric': 0.16197935208259168, 'fraction_numerical': 0.00355998576005696, 'mean_word_length': 3.5915032679738563, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1853406', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1113, 'n_words': 222}