Feedback

Wunderground Wettervorhersage

Sprache: C#

Diese Klasse ruft per XML Schnittstelle von Wunderground die Wettervorhersage für einen beliebigen Ort ab. Aufruf: List<WundergroundForecastData> GetForecast("Regensburg,Germany"); Die Ortsangabe kann direkt unter www.wunderground.com geprüft werden, Angabe am Besten immer inkl. Land (auf Englisch). Eine vollständige Beschreibung der API gibt es hier: http://wiki.wunderground.com/index.php/API_-_XML#ForecastXML
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Xml;

namespace MaxPro.Common.Wetter
{
    //========================================================================================================================
    /// <summary>
    /// Schnittstelle zum Weather Underground, wunderground.com
    /// </summary>
    public static class Wunderground
    {
        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Struktur für die Ergebnis-Daten
        /// </summary>
        public struct WundergroundForecastData
        {
            /// <summary>
            /// Tagesdatum
            /// </summary>
            public DateTime Datum;
            /// <summary>
            /// Wetter-Vorhersage
            /// </summary>
            public string Condition;
            /// <summary>
            /// Bezeichnung für das dazustellende Icon
            /// </summary>
            public string Icon;
            /// <summary>
            /// Tiefste Temperatur
            /// </summary>
            public int TemperatureLow;
            /// <summary>
            /// Höchste Temperatur
            /// </summary>
            public int TemperatureHigh;
        }

        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Wettervorhersage für einen Ort herunterladen
        /// </summary>
        /// <param name="ort"></param>
        /// <returns></returns>
        public static IEnumerable<WundergroundForecastData> GetForecast(string ort)
        {
            // Ergebnis
            List<WundergroundForecastData> ergebnis = new List<WundergroundForecastData>();

            // Erstmal einen Webclient holen
            WebClient web = new WebClient();

            // XML Service von Wunderground
            string url = "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=" + ort;
            string xmlWunderground;
            try
            {
                using (StreamReader sr = new StreamReader(web.OpenRead(url)))
                {
                    xmlWunderground = sr.ReadToEnd();
                    sr.Close();
                }
            }
            catch (Exception)
            {
                return null;
            }

            // XML auslesen
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlWunderground);
            XmlElement root = doc.DocumentElement;
            if (root != null)
            {
                XmlNode simpleForecast = root["simpleforecast"];
                if (simpleForecast != null)
                {
                    // Alle Elemente der Auflistung durcharbeiten
                    foreach (XmlNode forecastDay in simpleForecast)
                    {
                        // Ist es wirklich ein Forecast?
                        if (forecastDay.Name == "forecastday")
                        {
                            // Ergebnis einfügen
                            WundergroundForecastData wfd = new WundergroundForecastData();

                            // Datum aus der Datei lesen
                            XmlNode dateNode = forecastDay["date"];
                            if (dateNode != null)
                                wfd.Datum = new DateTime(dateNode["year"].GetInnerTextInt(), dateNode["month"].GetInnerTextInt(), dateNode["day"].GetInnerTextInt());

                            // Temperatur
                            XmlNode lowTemp = forecastDay["low"];
                            if (lowTemp != null)
                                wfd.TemperatureLow = lowTemp["celsius"].GetInnerTextInt();
                            XmlNode highTemp = forecastDay["low"];
                            if (highTemp != null)
                                wfd.TemperatureHigh = highTemp["celsius"].GetInnerTextInt();

                            // Condition
                            wfd.Condition = forecastDay["conditions"].GetInnerText();
                            wfd.Icon = forecastDay["icon"].GetInnerText();

                            // Ergebnis speichern
                            ergebnis.Add(wfd);
                        }
                    }
                }
            }

