コード例 #1
0
ファイル: main.go プロジェクト: farazfazli/go-weather
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
ファイル: main.go プロジェクト: farazfazli/go-weather
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)))
		}
	}
}