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