Re: Simple Weather Label
Posted: 01 Nov 2013 20:38
With OpenWeatherMap API it's 10C, so one degree better. 
Forum for XYplorer Users and Developers
https://www.xyplorer.com/xyfc/
Thanks serendipity. Hopefully Yahoo itself is around for awhile since I get some mail through them, but as you said, who knows about their weather API. It looks like with the Yahoo API you have to use the "Where on Earth Identifier", WOEID and can't use the City, State, country etc, though it's easy to obtain. There's even a site dedicated to retrieving the WOEID. http://woeid.rosselliot.co.nz/serendipity wrote:But I am going with OpenWeatherMap API because I don't want to deal with another service hanging up.
Code: Select all
//http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=9699
//Weather in Your City (using Yahoo API)
//Simple Script That Displays Temperature in a User Button
//You must obtain the WOEID for your location by running http://weather.yahoo.com/ Plug in your city and country and the resultant URL will have the WOEID as the last set of numbers in the URL. Change the WOEID# below to your location's WOEID and in the second line change the temperature unit.
$WOEID=667931; //Cologne, Germany
//$WOEID=2410534; //Glade Spring, VA
$Unit="c"; //Set It To c For Celsius and f For Fahrenheit
$URL=ReadURL("http://weather.yahooapis.com/forecastrss?w=$WOEID&u=$Unit");
//http://weather.yahooapis.com/forecastrss?w=2475687&u=F Portland in F
$TempNow=GetToken($URL,2,"temp=");$TempNow=GetToken($TempNow,1," ");$TempNow=Quote($TempNow,1);
$Cond=GetToken($URL,2,"text=");$Cond=GetToken($Cond,1," ");$Cond=Quote($Cond,1);
$Sunrise=GetToken($URL,2,"sunrise=");$Sunrise=GetToken($Sunrise,1," ");$Sunrise=Quote($Sunrise,1);
$Sunset=GetToken($URL,2,"sunset=");$Sunset=GetToken($Sunset,1,"/>");$Sunset=Quote($Sunset,1);
$Humidity=GetToken($URL,2,"humidity=");$Humidity=GetToken($Humidity,1," ");$Humidity=Quote($Humidity,1);
$Visibility=GetToken($URL,2,"visibility=");$Visibility=GetToken($Visibility,1," ");$Visibility=Quote($Visibility,1);
$Pressure=GetToken($URL,3,"pressure=",,2);$Pressure=GetToken($Pressure,1," ");$Pressure=Quote($Pressure,1);
$Description=GetToken($URL,2,"<BR /><b>Forecast:</b><BR />");$Description=GetToken($Description,1,"<a");$Description=RegExReplace($Description,"<br />","");
$Description=RegExReplace($Description,"\n","|");$Description=FormatList($Description,"e","|");$Description=Replace($Description,"|","<crlf>");
IF($Unit LikeI "f"){
$TempNow=Quote("""$TempNow"."F""",1);
}
ELSE{
$TempNow=Quote("""$TempNow"."C""",1);
}
CTBIcon("label: $TempNow >554433,FFFFEE");
$FullWeather="Current Condition:<crlf>$Cond $TempNow<crlf><crlf>Sunrise: $Sunrise<crlf>Sunset: $Sunset<crlf>Humidity: $Humidity<crlf>Visibility: $Visibility<crlf>Pressure: $Pressure<crlf><crlf>Forcast: <crlf>$Description";
Echo $FullWeather;Lol! Yep a killer function! It really shows how powerful XYplorer's scripting capabilities are!admin wrote:A file manager with weather forecast -- killer!
admin wrote:Something must have changed with the OpenWeatherMap. It just does not feel like "609C" outside in Cologne.
Update:serendipity wrote:admin wrote:Something must have changed with the OpenWeatherMap. It just does not feel like "609C" outside in Cologne.i'll look into it.
Oh ok, I'll add that.admin wrote:AH, I see! It's because recently fixed SC round()! Here in Germany it now expects comma as decimal separator, but OpenWeatherMap returns a dot. So the script needs a further configuration possibility for comma/dot.
Well, this fails with -1 and 1 degrees.serendipity wrote:If temperature is < -200 or > 200, decimal type . is replaced with , which automatically fixes extreme temperatures.
Code: Select all
//Weather and Forecast in your city (using OpenWeatherMap API)
//Simple script that displays current temperature as ctb label and forecast as menu
//Change the location, unit and forecast days in lines 1,2 and 3 respectively.
$location="Cologne, Germany";// Can be of type "City, State, Country" or just "City, Country" or even "City" in most cases
$decimalSeparator = ",";
$unit="metric";//Set it to "metric" for celsius, "imperial" for fahrenheit and "" for kelvin
$forecastdays=5;//upto 11 days
IF($forecastdays>11){
$forecastdays=11;
}
$url=readurl("http://api.openweathermap.org/data/2.5/find?q=$location&units=$unit&mode=xml",,,8);
$url2=readurl("http://api.openweathermap.org/data/2.5/forecast/daily?q=$location&mode=xml&units=$unit&cnt=$forecastdays",,,8);
$quote=chr(34);
//City name
$city=gettoken($url, 2, "<city id=");
$city=gettoken($city, 1, "$quote>");
$city=gettoken($city, 2, "name=$quote");
status "$city weather";
//Current Weather
$TempNow=gettoken($url, 2, "<temperature value=$quote");
$TempNow=gettoken($TempNow, 1, "$quote min=");
IF($decimalSeparator != "."){
$TempNow=replace ($TempNow,".", $decimalSeparator);
}
$TempNow=round("$TempNow");
IF($unit LikeI "metric"){
$TempNow=$TempNow."C";
}
ELSEIF($unit LikeI "imperial"){
$TempNow=$TempNow."F";
}
ELSE{
$TempNow=$TempNow."K";
}
ctbicon("label: $TempNow >554433,ffffee");
wait(50);
//Weather Forecast
$daycount=1;
$token=2;
$forecastlist="";
WHILE ($token-2<$forecastdays){
$forecast=gettoken($url2, $token, "<temperature day=$quote");
$forecast=gettoken($forecast, 1, "$quote min=");
IF($decimalSeparator != "."){
$forecast=replace ($forecast,".", $decimalSeparator);
}
$forecast=round("$forecast");
IF($unit LikeI "metric"){
$forecast=$forecast."C";
}
ELSEIF($unit LikeI "imperial"){
$forecast=$forecast."F";
}
ELSE{
$forecast=$forecast."K";
}
$day=formatdate(,"ddd", "d", $daycount);
$forecast="$forecast [$day]";
$forecastlist="$forecastlist<crlf>$forecast";
$token++;
$daycount++;
}
popupmenu($forecastlist,,,,,,"<crlf>");