            // Ergebnis
            return ergebnis;
        }

        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// InnerText eines XmlElement als String zurückgeben
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private static string GetInnerText(this XmlElement node)
        {
            if (node != null)
                return node.InnerText;
            return "";
        }

        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// InnerText eines XmlElement als Integer zurückgeben
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private static int GetInnerTextInt(this XmlElement node)
        {
            try
            {
                if (node != null)
                    return Convert.ToInt32(node.InnerText);
            }
            catch (Exception) { }
            return 0;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Xml;

namespace MaxPro.Common.Wetter
{
    //========================================================================================================================
    /// <summary>
    /// Schnittstelle zum Weather Underground, wunderground.com
    /// </summary>
    public static class Wunderground
    {
        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Struktur für die Ergebnis-Daten
        /// </summary>
        public struct WundergroundForecastData
        {
            /// <summary>
            /// Tagesdatum
            /// </summary>
            public DateTime Datum;
            /// <summary>
            /// Wetter-Vorhersage
            /// </summary>
            public string Condition;
            /// <summary>
            /// Bezeichnung für das dazustellende Icon
            /// </summary>
            public string Icon;
            /// <summary>
            /// Tiefste Temperatur
            /// </summary>
            public int TemperatureLow;
            /// <summary>
            /// Höchste Temperatur
            /// </summary>
            public int TemperatureHigh;
        }

        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Wettervorhersage für einen Ort herunterladen
        /// </summary>
        /// <param name="ort"></param>
        /// <returns></returns>
        public static IEnumerable<WundergroundForecastData> GetForecast(string ort)
        {
            // Ergebnis
            List<WundergroundForecastData> ergebnis = new List<WundergroundForecastData>();

            // Erstmal einen Webclient holen
            WebClient web = new WebClient();

            // XML Service von Wunderground
            string url = "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=" + ort;
            string xmlWunderground;
            try
            {
                using (StreamReader sr = new StreamReader(web.OpenRead(url)))
                {
                    xmlWunderground = sr.ReadToEnd();
                    sr.Close();
                }
            }
            catch (Exception)
            {
                return null;
            }

            // XML auslesen
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlWunderground);
            XmlElement root = doc.DocumentElement;
            if (root != null)
            {
                XmlNode simpleForecast = root["simpleforecast"];
                if (simpleForecast != null)
                {
                    // Alle Elemente der Auflistung durcharbeiten
                    foreach (XmlNode forecastDay in simpleForecast)
                    {
                        // Ist es wirklich ein Forecast?
                        if (forecastDay.Name == "forecastday")
                        {
                            // Ergebnis einfügen
                            WundergroundForecastData wfd = new WundergroundForecastData();

                            // Datum aus der Datei lesen
                            XmlNode dateNode = forecastDay["date"];
                            if (dateNode != null)
                                wfd.Datum = new DateTime(dateNode["year"].GetInnerTextInt(), dateNode["month"].GetInnerTextInt(), dateNode["day"].GetInnerTextInt());

                            // Temperatur
                            XmlNode lowTemp = forecastDay["low"];
                            if (lowTemp != null)
                                wfd.TemperatureLow = lowTemp["celsius"].GetInnerTextInt();
                            XmlNode highTemp = forecastDay["low"];
                            if (highTemp != null)
                                wfd.TemperatureHigh = highTemp["celsius"].GetInnerTextInt();

                            // Condition
                            wfd.Condition = forecastDay["conditions"].GetInnerText();
                            wfd.Icon = forecastDay["icon"].GetInnerText();

                            // Ergebnis speichern
                            ergebnis.Add(wfd);
                        }
                    }
                }
            }

            // Ergebnis
            return ergebnis;
        }

        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// InnerText eines XmlElement als String zurückgeben
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private static string GetInnerText(this XmlElement node)
        {
            if (node != null)
                return node.InnerText;
            return "";
        }

        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// InnerText eines XmlElement als Integer zurückgeben
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private static int GetInnerTextInt(this XmlElement node)
        {
            try
            {
                if (node != null)
                    return Convert.ToInt32(node.InnerText);
            }
            catch (Exception) { }
            return 0;
        }
    }
}

3 Kommentare

  1. An sich ganz gut, nur in Zeile 100 müsste
    XmlNode highTemp = forecastDay[„[b]high[/b][u][/u]“];
    statt
    XmlNode highTemp = forecastDay[„low“];

    stehen.

  2. Stand 18.09.2019:
    Die Api funktioniert wohl nicht mehr bzw. nicht mehr kostenlos.
    Ich bekam jedenfalls immer als Ergebnis NULL zurück.

    Schade, aber so ist das mit externen Schnittstellenanbieter. Stellen von heute auf morgen einfach so den Dienst ein.