Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Further work on wunderground plugin

Parsing response
Handling bogus location
Building response string
  • Loading branch information...
commit 483d6df144a5c0d739650b1bb6ff5fe2be79f090 1 parent cb04ce8
zach authored
Showing with 102 additions and 7 deletions.
  1. +102 −7 Phergie/Plugin/Wunderground.php
View
109 Phergie/Plugin/Wunderground.php
@@ -42,9 +42,38 @@
class Phergie_Plugin_Wunderground extends Phergie_Plugin_Abstract
{
/**
- * Makes call to wunderground's api for conditions at a particular location.
+ * Tracks whether or not a given location is valid
+ *
+ * @var bool
+ */
+ private $bogusLocation = true;
+
+ /**
+ * Checks for dependencies
+ *
+ * @return void
+ */
+
+ public function onLoad()
+ {
+ $plugins = $this->getPluginHandler();
+ $plugins->getPlugin('Cache');
+ $plugins->getPlugin('Command');
+ $plugins->getPlugin('Http');
+
+ if (empty($this->config['wunderground.api_key'])) {
+ $this->fail("API key must be specified.");
+ }
+ }
+
+
+ /**
+ * Makes call to wunderground's api for conditions at a particular location
+ *
* @param string $location
+ * @return void
*/
+
public function onCommandWeather($location)
{
$response = $this->getPluginHandler()
@@ -53,23 +82,89 @@ public function onCommandWeather($location)
'http://api.wunderground.com/api/' .
$this->getConfig('wunderground.api_key') .
'/conditions/q/' .
- $location);
+ $location . '.xml');
+ try {
+ $data = $this->parseWeatherInfo($response);
+ } catch (Phergie_Exception $pe) {
+ $this->doNotice($this->event->getNick(), $pe->getMessage());
+ }
+
+ $bits = array();
+ $bits[] = 'Temperature: ' . $data['tempString'];
+ $bits[] = 'Weather: ' . $data['weather'];
+ $bits[] = 'Wind: ' . $data['wind_string'];
+ if ('NA' != $data['wind_chill_string']) {
+ $bits[] = "Wind Chill: " . $data['wind_chill_string'];
+ }
+ if ('NA' != $data['heat_index_string']) {
+ $bits[] = "Heat Index: " . $data['heat_index_string'];
+ }
+
+ $string = 'Current Conditions: ' . implode(', ', $bits);
- $xml = new SimpleXMLElement($response->getContent());
+ $bogosity = $this->getBogusLocation();
- if (!$xml) {
- throw new Phergie_Exception("Error parsing XML content returned.");
+ if (!$bogosity) {
+ $this->doPrivmsg($this->event->getSource(),
+ $this->event->getNick(). ': ' .
+ $string);
}
+ }
+
+ /**
+ * Chews on reply from wunderground's API and spits out useful information
+ *
+ * @param Phergie_Plugin_Http_Response $response
+ * @return array Array of useful information
+ * @throws Phergie_Exception
+ */
+
+ public function parseWeatherInfo($response)
+ {
+ $xml = $response->getContent();
+
+// if (!$xml) {
+// throw new Phergie_Exception("Error parsing XML content returned.");
+// }
if (isset($xml->results)) {
- throw new Phergie_Exception("That location is too ambiguous. Please
- be more specific.");
+ $this->setBogusLocation(true);
+ throw new Phergie_Exception("That location is too ambiguous. Please be more specific.");
}
if (isset($xml->error)) {
+ $this->setBogusLocation(true);
throw new Phergie_Exception("That location was not found.");
}
+ $this->setBogusLocation(false);
+
+ $co = $xml->current_observation;
+
+ return array(
+ 'tempString' => $co->temperature_string,
+ 'weather' => $co->weather,
+ 'wind_string' => $co->wind_string,
+ 'wind_chill_string' => $co->windchill_string,
+ 'heat_index_string' => $co->heat_index_string);
+ }
+
+ /**
+ * @return bool
+ */
+ public function getBogusLocation()
+ {
+ return $this->bogusLocation;
+ }
+
+ /**
+ * @param bool $bogusLocation
+ * @return \Phergie_Plugin_Wunderground
+ */
+ public function setBogusLocation($bogusLocation)
+ {
+ $this->bogusLocation = $bogusLocation;
+ return $this;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.