Simple Weather Label

Discuss and share scripts and script files...
serendipity
Posts: 3358
Joined: 07 May 2007 18:14
Location: NJ/NY

Simple Weather Label

Post by serendipity »

UPDATED: Jan 26th 2014
Change log:
Jan 26 2014 Fixed decimal separator bug
Nov 2 2013 Added- forecast menu.
Here's a simple weather label icon, when clicked it will show current temperature in the user button and will show forecast in the menu (11 days max).
I use OpenWeatherMap API now. (as of Nov 1st 2013 google removed it's weather API)
Weather&Forecast.gif
Weather&Forecast.gif (8.95 KiB) Viewed 4101 times
------How it works------------
1) Add a new User Button in the toolbar.
2) In the on-click edit field paste the below code and OK.
3) Click on the button to display current temperature and menu shows the forecast. :)
-------------------------------

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="New york, USA";// Can be of type "City, State, Country" or just "City, Country" or even "City" in most cases 
   $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((get("locale", 14)==",")){
   //$TempNow=replace ($TempNow,".", ",");
   //}
   //ELSE{
   //$TempNow=replace ($TempNow,",", ".");
   //}
   $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((get("locale", 14)==",")){
      //   $forecast=replace ($forecast,".", ",");
  // }
   //ELSE{
   //    $forecast=replace ($forecast,",", ".");
   //}
      $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>");
There's lot of other info that can be retrieved from the OpenWeatherAPI. For example you can retrieve weather conditions codes and get suitable icons and so on. See project here: http://openweathermap.org/API
Feel free to use it the way you want.

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

Re: Simple Weather Label

Post by admin »

Holy crap, is this the dawning of a new era? (to quote The Specials)

Wow!!!

aurumdigitus
Posts: 1075
Joined: 30 May 2008 21:02
Location: Lake Erie

Re: Simple Weather Label

Post by aurumdigitus »

Geez, now comes ctbicon. Some of us aren't used to CFI yet.

Danger, Will Robinson, danger ... :naughty:

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47

Re: Simple Weather Label

Post by nerdweed »

The next thing Don adds is ability to customize tooltips (overlong) and then we have a dashboard occupying just 32x32 pixels.

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

Re: Simple Weather Label

Post by admin »

nerdweed wrote:The next thing Don adds is ability to customize tooltips (overlong) and then we have a dashboard occupying just 32x32 pixels.
Tooltips? Explain, please...

Actually the next thing I did is this:

Code: Select all

    + Scripting got a new function.
      Name: CTBState
      Action: Set/Get the pressed state of a Custom Toolbar Button.
      Syntax: ctbstate([state], [button_index])
        state:   0 = unpressed
                 1 = pressed 
        button_index: Index of the button.
                      If empty then the index of the button owning the 
                      script is used. If the script is called from 
                      outside a CTB you *have* to state a button index, 
                      else you will get an error message.
        return:   The current state (before any new state was set).
      Example (use this script in the On Click event of a CTB):
        // state toggle
          if (ctbstate() == 1) {
            ctbstate(0);
          } else {
            ctbstate(1);
          }
      Note: The state is remembered across sessions.

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

Re: Simple Weather Label

Post by serendipity »

admin wrote:
nerdweed wrote:The next thing Don adds is ability to customize tooltips (overlong) and then we have a dashboard occupying just 32x32 pixels.
Tooltips? Explain, please...

Actually the next thing I did is this:

Code: Select all

    + Scripting got a new function.
      Name: CTBState
      Action: Set/Get the pressed state of a Custom Toolbar Button.
      Syntax: ctbstate([state], [button_index])
        state:   0 = unpressed
                 1 = pressed 
        button_index: Index of the button.
                      If empty then the index of the button owning the 
                      script is used. If the script is called from 
                      outside a CTB you *have* to state a button index, 
                      else you will get an error message.
        return:   The current state (before any new state was set).
      Example (use this script in the On Click event of a CTB):
        // state toggle
          if (ctbstate() == 1) {
            ctbstate(0);
          } else {
            ctbstate(1);
          }
      Note: The state is remembered across sessions.
WOW! already? :appl:
I was planning to ask this after breadcrumb.

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

Re: Simple Weather Label

Post by admin »

Well, it was natural... :cup: :whistle:

LittleBiG
Posts: 1846
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: Simple Weather Label

Post by LittleBiG »

serendipity wrote:OK here's a simple weather label icon, when clicked it will show temperature in your city in the user button.
What a sick idea! :mrgreen: And how well it works. Awesome! :appl:

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47

Re: Simple Weather Label

Post by nerdweed »

admin wrote:
nerdweed wrote:The next thing Don adds is ability to customize tooltips (overlong) and then we have a dashboard occupying just 32x32 pixels.
Tooltips? Explain, please...
The tip that you get when you hover over something. I had hovered over Enable Shell Thumbnails - Mouse pointer was on a holiday
Tooltip.png
Tooltip.png (3.73 KiB) Viewed 4442 times

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

Re: Simple Weather Label

Post by admin »

nerdweed wrote:
admin wrote:
nerdweed wrote:The next thing Don adds is ability to customize tooltips (overlong) and then we have a dashboard occupying just 32x32 pixels.
Tooltips? Explain, please...
The tip that you get when you hover over something. I had hovered over Enable Shell Thumbnails - Mouse pointer was on a holiday
Tooltip.png
OK, I know what a tooltip is, but I did not understand your post (and still don't). :|

Borut
Posts: 1412
Joined: 19 Oct 2010 19:29

Re: Simple Weather Label

Post by Borut »

I guess he means some SC means to control/define a tool tip of a button. I guess we could then have a complete weather forecast in it. :mrgreen:

Edit:

@serendipity
BTW, the temperature information which Google API returns seems plain wrong, at least for some mid European places I have tested (differences currently up to 6 C).
Last edited by Borut on 22 May 2013 10:03, edited 1 time in total.
Win 10 Pro 64bit

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

Re: Simple Weather Label

Post by admin »

Borut wrote:I guess he means some SC means to control/define a tool tip of a button. I guess we could then have a complete weather forecast in it. :mrgreen:
Currently with CTBs, the tooltip is identical with Name-field. But the Name field can also control the functioning of the button when other fields are not set. So there is some overloading here that will not work together smoothly with a weather forecast in the tooltip. It would be easily solvable by adding an extra field for tooltips, but low prio at the moment.

40k
Posts: 234
Joined: 09 Dec 2011 21:25

Re: Simple Weather Label

Post by 40k »

I did not know you could use the gettoken() function in such a way that it lets you define the prefix like that. I have always used a regEx to get data out of an XML entry of the form <type="value"/>
I develop scripts that integrate media functionality into Xyplorer.
Hash - Twitch.tv in VLC (NEW 2.0!) - FFmpeg GUI - Youtube downloading
XYplorer for Linux! Tutorial

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

Re: Simple Weather Label

Post by serendipity »

Borut wrote:@serendipity
BTW, the temperature information which Google API returns seems plain wrong, at least for some mid European places I have tested (differences currently up to 6 C).
Works more or less accurately when compared with other weather stations here in the US. Before posting I tested briefly at few places in Europe and it seemed decently close.
Well, not something I can help as google provides this info, they themselves have been using Wunderground now, maybe i can try that when i have time.
Last edited by serendipity on 22 May 2013 20:09, edited 1 time in total.

aurumdigitus
Posts: 1075
Joined: 30 May 2008 21:02
Location: Lake Erie

Re: Simple Weather Label

Post by aurumdigitus »

@serendipity - Having gone "Beta" for reasons stated in Bug Reports, could not resist trying this out. Is somewhere between fiendishly and nefariously clever.

When these new script commends appear in the next Official Release you have provided a nice learning tool. :appl:

Post Reply