FYI Yahoo Weather API (949) "Improved"

wardmundy

Nerd Uno
Joined
Oct 12, 2007
Messages
19,201
Reaction score
5,219
Yahoo has made "improvements" to their weather API which, of course, blew the Weather TTS app (949) out of the water. Looks rather warm in Atlanta this week. Probably will take a complete rewrite to properly scrape the data from the new site (shown below). In the meantime, Weather by ZIP (947) still works. :rolleyes:

Code:
    -- Executing [949@from-internal:11] NoOp("PJSIP/702-00000005", "Forecast: Here
are the latest weather conditions and forecast for Atlanta Georgia. Brought to you by Yahoo and
Nerd Viddles. Currently: nPartly Cloudy. 113 degrees fahrenheit. Humidity:  per cent. Barometric
pressure: 0 inches. Wend direction and speed: from the north at 0 miles per hour. Visibility: 0
miles. Sunrise today at { Sunset tonight at :{ Here is the latest forecast. nWednesday: Sunny.
High: 123.8. Low: 95. nThursday: afternoon Rain. High: 136.4. Low: 107.6. nFriday: morning
Clouds/PM Sun. High: 136.4. Low: 95. nSaturday: Sunny. High: 65. Low: 40. nSunday: Sunny.
High: 67. Low: 43. n Have a great day. Goodbye.") in new stack

Code:
$oldquery ="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";

$newquery = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22$place%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
 
Last edited:

Bham Tony

New Member
Joined
Oct 4, 2015
Messages
21
Reaction score
2
https://www.igorkromin.net/index.ph...her-api-by-forcing-oauth-10-and-crippling-it/
:-/
The article in the link talks about yahoo weather now requiring oauth 1.0 authentication and says they are using openweathermap ... I signed up and did a query the results look look like this for "Bellingham WA":
Code:
{"coord":{"lon":-122.49,"lat":48.76},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"02d"}],"base":"cmc stations","main":{"temp":296.813,"pressure":1004.83,"humidity":43,"temp_min":296.813,"temp_max":296.813,"sea_level":1027.65,"grnd_level":1004.83},"wind":{"speed":0.92,"deg":55.0001},"clouds":{"all":8},"dt":1463172123,"sys":{"message":0.0076,"country":"US","sunrise":1463142554,"sunset":1463197453},"id":5786899,"name":"Bellingham","cod":200}
 
Last edited:

Bham Tony

New Member
Joined
Oct 4, 2015
Messages
21
Reaction score
2
I put together something that works for openweathermap but there seems to be an error when it runs the SET VARIABLE WEATHER $msg thing but the voice seems to read the weather OK.

DEBUG output:

Code:
Nerd Vittles Weather by OpenWeatherMap ver. 5.1.1a (c) Copyright 2007-2016, Ward Mundy. All Rights Reserved.

May 16, 2016 - 14:46:52  *** New session ***

agi_request: nv-weather-google.php
agi_channel: SIP/705-0000005e
agi_language: en
agi_type: SIP
agi_uniqueid: 1463424387.604
agi_version: 13.7.2
agi_callerid: 705
agi_calleridname: HandyTone
agi_callingpres: 0
agi_callingani2: 0
agi_callington: 0
agi_callingtns: 0
agi_dnid: 949
agi_rdnis: unknown
agi_context: from-internal
agi_extension: 949
agi_priority: 10
agi_enhanced: 0.0
agi_accountcode:
agi_threadid: 1869554720
agi_arg_1: Bellingham Washington

Location: Bellingham Washington
Query: http://api.openweathermap.org/data/2.5/weather?q='Bellingham%20Washington'&units=imperial&appid=1234567890abcdef
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":1463423381,"sys":{"message":0.0047,"country":"US","sunrise":1463401530,"sunset":1463456883},"id":5786899,"name":"Bellingham","cod":200}
Weather:
Forecast: "Here are the latest weather conditions and forecast for Bellingham Washington. Brought to you by Nerd Viddles. Currently: overcast clouds in Bellingham with a temperature of 54.33 degrees. Wind is from the southwest at 1.7 miles per hour. Sunrise today at 08:25:30 AM. Sunset today at 11:48:03 PM.  Have a great day. Goodbye."
SET VARIABLE WEATHER "Here are the latest weather conditions and forecast for Bellingham Washington. Brought to you by Nerd Viddles. Currently: overcast clouds in Bellingham with a temperature of 54.33 degrees. Wind is from the southwest at 1.7 miles per hour. Sunrise today at 08:25:30 AM. Sunset today at 11:48:03 PM.  Have a great day. Goodbye."
510 Invalid or unknown command
Could not figure out returned string, Returning code=510 result=0
 

Bham Tony

New Member
Joined
Oct 4, 2015
Messages
21
Reaction score
2
CODE:
Code:
#!/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 ;
?>
 

Members online

Forum statistics

Threads
25,810
Messages
167,754
Members
19,240
Latest member
nikko
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top