Skip to content

Instantly share code, notes, and snippets.

@MattRead
Created November 22, 2011 14:56
Show Gist options
  • Save MattRead/1385848 to your computer and use it in GitHub Desktop.
Save MattRead/1385848 to your computer and use it in GitHub Desktop.
weather
<?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