Welcome to the Cumulus Support forum.

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Cumulus MX V4 beta test release 4.0.0 (build 4017) - 17 March 2024

Legacy Cumulus 1 release v1.9.4 (build 1099) - 28 November 2014 (a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

Wunderground API calls

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
Hunter362
Posts: 93
Joined: Tue 19 Oct 2010 12:20 am
Weather Station: Ambient Weather WS-2080
Operating System: Ubuntu 14.4
Location: Fairmount, New York (USA)
Contact:

Wunderground API calls

Post by Hunter362 »

I'm including a .php script in my html index page. I've set the cache time to 4 hours, shouldn't the script look at the cached file for the info rather than running the script again? It's getting the temperature records for today's date, so it really only needs to be called once or twice during the day right?
Found the script in the wxfourms, I believe Ken True wrote it or modified it to work for a user.

Or just a way to get the associated values to insert as the webtags and just call the script once via a cron job


I get the API usage alert from wunderground, about going over the 500 calls/day

Your wunderground API key (xxx) exceeded its allotted usage today by
making 503 calls in a day but the limit is 500.

Code: Select all

<?php
// Settings:
$APIkey = 'xxxx';

$WU_URL = 'http://api.wunderground.com/api/'.$APIkey.'/almanac/q/NY/Syracuse.json';

$cacheFileDir = './cache/';                     // default cache file directory
$cacheName = "WU-almanac-Syr.txt";           // locally cached page from WU
// $refetchSeconds = 1800;                    // cache lifetime (1800sec = 30 minutes)
$refetchSeconds = 14400;

// end of settings
// overrides from Settings.php if available
global $SITE;
if(isset($SITE['cacheFileDir']))     {$cacheFileDir = $SITE['cacheFileDir']; }
// end of overrides from Settings.php
//
// -------------------begin code ------------------------------------------
$Status = "<!-- begin WU-record via API -->\n";
$Force = false;
if (isset($_REQUEST['force']) and  $_REQUEST['force']=="1" ) {
  $Force = true;
}

$doDebug = false;
if (isset($_REQUEST['debug']) and strtolower($_REQUEST['debug'])=='y' ) {
  $doDebug = true;
}

$fileName = $WU_URL;
if ($doDebug) {
  $Status .= "<!-- WU URL: $fileName -->\n";
}

$cacheName = $cacheFileDir . $cacheName;

// The number 1800 below is the number of seconds the cache will be used instead of pulling a new file
// 1800 = 60s x 30m so it retreives every 30 minutes. 

if (! $Force and file_exists($cacheName) and filemtime($cacheName) + $refetchSeconds > time()) {
      $html = implode('', file($cacheName)); 
      $Status .= "<!-- loading from $cacheName (" . strlen($html) . " bytes) -->\n"; 
  } else { 
      $Status .= "<!-- loading from $fileName. -->\n"; 
      $html = fetchUrlWithoutHangingWUR($fileName,true); 
      $fp = fopen($cacheName, "w"); 
	  if (!$fp) { 
	    $Status .= "<!-- unable to open $cacheName for writing. -->\n"; 
	  } else {
        $write = fputs($fp, $html); 
        fclose($fp);  
		$Status .= "<!-- saved cache to $cacheName (". strlen($html) . " bytes) -->\n";
	  } 
} 

  if($doDebug) { print $Status; }

  $JSON = json_decode($html,true); // get as associative array
  $recordHigh = $JSON['almanac']['temp_high']['record']['F'];
  $recordHighYear = $JSON['almanac']['temp_high']['recordyear'];
  $recordLow = $JSON['almanac']['temp_low']['record']['F'];
  $recordLowYear = $JSON['almanac']['temp_low']['recordyear'];
  $normalHigh = $JSON['almanac']['temp_high']['normal']['F'];
  $normalLow = $JSON['almanac']['temp_low']['normal']['F'];

// put rest of your processing here

// Functions --------------------------------------------------------------------------------

function WURmicrotime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}
// ------------------------------------------------------------------

// get contents from one URL and return as string 
 function fetchUrlWithoutHangingWUR($url,$useFopen) {

  global $Status, $needCookie,$timeStamp,$TOTALtime;
  $overall_start = time();
 
//   print "<!-- using file function -->\n";
   $T_start = WURmicrotime_float();

   $xml = implode('',file($url));
   $T_close = WURmicrotime_float();
   $ms_total = sprintf("%01.3f",round($T_close - $T_start,3)); 
   $Status .= "<!-- file() stats: total=$ms_total secs -->\n";
   $TOTALtime+= ($T_close - $T_start);
   $overall_end = time();
   $overall_elapsed =   $overall_end - $overall_start;
   $Status .= "<!-- fetch function elapsed= $overall_elapsed secs. -->\n"; 
   return($xml);

   }    // end fetchUrlWithoutHangingWUR
// ------------------------------------------------------------------
?>
Last edited by Hunter362 on Sat 27 Aug 2016 1:35 am, edited 2 times in total.
weatherist34
Posts: 51
Joined: Wed 13 Apr 2016 11:09 am
Weather Station: Davis Vantage Pro2 Plus
Operating System: OSX High Sierra
Contact:

Re: Wunderground API calls

Post by weatherist34 »

hi

do you have any other scripts using the same api key ??
Post Reply