Created
November 22, 2011 14:56
-
-
Save MattRead/1385848 to your computer and use it in GitHub Desktop.
weather
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function onDoWeather($where, $day = null) | |
{ | |
$target = $this->event->getNick(); | |
$opts = array( | |
'http' => array( | |
'timeout' => 3.5, | |
'method' => 'GET', | |
'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12' | |
) | |
); | |
$context = stream_context_create($opts); | |
$contents = file_get_contents(sprintf('http://www.google.com/ig/api?weather=%s', urlencode($where)), false, $context); | |
if (!$contents) { | |
return; | |
} | |
$a = simplexml_load_string($contents); | |
if ($day) { | |
foreach( $a->weather->forecast_conditions as $f) { | |
if (strtolower($f->day_of_week['data']) == $day) { | |
$low = round(($f->low['data'] - 32)/1.8); | |
$high = round(($f->high['data'] - 32)/1.8); | |
$weather = "Conditions for {$a->weather->forecast_information->city['data']} on {$f->day_of_week['data']}: Low: {$low}C, High: {$high}C, Condition: {$f->con$ | |
} | |
} | |
} | |
else { | |
$b = $a->weather->current_conditions; | |
$weather = "Current conditions for {$a->weather->forecast_information->city['data']}: {$b->temp_c['data']}C, {$b->condition['data']}, {$b->humidity['data']}, {$b->w$ | |
} | |
if (!$a->weather->current_conditions) { | |
$this->doPrivmsg($this->event->getSource(), $target . ': no weather in ' . $where); | |
} | |
else { | |
$this->doPrivmsg($this->event->getSource(), $target . ': ' . $weather); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment