Simple Weather Label

Discuss and share scripts and script files...
admin
Site Admin
Posts: 60617
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Simple Weather Label

Post by admin »

With OpenWeatherMap API it's 10C, so one degree better. :mrgreen:

klownboy
Posts: 4142
Joined: 28 Feb 2012 19:27

Re: Simple Weather Label

Post by klownboy »

serendipity wrote:But I am going with OpenWeatherMap API because I don't want to deal with another service hanging up.
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/
Thanks again,
Ken
Windows 11, 23H2 Build 22631.3447 at 100% 2560x1440

Enternal
Posts: 1174
Joined: 10 Jan 2012 18:26

Re: Simple Weather Label

Post by Enternal »

Wow, this is such an interesting script! I haven't changed it to the OpenWeatherMap but it also displays the sunrise, sunset, humidity, and some others:

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;

serendipity
Posts: 3358
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Simple Weather Label

Post by serendipity »

Thanks Klownboy and Enternal, this is getting interesting.
Just updated the first post with forecast menu.
It's starting to get not so simple now. :)

admin
Site Admin
Posts: 60617
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Simple Weather Label

Post by admin »

A file manager with weather forecast -- killer! :mrgreen:

Enternal
Posts: 1174
Joined: 10 Jan 2012 18:26

Re: Simple Weather Label

Post by Enternal »

admin wrote:A file manager with weather forecast -- killer! :mrgreen:
Lol! Yep a killer function! It really shows how powerful XYplorer's scripting capabilities are!

klownboy
Posts: 4142
Joined: 28 Feb 2012 19:27

Re: Simple Weather Label

Post by klownboy »

Thanks Enternal and serendipity, very nice additions. :appl: Left click - Maine's weather, right click - Virginia's! So, when are we getting news feeds via XYplorer? :)
Windows 11, 23H2 Build 22631.3447 at 100% 2560x1440

CookieMonster

Re: Simple Weather Label

Post by CookieMonster »

The script needs to include WindChill / Humidex. If it's 20C with a WindChill it may be only 16C.

admin
Site Admin
Posts: 60617
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Simple Weather Label

Post by admin »

Something must have changed with the OpenWeatherMap. It just does not feel like "609C" outside in Cologne. :)

serendipity
Posts: 3358
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Simple Weather Label

Post by serendipity »

admin wrote:Something must have changed with the OpenWeatherMap. It just does not feel like "609C" outside in Cologne. :)
:lol: i'll look into it.

serendipity
Posts: 3358
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Simple Weather Label

Post by serendipity »

serendipity wrote:
admin wrote:Something must have changed with the OpenWeatherMap. It just does not feel like "609C" outside in Cologne. :)
:lol: i'll look into it.
Update:
With $location set to: cologne, germany gives me 3C. Maybe something was wrong before and not anymore?

admin
Site Admin
Posts: 60617
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Simple Weather Label

Post by admin »

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.

serendipity
Posts: 3358
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Simple Weather Label

Post by serendipity »

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.
Oh ok, I'll add that.

serendipity
Posts: 3358
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Simple Weather Label

Post by serendipity »

Fixed decimal separator bug in the first post.

I don't know how to determine if locale uses dot or comma for decimals, so opted for an alternative way to fix the bug.
If temperature is < -200 or > 200, decimal type . is replaced with , which automatically fixes extreme temperatures.

admin
Site Admin
Posts: 60617
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Simple Weather Label

Post by admin »

serendipity wrote:If temperature is < -200 or > 200, decimal type . is replaced with , which automatically fixes extreme temperatures.
Well, this fails with -1 and 1 degrees. :)

Why not simply let the user configure it:

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>");

Post Reply