New API Icons and Dayparts

  • 1
  • Question
  • Updated 3 months ago
I am wondering about two things in the new API:

Icons:
How to link the icon code against icons ... where to find those icons?

Daypart:
I get the concept of the daypart, while the current one seems to be always NULL (I guess due to the fact that it is current data and little to forecast? But when looking the the time mentioned in the API 

NOTE: For the purposes of this product day(D) = 7am to 7pm and night(N) = 7pm to 7am

I wonder what this is relative to? Is it relative to the location I am pulling the forecast for or relative to the Wunderground HQ, the US or ???

Thunder
This value also seems to be empty ... it might make sense to say "no thunder" or something.

Apart from those questions the API seems to be running fine.


Photo of Enno Lueckel

Enno Lueckel

  • 21 Posts
  • 1 Reply Like

Posted 3 months ago

  • 1
Photo of Emery Wooten

Emery Wooten

  • 13 Posts
  • 10 Reply Likes
In the API documentation there is a link to the icon codes.  Go there and follow it to the download link and download the icon set.  I just hosted them locally in a folder on my server.  The API returns a number like 00 01 02 etc.  In your code build a link using that number: example ../api_images/XX.png where XX is your inserted number.  Beware the API returns 0 to 9 without a leading zero so you will either have to add one or rename the icon files.
---
I can't answer your timing questions but I will post a little code snippet here in case there are readers who don't understand "daypart".  It took me a few minutes to figure it out... The daypart key in the returned data is a series of synchronized arrays.  If you are coding in php the code below may help.

// After you capture the API data in $json_string

$parsed_json = json_decode($json_string, true);
$day = $parsed_json['daypart'][0]['daypartName'];  // get the day array
$nar = $parsed_json['daypart'][0]['narrative']; // get narrative array
$icn = $parsed_json['daypart'][0]['iconCode']; // get icon code array
// then do a loop to extract pieces and build your output string.
$icount = count($day); // get the number of days in the array
if ($icount > 12) $icount = 12; // limit the output to 12 periods
for($i=0; $i<$icount; $i++){
     if ($day[$i] != ""){  // skip any null elements in the array
          // Process your stuff however you need here inside the loop
          // You get the day name with $day[$i]
          // You get the narrative with $nar[$i]
          // You get the icon code with sprintf("%02d", $icn[$i]) // this inserts leading zero
          // I am sure there are other approaches but you get the idea!
     }
}

I didn't like the stock icons.  I downloaded my favorite set from the old API and built myself a new set of numbered .gifs.
(Edited)
Photo of Dan Tripp

Dan Tripp

  • 5 Posts
  • 0 Reply Likes

The daypart for the current day is null after 3:00 pm.  I suppose after this time there’s not enough “day” remaining to make a forecast.  See https://goo.gl/QBN6Rc.

Photo of Emery Wooten

Emery Wooten

  • 13 Posts
  • 10 Reply Likes
Yes, that is why I had to put <if ($day[$i] != "")> in the code.  One would think they would have moved everything to the left and added another time period at the end but not how it was done.