Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Further work on wunderground plugin

Parsing response
Handling bogus location
Building response string
  • Loading branch information...
commit 483d6df144a5c0d739650b1bb6ff5fe2be79f090 1 parent cb04ce8
zach authored

Showing 1 changed file with 102 additions and 7 deletions. Show diff stats Hide diff stats

  1. +102 7 Phergie/Plugin/Wunderground.php
109 Phergie/Plugin/Wunderground.php
@@ -42,9 +42,38 @@
42 42 class Phergie_Plugin_Wunderground extends Phergie_Plugin_Abstract
43 43 {
44 44 /**
45   - * Makes call to wunderground's api for conditions at a particular location.
  45 + * Tracks whether or not a given location is valid
  46 + *
  47 + * @var bool
  48 + */
  49 + private $bogusLocation = true;
  50 +
  51 + /**
  52 + * Checks for dependencies
  53 + *
  54 + * @return void
  55 + */
  56 +
  57 + public function onLoad()
  58 + {
  59 + $plugins = $this->getPluginHandler();
  60 + $plugins->getPlugin('Cache');
  61 + $plugins->getPlugin('Command');
  62 + $plugins->getPlugin('Http');
  63 +
  64 + if (empty($this->config['wunderground.api_key'])) {
  65 + $this->fail("API key must be specified.");
  66 + }
  67 + }
  68 +
  69 +
  70 + /**
  71 + * Makes call to wunderground's api for conditions at a particular location
  72 + *
46 73 * @param string $location
  74 + * @return void
47 75 */
  76 +
48 77 public function onCommandWeather($location)
49 78 {
50 79 $response = $this->getPluginHandler()
@@ -53,23 +82,89 @@ public function onCommandWeather($location)
53 82 'http://api.wunderground.com/api/' .
54 83 $this->getConfig('wunderground.api_key') .
55 84 '/conditions/q/' .
56   - $location);
  85 + $location . '.xml');
57 86
  87 + try {
  88 + $data = $this->parseWeatherInfo($response);
  89 + } catch (Phergie_Exception $pe) {
  90 + $this->doNotice($this->event->getNick(), $pe->getMessage());
  91 + }
  92 +
  93 + $bits = array();
  94 + $bits[] = 'Temperature: ' . $data['tempString'];
  95 + $bits[] = 'Weather: ' . $data['weather'];
  96 + $bits[] = 'Wind: ' . $data['wind_string'];
  97 + if ('NA' != $data['wind_chill_string']) {
  98 + $bits[] = "Wind Chill: " . $data['wind_chill_string'];
  99 + }
  100 + if ('NA' != $data['heat_index_string']) {
  101 + $bits[] = "Heat Index: " . $data['heat_index_string'];
  102 + }
  103 +
  104 + $string = 'Current Conditions: ' . implode(', ', $bits);
58 105
59   - $xml = new SimpleXMLElement($response->getContent());
  106 + $bogosity = $this->getBogusLocation();
60 107
61   - if (!$xml) {
62   - throw new Phergie_Exception("Error parsing XML content returned.");
  108 + if (!$bogosity) {
  109 + $this->doPrivmsg($this->event->getSource(),
  110 + $this->event->getNick(). ': ' .
  111 + $string);
63 112 }
  113 + }
  114 +
  115 + /**
  116 + * Chews on reply from wunderground's API and spits out useful information
  117 + *
  118 + * @param Phergie_Plugin_Http_Response $response
  119 + * @return array Array of useful information
  120 + * @throws Phergie_Exception
  121 + */
  122 +
  123 + public function parseWeatherInfo($response)
  124 + {
  125 + $xml = $response->getContent();
  126 +
  127 +// if (!$xml) {
  128 +// throw new Phergie_Exception("Error parsing XML content returned.");
  129 +// }
64 130
65 131 if (isset($xml->results)) {
66   - throw new Phergie_Exception("That location is too ambiguous. Please
67   - be more specific.");
  132 + $this->setBogusLocation(true);
  133 + throw new Phergie_Exception("That location is too ambiguous. Please be more specific.");
68 134 }
69 135
70 136 if (isset($xml->error)) {
  137 + $this->setBogusLocation(true);
71 138 throw new Phergie_Exception("That location was not found.");
72 139 }
73 140
  141 + $this->setBogusLocation(false);
  142 +
  143 + $co = $xml->current_observation;
  144 +
  145 + return array(
  146 + 'tempString' => $co->temperature_string,
  147 + 'weather' => $co->weather,
  148 + 'wind_string' => $co->wind_string,
  149 + 'wind_chill_string' => $co->windchill_string,
  150 + 'heat_index_string' => $co->heat_index_string);
  151 + }
  152 +
  153 + /**
  154 + * @return bool
  155 + */
  156 + public function getBogusLocation()
  157 + {
  158 + return $this->bogusLocation;
  159 + }
  160 +
  161 + /**
  162 + * @param bool $bogusLocation
  163 + * @return \Phergie_Plugin_Wunderground
  164 + */
  165 + public function setBogusLocation($bogusLocation)
  166 + {
  167 + $this->bogusLocation = $bogusLocation;
  168 + return $this;
74 169 }
75 170 }

0 comments on commit 483d6df

Please sign in to comment.
Something went wrong with that request. Please try again.