コード例 #1
0
        public WeatherForecast GetForecast(string stationId)
        {
            WeatherForecast fc = new WeatherForecast();
            Uri dailyUri = new Uri(
                string.Format("http://api.wunderground.com/api/{0}/forecast/q/zmw:{1}.json", apiKey, stationId));

            var dailyResp = GetResponse(DataFeatures.forecast, stationId);
            var hourlyResp = GetResponse(DataFeatures.hourly, stationId);

            foreach (var token in dailyResp.SelectToken("forecast.simpleforecast.forecastday"))
            {
                fc.Daily["T-" + fc.Daily.Count] = new DayForecast()
                {
                    day = GetIntValue(token, "date.day"),
                    month = GetIntValue(token, "date.month"),
                    year = GetIntValue(token, "date.year"),
                    weekDay = token.SelectToken("date.weekday").ToString(),
                    tempMin = GetIntValue(token, "low.celsius"),
                    tempMax = GetIntValue(token, "high.celsius"),
                    windAvg = GetIntValue(token, "avewind.kph"),
                    windMax = GetIntValue(token, "maxwind.kph"),
                    rainTotal = GetIntValue(token, "qpf_allday.mm")
                };
            }

            foreach (var hourlyForecast in hourlyResp.GetValue("hourly_forecast"))
            {
                fc.Hourly["T-" + fc.Hourly.Count] = new HourForecast()
                {
                    hour = GetIntValue(hourlyForecast, "FCTTIME.hour"),
                    timestamp = FromUnixTime(Int64.Parse(hourlyForecast.SelectToken("FCTTIME.epoch")?.ToString())),
                    temp = GetIntValue(hourlyForecast, "temp.metric"),
                    wind = GetIntValue(hourlyForecast, "wspd.metric"),
                    rain = GetIntValue(hourlyForecast, "qpf.metric")
                };
            }

            return fc;
        }