Page 1 of 4

Simple Weather Label

Posted: 20 May 2013 03:36
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 4936 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.

Re: Simple Weather Label

Posted: 20 May 2013 08:44
by admin
Holy crap, is this the dawning of a new era? (to quote The Specials)

Wow!!!

Re: Simple Weather Label

Posted: 20 May 2013 15:53
by aurumdigitus
Geez, now comes ctbicon. Some of us aren't used to CFI yet.

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

Re: Simple Weather Label

Posted: 20 May 2013 21:35
by nerdweed
The next thing Don adds is ability to customize tooltips (overlong) and then we have a dashboard occupying just 32x32 pixels.

Re: Simple Weather Label

Posted: 20 May 2013 21:43
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.

Re: Simple Weather Label

Posted: 20 May 2013 21:47
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.

Re: Simple Weather Label

Posted: 20 May 2013 22:00
by admin
Well, it was natural... :cup: :whistle:

Re: Simple Weather Label

Posted: 21 May 2013 10:20
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:

Re: Simple Weather Label

Posted: 21 May 2013 20:49
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 5277 times

Re: Simple Weather Label

Posted: 22 May 2013 08:24
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). :|

Re: Simple Weather Label

Posted: 22 May 2013 09:16
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).

Re: Simple Weather Label

Posted: 22 May 2013 10:03
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.

Re: Simple Weather Label

Posted: 22 May 2013 10:18
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"/>

Re: Simple Weather Label

Posted: 22 May 2013 15:49
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.

Re: Simple Weather Label

Posted: 22 May 2013 20:01
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: