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
36 class Phergie_Plugin_TheFuckingWeather extends Phergie_Plugin_Abstract
41 * @var Phergie_Plugin_Http
43 protected $http = null;
50 protected $url = 'http://www.thefuckingweather.com/?zipcode=';
53 * Checks for dependencies.
57 public function onLoad()
59 $pluginHandler = $this->getPluginHandler();
60 $pluginHandler->getPlugin('Command');
61 $this->http = $pluginHandler->getPlugin('Http');
65 * Returns the weather from the specified location.
67 * @param string $location Location term
70 * @todo Implement use of URL shortening here
72 public function onCommandThefuckingweather($location)
74 $source = $this->getEvent()->getSource();
75 $target = $this->getEvent()->getNick();
76 $out = $this->getWeather($location);
78 $this->doNotice($source, $out);
80 $this->doPrivmsg($source, $target . ': ' . $out);
85 * Alias for TheFuckingWeather command.
87 * @param string $location Location term
91 public function onCommandTfw($location)
93 $this->onCommandThefuckingweather($location);
97 * Get the necessary content and returns the search result.
99 * @param string $location Location term
101 * @return string|bool Search result or FALSE if none is found
102 * @todo Try to optimize pregs
104 protected function getWeather($location)
106 $url = $this->url . urlencode($location);
107 $response = $this->http->get($url);
108 $content = $response->getContent();
111 '#<div><span class="small">(.*?)<\/span><\/div>#im',
114 $location = $matches[1][0];
116 if (!empty($location)) {
118 '#<div class="large" >(.*?)<br \/>#im',
121 $temp_numb = (int) $matches[1][0];
122 $temp_numb .= ' F / ' . round(($temp_numb - 32) / 1.8, 0) . ' C?!';
125 '#<br \/>(.*?)<\/div><div id="remark"><br \/>#im',
128 $temp_desc = $matches[1][0];
131 '#<div id="remark"><br \/>\n<span>(.*?)<\/span><\/div>#im',
134 $remark = $matches[1][0];
136 $result = "{$location}: {$temp_numb} {$temp_desc} ({$remark})";
137 $result = preg_replace('/</', ' <', $result);
138 $result = strip_tags($result);
139 return html_entity_decode($result);
141 return 'No fucking clue where that is.';