9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_TheFuckingWeather
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_TheFuckingWeather
23 * Detects and responds to requests for current weather conditions in a
24 * particular location using data from a web service.
27 * @package Phergie_Plugin_TheFuckingWeather
28 * @author Phergie Development Team <team@phergie.org>
29 * @license http://phergie.org/license New BSD License
30 * @link http://pear.phergie.org/package/Phergie_Plugin_TheFuckingWeather
31 * @link http://thefuckingweather.com
32 * @uses Phergie_Plugin_Command pear.phergie.org
33 * @uses Phergie_Plugin_Http pear.phergie.org
35 * @pluginDesc Detects and responds to requests for current weather
36 * conditions in a particular location.
39 class Phergie_Plugin_TheFuckingWeather extends Phergie_Plugin_Abstract
44 * @var Phergie_Plugin_Http
46 protected $http = null;
53 protected $url = 'http://www.thefuckingweather.com/?zipcode=';
56 * Checks for dependencies.
60 public function onLoad()
62 $pluginHandler = $this->getPluginHandler();
63 $pluginHandler->getPlugin('Command');
64 $this->http = $pluginHandler->getPlugin('Http');
68 * Returns the weather from the specified location.
70 * @param string $location Location term
73 * @todo Implement use of URL shortening here
74 * @pluginCmd [location] Detects and responds to requests for current
75 * weather conditions in a particular location.
77 public function onCommandThefuckingweather($location)
79 $source = $this->getEvent()->getSource();
80 $target = $this->getEvent()->getNick();
81 $out = $this->getWeather($location);
83 $this->doNotice($source, $out);
85 $this->doPrivmsg($source, $target . ': ' . $out);
90 * Alias for TheFuckingWeather command.
92 * @param string $location Location term
95 * @pluginCmd [location] Alias for thefuckingweather command.
97 public function onCommandTfw($location)
99 $this->onCommandThefuckingweather($location);
103 * Get the necessary content and returns the search result.
105 * @param string $location Location term
107 * @return string|bool Search result or FALSE if none is found
108 * @todo Try to optimize pregs
110 protected function getWeather($location)
112 $url = $this->url . urlencode($location);
113 $response = $this->http->get($url);
114 $content = $response->getContent();
117 '#<div><span class="small">(.*?)<\/span><\/div>#im',
120 $location = $matches[1][0];
122 if (!empty($location)) {
124 '#<div class="large" >(.*?)<br \/>#im',
127 $temp_numb = (int) $matches[1][0];
128 $temp_numb .= ' F / ' . round(($temp_numb - 32) / 1.8, 0) . ' C?!';
131 '#<br \/>(.*?)<\/div><div id="remark"><br \/>#im',
134 $temp_desc = $matches[1][0];
137 '#<div id="remark"><br \/>\n<span>(.*?)<\/span><\/div>#im',
140 $remark = $matches[1][0];
142 $result = "{$location}: {$temp_numb} {$temp_desc} ({$remark})";
143 $result = preg_replace('/</', ' <', $result);
144 $result = strip_tags($result);
145 return html_entity_decode($result);
147 return 'No fucking clue where that is.';