예제 #1
0
func getUserLocation(conn *sse.Conn) {
	response, err := http.Get("http://freegeoip.net/json/" + ip)
	if err != nil {
		fmt.Printf("Error occured: %s", err)
	} else {
		defer response.Body.Close()
		contents, err := ioutil.ReadAll(response.Body)
		res := Location{}
		json.Unmarshal(contents, &res)
		if err != nil {
			fmt.Printf("Error occured: %s", err)
		}
		latitude = fmt.Sprintf("%f", res.Latitude)
		longitude = fmt.Sprintf("%f", res.Longitude)
		conn.WriteStringEvent("location", fmt.Sprintf("Location: %s, %s", res.City, res.Region))
	}
}
예제 #2
0
func updateCurrentWeather(conn *sse.Conn) {
	apikey := "INSERTAPIKEYHERE"
	response, err := http.Get("https://api.forecast.io/forecast/" + apikey + "/" + latitude + "," + longitude + "?units=auto")
	if err != nil {
		fmt.Printf("Error occured: %s", err)
	} else {
		defer response.Body.Close()
		contents, err := ioutil.ReadAll(response.Body)
		cur := Forecast{}
		json.Unmarshal(contents, &cur)
		if err != nil {
			fmt.Printf("Error occured: %s", err)
		}
		conn.WriteStringEvent("time", fmt.Sprintf("Last updated: %s", time.Now().Format("15:04:05 MST")))
		conn.WriteStringEvent("summary", fmt.Sprintf("Conditions:  %s", cur.Currently.Summary))
		conn.WriteStringEvent("temperature", fmt.Sprintf("Temperature: %v °F", cur.Currently.Temperature))
		var divTag string
		for i := 1; i < 8; i++ {
			divTag = fmt.Sprintf("%v", i)
			conn.WriteStringEvent(divTag, fmt.Sprintf("+%s day(s): %v °F", divTag, averageTemperature(cur.Daily.Data[i].TemperatureMin, cur.Daily.Data[i].TemperatureMax)))
		}
	}
}