#!/usr/bin/php -q
<?php
ob_implicit_flush(false);
error_reporting(0);
set_time_limit(300);
// Nerd Vittles Weather by Openweathermap ver. 5.1.1a, (c) Copyright Ward Mundy, 2007-2016. All rights reserved.
// This software is licensed under the GPL2 license.
//
// Material alteration of the spoken content provided by this application is strictly prohibited.
//
// For a copy of license, visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
//
// For additional information, contact us: http://pbxinaflash.com/about/comment.php
//-------- DON'T CHANGE ANYTHING ABOVE THIS LINE ----------------
$owmappid="1234567890abcdef";
//$metric = false ;
$debug = 1;
$newlogeachdebug = 1;
$emaildebuglog = 0;
$email = "youremail@yourdomain" ;
//-------- DON'T CHANGE ANYTHING BELOW THIS LINE ----------------
$states_name = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'DC'=>"Washington D.C.",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming",'AB'=>"Alberta",'BC'=>"British Columbia",'MB'=>"Manitoba",'NB'=>"New Brunswick",'WY'=>"Wyoming",'NL'=>"Newfoundland",'WY'=>"Wyoming",'NT'=>"Northwest Territories",'NS'=>"Nova Scotia",'NU'=>"Nunavut",'ON'=>"Ontario",'PE'=>"Prince Edward Island",'QC'=>"Quebec",'SK'=>"Saskatchewan",'YT'=>"Yukon");
$states_abbr = array();
foreach ($states_name as $abbr => $state) {
$states_abbr[$state] = $abbr ;
}
$day_of_week = array('Sunday'=>"Sun",'Monday'=>"Mon",'Tuesday'=>"Tue",'Wednesday'=>"Wed",'Thursday'=>"Thu",'Friday'=>"Fri",'Saturday'=>"Sat");
function fulldow($val) {
global $day_of_week;
$value = array_keys($day_of_week,$val);
$val= $value[0] ;
return $val ;
}
function state($val) {
global $states_name, $states_abbr;
$value = array_keys($states_abbr,$val);
$val= $value[0] ;
return $val ;
}
$log = "/var/log/asterisk/nv-weather-openweathermap.txt" ;
if ($debug and $newlogeachdebug) :
if (file_exists($log)) :
unlink($log) ;
endif ;
endif ;
$stdlog = fopen($log, 'a');
$stdin = fopen('php://stdin', 'r');
$stdout = fopen( 'php://stdout', 'w' );
if ($debug) :
fputs($stdlog, "Nerd Vittles Weather by OpenWeatherMap ver. 5.1.1a (c) Copyright 2007-2016, Ward Mundy. All Rights Reserved.\n\n" . date("F j, Y - H:i:s") . " *** New session ***\n\n" );
endif ;
function read() {
global $stdin;
$input = str_replace("\n", "", fgets($stdin, 4096));
dlog("read: $input\n");
return $input;
}
function write($line) {
dlog("write: $line\n");
echo $line."\n";
}
function dlog($line) {
global $debug, $stdlog;
if ($debug) fputs($stdlog, $line);
}
function execute_agi( $command )
{
GLOBAL $stdin, $stdout, $stdlog, $debug;
fputs( $stdout, $command . "\n" );
fflush( $stdout );
if ($debug)
fputs( $stdlog, $command . "\n" );
$resp = fgets( $stdin, 4096 );
if ($debug)
fputs( $stdlog, $resp );
if ( preg_match("/^([0-9]{1,3}) (.*)/", $resp, $matches) )
{
if (preg_match('/result=([-0-9a-zA-Z]*)(.*)/', $matches[2], $match))
{
$arr['code'] = $matches[1];
$arr['result'] = $match[1];
if (isset($match[3]) && $match[3])
$arr['data'] = $match[3];
return $arr;
}
else
{
if ($debug)
fputs( $stdlog, "Could not figure out returned string, Returning code=".$matches[1]." result=0\n" );
$arr['code'] = $matches[1];
$arr['result'] = 0;
return $arr;
}
}
else
{
if ($debug)
fputs( $stdlog, "Could not process string, Returning -1\n" );
$arr['code'] = -1;
$arr['result'] = -1;
return $arr;
}
}
// ------ Code execution begins here
// parse agi headers into array
//while ($env=read()) {
// $s = split(": ",$env);
// $agi[str_replace("agi_","",$s0)] = trim($s1);
// if (($env == "") || ($env == "\n")) {
// break;
// }
//}
while ( !feof($stdin) )
{
$temp = fgets( $stdin );
if ($debug)
fputs( $stdlog, $temp );
// Strip off any new-line characters
$temp = str_replace( "\n", "", $temp );
$s = explode( ":", $temp );
$agivar[$s[0]] = trim( $s[1] );
if ( ( $temp == "") || ($temp == "\n") )
{
break;
}
}
$zip = $_SERVER["argv"][1];
$zip=trim($zip);
#
#$zip="Bellingham Washington";
#
if ($debug) :
fputs($stdlog, "Location: " . $zip . "\n" );
endif ;
$place = $zip;
if ($metric)
$query="http://api.openweathermap.org/data/2.5/weather?q='$place'&units=metric&appid=".$owmappid;
else
$query="http://api.openweathermap.org/data/2.5/weather?q='$place'&units=imperial&appid=".$owmappid;
//Original// $query ="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.bylocation%20where%20location%3D'$place'%20and%20unit%3D'c'%3B&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$query = trim(str_replace( " ", "%20", $query));
if ($debug) :
fputs($stdlog, "Query: " . $query . "\n" );
#echo "Query: " . $query . "\n";
endif ;
$fd = fopen($query, "r");
if (!$fd) {
echo "<p>Unable to open web connection. \n";
$msg=chr(34)."I'm sorry. No weather information currently is available for $place. Please try again later.".chr(34);
execute_agi("SET VARIABLE WEATHER $msg");
exit;
}
$value = "";
#
#$value='{"coord":{"lon":-122.49,"lat":48.76},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"cmc stations","main":{"temp":54.33,"pressure":1011.95,"humidity":87,"temp_min":54.33,"temp_max":54.33,"sea_level":1034.93,"grnd_level":1011.95},"wind":{"speed":1.7,"deg":232.503},"clouds":{"all":92},"dt":1463416485,"sys":{"message":0.0074,"country":"US","sunrise":1463401536,"sunset":1463456877},"id":5786899,"name":"Bellingham","cod":200}';
#
while(!feof($fd)){
$value .= fread($fd, 4096);
}
fclose($fd);
if ($debug) :
fputs($stdlog, "Value: " . $value . "\n" );
#echo "Value: " . $value . "\n";
endif ;
$weather=json_decode($value, true);
try
{
if ($weather["cod"]!=200)
:
$msg=chr(34)."No weather information currently is available for $place: Please try again later.".chr(34);
execute_agi("SET VARIABLE WEATHER $msg");
exit;
endif;
} catch (Exception $e) {
$msg=chr(34)."No weather information currently is available for $place: Please try again later.".chr(34);
execute_agi("SET VARIABLE WEATHER $msg");
exit;
}
if ($debug) :
fputs($stdlog, "Weather: " . var_dump($weather) . "\n" );
endif ;
$degrees = $weather["wind"]["deg"];
if ($degrees>=349) :
$direction="north";
elseif ($degrees>=326 ) :
$direction="north northwest";
elseif ($degrees>=304 ) :
$direction="northwest";
elseif ($degrees>=281 ) :
$direction="west northwest";
elseif ($degrees>=259 ) :
$direction="west";
elseif ($degrees>=236 ) :
$direction="west southwest";
elseif ($degrees>=214 ) :
$direction="southwest";
elseif ($degrees>=191 ) :
$direction="south southwest";
elseif ($degrees>=169 ) :
$direction="south";
elseif ($degrees>=146 ) :
$direction="south southeast";
elseif ($degrees>=124 ) :
$direction="southeast";
elseif ($degrees>=101 ) :
$direction="east southeast";
elseif ($degrees>=79 ) :
$direction="east";
elseif ($degrees>=56 ) :
$direction="east northeast";
elseif ($degrees>=34 ) :
$direction="northeast";
elseif ($degrees>=11 ) :
$direction="north northeast";
else :
$direction="north";
endif ;
$thetext= $weather["weather"][0]["description"]." in ".$weather["name"]." with a temperature of ". $weather["main"]["temp"]." degrees. ";
$thetext = $thetext. "Wind is from the ";
$thetext = $thetext. $direction." at ";
$thetext = $thetext. (string)$weather["wind"]["speed"];
if ($metric)
:
$thetext = $thetext." kilometer";
else
:
$thetext = $thetext." mile";
endif;
if ($weather["wind"]["speed"]!==1)
:
$thetext = $thetext."s";
endif;
$thetext = $thetext. " per hour. ";
$thetext = $thetext. "Sunrise today at ". date("h:i:s A",$weather["sys"]["sunrise"]).". ";
$thetext = $thetext. "Sunset today at ". date("h:i:s A",$weather["sys"]["sunset"]).". ";
$forecast = "Here are the latest weather conditions and forecast for $place. Brought to you by Nerd Viddles. ";
$forecast = $forecast . "Currently: ". $thetext;
$forecast = $forecast . " Have a great day. Goodbye.";
//==============
$msg= chr(34) .$forecast. chr(34);
$msg = str_replace( ",", " ", $msg );
if ($debug) :
fputs($stdlog, "Forecast: " . $msg . "\n" );
endif ;
execute_agi("SET VARIABLE WEATHER ".$msg);
//echo $msg;
//echo chr(10);
//echo chr(10);
if ($emaildebuglog) :
system("mime-construct --to $email --subject " . chr(34) . "Nerd Vittles Weather by OpenWeatherMap ver. 5.1.1a Session Log" . chr(34) . " --attachment $log --type text/plain --file $log") ;
endif ;
?>