]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/TheFuckingWeather.php
e8a02860b2c7ec68f19ab7e727f0f663b554cb40
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / TheFuckingWeather.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
23  * Detects and responds to requests for current weather conditions in a
24  * particular location using data from a web service.
25  *
26  * @category Phergie
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
34  *
35  * @pluginDesc Detects and responds to requests for current weather 
36  *             conditions in a particular location.
37  */
38
39 class Phergie_Plugin_TheFuckingWeather extends Phergie_Plugin_Abstract
40 {
41     /**
42      * HTTP plugin
43      *
44      * @var Phergie_Plugin_Http
45      */
46     protected $http = null;
47
48     /**
49      * Base API URL
50      *
51      * @var string
52      */
53     protected $url = 'http://www.thefuckingweather.com/?zipcode=';
54
55     /**
56      * Checks for dependencies.
57      *
58      * @return void
59      */
60     public function onLoad()
61     {
62         $pluginHandler = $this->getPluginHandler();
63         $pluginHandler->getPlugin('Command');
64         $this->http = $pluginHandler->getPlugin('Http');
65     }
66
67     /**
68      * Returns the weather from the specified location.
69      *
70      * @param string $location Location term
71      *
72      * @return void
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.
76      */
77     public function onCommandThefuckingweather($location)
78     {
79         $source = $this->getEvent()->getSource();
80         $target = $this->getEvent()->getNick();
81         $out = $this->getWeather($location);
82         if (!$out) {
83             $this->doNotice($source, $out);
84         } else {
85             $this->doPrivmsg($source, $target . ': ' . $out);
86         }
87     }
88
89     /**
90     * Alias for TheFuckingWeather command.
91     *
92     * @param string $location Location term
93     *
94     * @return void
95     * @pluginCmd [location] Alias for thefuckingweather command.
96     */
97     public function onCommandTfw($location)
98     {
99         $this->onCommandThefuckingweather($location);
100     }
101
102     /**
103      * Get the necessary content and returns the search result.
104      *
105      * @param string $location Location term
106      *
107      * @return string|bool Search result or FALSE if none is found
108      * @todo Try to optimize pregs
109      */
110     protected function getWeather($location)
111     {
112         $url = $this->url . urlencode($location);
113         $response = $this->http->get($url);
114         $content = $response->getContent();
115
116         preg_match_all(
117             '#<div><span class="small">(.*?)<\/span><\/div>#im',
118             $content, $matches
119         );
120         $location = $matches[1][0];
121
122         if (!empty($location)) {
123             preg_match_all(
124                 '#<div class="large" >(.*?)<br \/>#im',
125                 $content, $matches
126             );
127             $temp_numb = (int) $matches[1][0];
128             $temp_numb .= ' F / ' . round(($temp_numb - 32) / 1.8, 0) . ' C?!';
129
130             preg_match_all(
131                 '#<br \/>(.*?)<\/div><div  id="remark"><br \/>#im',
132                 $content, $matches
133             );
134             $temp_desc = $matches[1][0];
135
136             preg_match_all(
137                 '#<div  id="remark"><br \/>\n<span>(.*?)<\/span><\/div>#im',
138                 $content, $matches
139             );
140             $remark = $matches[1][0];
141
142             $result = "{$location}: {$temp_numb} {$temp_desc} ({$remark})";
143             $result = preg_replace('/</', ' <', $result);
144             $result = strip_tags($result);
145             return html_entity_decode($result);
146         } else {
147             return 'No fucking clue where that is.';
148         }
149     }
150 }