func weatherLookup(msg commands.Message, ret commands.MessageFunc) string { geoAddr, err := util.GetCoordinates(msg.Params[1:]) if err != nil { return "I couldn't track down that location!" } apiKey, avail := config.Get("WUNDERGROUND_API") if avail != true { return "" } url := fmt.Sprintf("http://api.wunderground.com/api/%s/conditions/q/%v,%v.json", apiKey, geoAddr.Lat, geoAddr.Long) data, err := util.Fetch(url) if err != nil { return "Unable to lookup weather" } var conditions weatherConditions if err := json.Unmarshal(data, &conditions); err != nil { return "Weather Underground gave me a bad response" } response := conditions.Current if response.Weather == "" { return "I couldn't find that location -- try again!" } location := fmt.Sprintf("%s (%s)", response.Location.Full, response.StationID) return fmt.Sprintf("%s is: %s - %s with %s humidity | Wind: %s | %s precip. today", location, response.Weather, response.TempString, response.RelHumidity, response.WindString, response.PrecipString) }
func fetchForecast(msg commands.Message, ret commands.MessageFunc) string { geoAddr, err := util.GetCoordinates(msg.Params[1:]) if err != nil { return "I couldn't track down that location!" } apikey, avail := config.Get("FORECASTIO_API") if avail != true { return "" } lat := strconv.FormatFloat(geoAddr.Lat, 'f', -1, 64) long := strconv.FormatFloat(geoAddr.Long, 'f', -1, 64) f, err := forecast.Get(apikey, lat, long, "now", forecast.US) if err != nil { return "Unable to fetch forecast!" } out := fmt.Sprintf("Forecast: %s", f.Daily.Summary) for i := 0; i < int(math.Min(float64(len(f.Daily.Data)), 3.0)); i++ { data := f.Daily.Data[i] out = fmt.Sprintf("%s | %s: %s [high: %.0fF, low: %.0fF]", out, time.Unix(int64(data.Time), 0).Format("Mon Jan 2"), data.Summary, data.TemperatureMax, data.TemperatureMin) if data.PrecipType != "" { out = fmt.Sprintf("%s; [%.0f%% of %s]", out, data.PrecipProbability*100.0, data.PrecipType) } } return out }
func walkscoreLookup(msg commands.Message, ret commands.MessageFunc) string { walkscore, avail := config.Get("WALKSCORE_API") if avail != true { return "" } geoAddr, err := util.GetCoordinates(msg.Params[1:]) if err != nil { return "Not found!" } lat := geoAddr.Lat long := geoAddr.Long addr := url.QueryEscape(geoAddr.FormattedAddress) walkscoreURL := fmt.Sprintf("http://api.walkscore.com/score?format=json&address=%v&lat=%v&lon=%v&wsapikey=%v", addr, lat, long, walkscore) wdata, err := util.Fetch(walkscoreURL) if err != nil { return "Error fetching walkscore data" } var ws walkScoreData if err := json.Unmarshal(wdata, &ws); err != nil { return "Walkscore gave me a bad response!" } if ws.Status != 1 { return "I couldn't find that in Walkscore's database" } return fmt.Sprintf("%s is a %s with a walkscore of %v", geoAddr.FormattedAddress, ws.Description, ws.Walkscore) }