コード例 #1
0
ファイル: weather.cs プロジェクト: johnsudaar/WeatherFetcher
        private static Prediction[] aggregateResults(Weatherdata data)
        {
            List <Prediction> predictions = new List <Prediction>();
            DateTime          last        = new DateTime();

            foreach (Time t in data.Forecast.Time)
            {
                DateTime date = DateTime.Parse(t.From);
                double   temp = Double.Parse(t.Temperature.Value, CultureInfo.InvariantCulture);
                if (predictions.Count == 0)
                {
                    predictions.Add(new Prediction(temp, temp, date.ToString("MM/dd/yyyy")));
                    last = date;
                }
                else if (last.Date != date.Date)
                {
                    predictions.Add(new Prediction(temp, temp, date.ToString("MM/dd/yyyy")));
                    last = date;
                }
                else if (predictions.Last().min > temp)
                {
                    predictions.Last().min = temp;
                }
                else if (predictions.Last().max < temp)
                {
                    predictions.Last().max = temp;
                }
            }
            return(predictions.ToArray());
        }
コード例 #2
0
ファイル: weather.cs プロジェクト: johnsudaar/WeatherFetcher
        public static Prediction[] FiveDayPrediction(string location, string appID)
        {
            WebRequest   request  = WebRequest.Create("http://api.openweathermap.org/data/2.5/forecast?q=" + location + "&mode=xml&units=metric&APPID=" + appID);
            WebResponse  response = request.GetResponse();
            Stream       stream   = response.GetResponseStream();
            StreamReader reader   = new StreamReader(stream);

            XmlSerializer deserializer = new XmlSerializer(typeof(Weatherdata));
            object        obj          = deserializer.Deserialize(reader);
            Weatherdata   data         = (Weatherdata)obj;

            reader.Close();
            response.Close();
            return(aggregateResults(data));
        }
        public Weatherdata getForecastData()
        {
            using (WebClient web = new WebClient())
            {
                string appID  = ConfigurationManager.AppSettings["APPID"];
                Int64  cityID = Convert.ToInt64(ConfigurationManager.AppSettings["cityID_Tvrdosin"]);

                string url = string.Format("https://api.openweathermap.org/data/2.5/forecast?id=" + cityID.ToString() + "&appid=" + appID + "&units=metric&mode=xml");
                var    xml = web.DownloadString(url);

                var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Weatherdata));

                Weatherdata data = new Weatherdata();

                using (TextReader reader = new StringReader(xml))
                {
                    data = serializer.Deserialize(reader) as Weatherdata;
                }
                return(data);
            }
        }