示例#1
0
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
}
示例#2
0
文件: gome.go 项目: bamarni/gome
func weatherHandler(c *AppConfig, w http.ResponseWriter, r *http.Request) (interface{}, error) {
	f, err := forecast.Get(c.ForecastKey, c.Lat, c.Long, "now", forecast.CA)
	if err != nil {
		return nil, err
	}

	weather := Weather{
		Timezone:    f.Timezone,
		Summary:     f.Daily.Data[0].Summary,
		Icon:        f.Daily.Data[0].Icon,
		Temperature: Round(f.Daily.Data[0].TemperatureMax),
		Unit:        "°C",
	}

	return weather, nil
}
示例#3
0
func weather(req []string, e *irc.Event) {
	var reply string
	if len(req) > 1 {
		r := &maps.GeocodingRequest{
			Address: req[1],
		}
		resp, _ := mapsClient.Geocode(context.Background(), r)
		//pretty.Println(resp)
		lat := strconv.FormatFloat(resp[0].Geometry.Location.Lat, 'f', -1, 64)
		lng := strconv.FormatFloat(resp[0].Geometry.Location.Lng, 'f', -1, 64)
		f, err := forecast.Get(weatherApiKey, lat, lng, "now", forecast.CA)
		if err != nil {
			log.Fatal(err)
		}
		reply = fmt.Sprintf("Weather in %s: %s, Humidity: %.2f, Temperature: %.2f Celsius, Wind Speed: %.2f", req[1], f.Currently.Summary, f.Currently.Humidity, f.Currently.Temperature, f.Currently.WindSpeed)
		go botSay(reply)
	}
}
func (w *WeatherController) Get_information(rw http.ResponseWriter, req *http.Request) {
	// my_ip := get_my_ip()
	// query := my_ip
	city_name := "Mysore"
	lat, lng, err := geocoder.Geocode(city_name)
	if err != nil {
		panic(err)
	}
	latitude := strconv.FormatFloat(lat, 'f', 6, 64)
	longitude := strconv.FormatFloat(lng, 'f', 6, 64)

	keybytes, err := ioutil.ReadFile("api_key.txt")
	if err != nil {
		log.Fatal(err)
	}
	key := string(keybytes)
	key = strings.TrimSpace(key)

	f, err := forecast.Get(key, latitude, longitude, "now", forecast.CA)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Temperature: %v*C\n", f.Currently.Temperature)

	daily_weather := []models.DailyWeather{}
	weather := models.DailyWeather{}
	for i := 0; i <= 6; i++ {
		weather = models.DailyWeather{f.Daily.Data[i].WindSpeed, f.Daily.Data[i].Icon, f.Daily.Data[i].Humidity * 100, f.Daily.Data[i].TemperatureMin, f.Daily.Data[i].TemperatureMax}
		daily_weather = append(daily_weather, weather)
		fmt.Printf("Windspeed: %v km/h \n", f.Daily.Data[i].WindSpeed)
		fmt.Printf("Climate: %v\n", f.Daily.Data[i].Icon)
		fmt.Printf("Humidity: %v%% \n", f.Daily.Data[i].Humidity*100)
		fmt.Printf("Minimum Temperature: %v*C \n", f.Daily.Data[i].TemperatureMin)
		fmt.Printf("Maximum Temperature: %v*C \n", f.Daily.Data[i].TemperatureMax)
	}

	float_lat, err := strconv.ParseFloat(latitude, 32)
	if err != nil {
		log.Fatal(err)
	}
	float_long, err := strconv.ParseFloat(longitude, 32)
	if err != nil {
		log.Fatal(err)
	}
	b, err := json.Marshal(models.GeoLocation{
		WeatherForWeek: models.DaysOfWeek{daily_weather[0], daily_weather[1], daily_weather[2], daily_weather[3], daily_weather[4], daily_weather[5], daily_weather[6]},
		Latitude:       float_lat,
		Longitude:      float_long,
		Temperature:    f.Currently.Temperature,
		Humidity:       f.Currently.Humidity,
		Windspeed:      f.Currently.WindSpeed,
		Climate:        f.Daily.Icon,
		City:           city_name,
		Success:        "True",
		Message:        "Windspeed updated.",
	})
	if err != nil {
		log.Fatal(err)
	}
	rw.Header().Set("Content-Type", "application/json")
	rw.Write(b)
}