6237 yahoovejr

From Teknologisk videncenter
Jump to: navigation, search

Yahoo Vejr API

Yahoo har en vejr API der kan returnere en vejrudsigt for et bestem område/by ud fra en såkaldt WOEID.
For Viborg er WOEID 558711, og du kan selv finde andre på adressen


 http://woeid.factormystic.net.


Output fra APIen kan du se ved at indsætte følgende adresse i din browser.


http://weather.yahooapis.com/forecastrss?w=558711&u=c


Som giver følgende XML output.


<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
<channel>
http://us.rd.yahoo.com/dailynews/rss/weather/Viborg__DK/*http://weather.yahoo.com/forecast/DAXX0040_c.html
</link>
<description>Yahoo! Weather for Viborg, DK</description>
<language>en-us</language>
<lastBuildDate>Sat, 27 Sep 2014 3:50 pm CEST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Viborg" region="" country="Denmark"/>
<yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>
<yweather:wind chill="16" direction="260" speed="27.36"/>
<yweather:atmosphere humidity="68" visibility="9.99" pressure="1015.92" rising="1"/>
<yweather:astronomy sunrise="7:16 am" sunset="7:08 pm"/>
<image>
<width>142</width>
<height>18</height>
<url>
http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif
</url>
</image>
<item>
<geo:lat>56.45</geo:lat>
<geo:long>9.39</geo:long>
http://us.rd.yahoo.com/dailynews/rss/weather/Viborg__DK/*http://weather.yahoo.com/forecast/DAXX0040_c.html
</link>
<pubDate>Sat, 27 Sep 2014 3:50 pm CEST</pubDate>
<yweather:condition text="Partly Cloudy" code="30" temp="16" date="Sat, 27 Sep 2014 3:50 pm CEST"/>
<description>
 [[File:asp_http://l.yimg.com/a/i/us/we/52/30.gif"/><br /> <b>Current Conditions:</b><br /> Partly Cloudy, 16 C<BR /> <BR /><b>Forecast:</b><BR /> Sat - Partly Cloudy. High: 16 Low: 10<br /> Sun - Partly Cloudy. High: 18 Low: 11<br /> Mon - AM Fog/PM Sun. High: 19 Low: 11<br /> Tue - PM Light Rain. H
]]>
igh: 17 Low: 12<br /> Wed - Showers. High: 17 Low: 11<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Viborg__DK/*http://weather.yahoo.com/forecast/DAXX0040_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]>
</description>
<yweather:forecast day="Sat" date="27 Sep 2014" low="10" high="16" text="Partly Cloudy" code="29"/>
<yweather:forecast day="Sun" date="28 Sep 2014" low="11" high="18" text="Partly Cloudy" code="30"/>
<yweather:forecast day="Mon" date="29 Sep 2014" low="11" high="19" text="AM Fog/PM Sun" code="20"/>
<yweather:forecast day="Tue" date="30 Sep 2014" low="12" high="17" text="PM Light Rain" code="11"/>
<yweather:forecast day="Wed" date="1 Oct 2014" low="11" high="17" text="Showers" code="11"/>
<guid isPermaLink="false">DAXX0040_2014_10_01_7_00_CEST</guid>
</item>
</channel>
</rss>
 fan1666.sports.bf1.yahoo.com Sat Sep 27 07:12:00 PDT 2014 
-->


I C# kan nedenstående kode anvendes til at hente XML fra Yahoo's API. Du skal selv gøre koden færdig.


String woeidViborg = "558711";
String urlStr = String.Format("http://weather.yahooapis.com/forecastrss?w={0}&u=c", woeidViborg);
WebClient cli = new WebClient();

String weather = cli.DownloadString(urlStr);
XmlReader xml = XmlReader.Create(new StringReader(weather));

while (xml.Read())
{
	//Indsæt koden fra XML read eksemplet her, og ret den til så den finder og gemmer temperaturen som er gemt under "yweather:condition"
	//Find også udsigten for de næste dage
}


Hvis du hellere vil have at koden selv kan finde WOEID kan du i stedet kigge på følgende link. Det er lidt mere kompliceret men giver også flere muligheder.


https://developer.yahoo.com/weather/?dataTypeRadios=XML