* @copyright 2008-2010 Phergie Development Team (http://phergie.org) * @license http://phergie.org/license New BSD License * @link http://pear.phergie.org/package/Phergie_Plugin_TheFuckingWeather */ /** * Detects and responds to requests for current weather conditions in a * particular location using data from a web service. * * @category Phergie * @package Phergie_Plugin_TheFuckingWeather * @author Phergie Development Team * @license http://phergie.org/license New BSD License * @link http://pear.phergie.org/package/Phergie_Plugin_TheFuckingWeather * @link http://thefuckingweather.com * @uses Phergie_Plugin_Command pear.phergie.org * @uses Phergie_Plugin_Http pear.phergie.org * * @pluginDesc Detects and responds to requests for current weather * conditions in a particular location. */ class Phergie_Plugin_TheFuckingWeather extends Phergie_Plugin_Abstract { /** * HTTP plugin * * @var Phergie_Plugin_Http */ protected $http = null; /** * Base API URL * * @var string */ protected $url = 'http://www.thefuckingweather.com/?zipcode='; /** * Checks for dependencies. * * @return void */ public function onLoad() { $pluginHandler = $this->getPluginHandler(); $pluginHandler->getPlugin('Command'); $this->http = $pluginHandler->getPlugin('Http'); } /** * Returns the weather from the specified location. * * @param string $location Location term * * @return void * @todo Implement use of URL shortening here * @pluginCmd [location] Detects and responds to requests for current * weather conditions in a particular location. */ public function onCommandThefuckingweather($location) { $source = $this->getEvent()->getSource(); $target = $this->getEvent()->getNick(); $out = $this->getWeather($location); if (!$out) { $this->doNotice($source, $out); } else { $this->doPrivmsg($source, $target . ': ' . $out); } } /** * Alias for TheFuckingWeather command. * * @param string $location Location term * * @return void * @pluginCmd [location] Alias for thefuckingweather command. */ public function onCommandTfw($location) { $this->onCommandThefuckingweather($location); } /** * Get the necessary content and returns the search result. * * @param string $location Location term * * @return string|bool Search result or FALSE if none is found * @todo Try to optimize pregs */ protected function getWeather($location) { $url = $this->url . urlencode($location); $response = $this->http->get($url); $content = $response->getContent(); preg_match_all( '#
(.*?)<\/span><\/div>#im', $content, $matches ); $location = $matches[1][0]; if (!empty($location)) { preg_match_all( '#
(.*?)
#im', $content, $matches ); $temp_numb = (int) $matches[1][0]; $temp_numb .= ' F / ' . round(($temp_numb - 32) / 1.8, 0) . ' C?!'; preg_match_all( '#
(.*?)<\/div>

#im', $content, $matches ); $temp_desc = $matches[1][0]; preg_match_all( '#

\n(.*?)<\/span><\/div>#im', $content, $matches ); $remark = $matches[1][0]; $result = "{$location}: {$temp_numb} {$temp_desc} ({$remark})"; $result = preg_replace('/