If you use Cumulus, please donate Image

Please read this post before posting

Latest Cumulus release v1.9.4 (build 1099) - Nov 28 2014
Latest Cumulus MX release - v3.0.0 build 3043 Jan 20 2017. See this post for download

Wunderground API calls

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

Moderators: daj, TNETWeather

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

Wunderground API calls

Postby Hunter362 » Tue Aug 23, 2016 11:58 pm

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 Aug 27, 2016 1:35 am, edited 2 times in total.

weatherist34
Posts: 391
Joined: Wed Apr 13, 2016 11:09 am
Weather Station: Fine Offset HP1000/2000
Operating System: OS X Sierra
Location: Istanbul
Contact:

Re: Wunderground API calls

Postby weatherist34 » Wed Aug 24, 2016 6:40 am

hi

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


Return to “Web site - General”

Who is online

Users browsing this forum: No registered users and 6 guests