commit | author | age
|
8bd4d9
|
1 |
<?php |
JK |
2 |
class pogoda implements module { |
|
3 |
static $wojewodztwa = array( |
|
4 |
'Kuiavia-Pomerania' => 'kujawsko-pomorskie', |
|
5 |
'Kuyavian-Pomeranian' => 'kujawsko-pomorskie', |
|
6 |
'Kujawsko-Pomorskie' => 'kujawsko-pomorskie', |
|
7 |
|
|
8 |
'Lesser Poland' => 'małopolskie', |
|
9 |
|
|
10 |
'Lodz' => 'łódzkie', |
|
11 |
'Łódź' => 'łódzkie', |
|
12 |
|
|
13 |
'Lower Silesia' => 'dolnośląskie', |
|
14 |
'Lower Silesian' => 'dolnośląskie', |
|
15 |
|
|
16 |
'Lublin' => 'lubelskie', |
|
17 |
'Lubelskie' => 'lubelskie', |
|
18 |
|
|
19 |
'Lubuskie' => 'lubuskie', |
|
20 |
|
|
21 |
'Mazovia' => 'mazowieckie', |
|
22 |
'Masovian' => 'mazowieckie', |
|
23 |
'Mazowieckie' => 'mazowieckie', |
|
24 |
|
|
25 |
'Opole' => 'opolskie', |
|
26 |
|
|
27 |
'Subcarpathia' => 'podkarpackie', |
|
28 |
'Podkarpackie' => 'podkarpackie', |
|
29 |
|
|
30 |
'Podlachia' => 'podlaskie', |
|
31 |
|
|
32 |
'Pomerania' => 'pomorskie', |
|
33 |
'Pomorskie' => 'pomorskie', |
|
34 |
|
|
35 |
'Silesia' => 'śląskie', |
|
36 |
|
|
37 |
'Swietokrzyskie' => 'świętokrzyskie', |
|
38 |
|
|
39 |
'Warmia and Masuria' => 'warmińsko-mazurskie', |
|
40 |
|
|
41 |
'Western Pomerania' => 'zachodniopomorskie', |
|
42 |
|
|
43 |
'Greater Poland' => 'wielkopolskie', |
|
44 |
'Wielkopolskie' => 'wielkopolskie', |
|
45 |
); |
|
46 |
|
|
47 |
static function register_cmd() { |
|
48 |
return array( |
|
49 |
'pogoda' => 'cmd_pogoda', |
|
50 |
'p' => 'cmd_pogoda', |
|
51 |
'weather' => 'cmd_pogoda', |
|
52 |
'miasto' => 'cmd_miasto', |
|
53 |
'm' => 'cmd_miasto', |
|
54 |
'temp' => 'cmd_pogoda', |
|
55 |
'temperatura' => 'cmd_pogoda', |
|
56 |
); |
|
57 |
} |
|
58 |
|
|
59 |
static function help($cmd=NULL) { |
|
60 |
if($cmd === NULL) { |
|
61 |
GGapi::putRichText('pogoda ', TRUE); |
|
62 |
GGapi::putRichText('[miasto]', FALSE, TRUE); |
|
63 |
GGapi::putRichText("\n".' Podaje pogodę dla miasta'."\n"); |
|
64 |
|
|
65 |
GGapi::putRichText('miasto ', TRUE); |
|
66 |
GGapi::putRichText('miasto', FALSE, TRUE); |
|
67 |
GGapi::putRichText("\n".' Ustala domyślne miasto dla funkcji pogoda'."\n\n"); |
|
68 |
} |
|
69 |
elseif(substr($cmd, 0, 1)=='m') |
|
70 |
{ |
|
71 |
GGapi::putRichText('miasto ', TRUE); |
|
72 |
GGapi::putRichText('miasto', FALSE, TRUE); |
|
73 |
GGapi::putRichText(' (alias: '); |
|
74 |
GGapi::putRichText('m', TRUE); |
|
75 |
GGapi::putRichText(')'."\n".' Ustawia domyślne '); |
|
76 |
GGapi::putRichText('miasto', FALSE, TRUE); |
|
77 |
GGapi::putRichText(' dla funkcji pogoda dla danego numeru Gadu-Gadu.'); |
|
78 |
} |
|
79 |
else |
|
80 |
{ |
|
81 |
GGapi::putRichText('pogoda ', TRUE); |
|
82 |
GGapi::putRichText('miasto', FALSE, TRUE); |
|
83 |
GGapi::putRichText(' (alias: '); |
|
84 |
GGapi::putRichText('p', TRUE); |
|
85 |
GGapi::putRichText(')'."\n".' Podaje pogodę dla danego miasta na najbliższe dni. Domyślne miasto można ustawić komendą '); |
|
86 |
GGapi::putRichText('miasto', TRUE); |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
static function cmd_pogoda($name, $arg) { |
|
91 |
if(empty($arg)) { |
|
92 |
$arg = database::get($_GET['from'], 'pogoda', 'miasto'); |
|
93 |
if(empty($arg)) { |
|
94 |
$arg = GGapi::getPublicData(); |
|
95 |
$arg = trim($arg['city']); |
|
96 |
if(empty($arg)) { |
|
97 |
$arg = 'Warszawa'; |
|
98 |
$forced = TRUE; |
|
99 |
} |
|
100 |
GGapi::putText('Nie ustawiono miasta (pomoc - wpisz: help miasto) - '.(!$forced ? 'na podstawie danych z katalogu publicznego ' : '').'wybieram '.$arg."\n\n"); |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
$dane = @file_get_contents('http://www.google.pl/ig/api?weather='.urlencode(ucwords(funcs::utfToAscii($arg)))); |
|
105 |
if(!$dane) { |
|
106 |
GGapi::putText('Przepraszamy, nie udało się połączyć z serwisem'); |
|
107 |
return; |
|
108 |
} |
|
109 |
|
|
110 |
$dane = iconv('iso-8859-2', 'utf-8', $dane); |
|
111 |
|
|
112 |
$dane = @simplexml_load_string($dane); |
|
113 |
if(!$dane) { |
|
114 |
GGapi::putText('Przepraszamy, błąd przy pobieraniu danych'); |
|
115 |
return; |
|
116 |
} |
|
117 |
|
|
118 |
if($dane->weather->problem_cause) { |
|
119 |
GGapi::putText('Problem w serwisie bądź danego miasta nie ma w bazie'."\n\n".'Przykład:'."\n".'pogoda Warszawa'."\n".'pogoda Kraków'); |
|
120 |
return; |
|
121 |
} |
|
122 |
|
|
123 |
$short2day = array( |
|
124 |
'pon.' => 'Poniedziałek', |
|
125 |
'wt.' => 'Wtorek', |
|
126 |
'śr.' => 'Środa', |
|
127 |
'czw.' => 'Czwartek', |
|
128 |
'pt.' => 'Piątek', |
|
129 |
'sob.' => 'Sobota', |
|
130 |
'niedz.' => 'Niedziela', |
|
131 |
); |
|
132 |
|
|
133 |
$region = substr(strstr($dane->weather->forecast_information->city['data'], ', '), 2); |
|
134 |
$region = trim(str_replace('Voivodeship', '', $region)); |
|
135 |
if(isset(self::$wojewodztwa[$region])) { |
|
136 |
$region = 'województwo '.self::$wojewodztwa[$region]; |
|
137 |
} |
|
138 |
|
|
139 |
$miasto = trim((string)$dane->weather->forecast_information->postal_code['data']); |
|
140 |
if(($a=strpos($miasto, '-'))!==FALSE) { |
|
141 |
$miasto = substr($miasto, 0, $a).'-'.ucfirst(substr($miasto, $a+1)); |
|
142 |
} |
|
143 |
|
|
144 |
GGapi::putRichText('Pogoda dla miasta '.$miasto.', '.$region."\n\n", TRUE); |
|
145 |
|
|
146 |
GGapi::putRichText('Teraz'."\n", TRUE); |
|
147 |
$icon = (string)$dane->weather->current_conditions->icon['data']; |
|
148 |
if(!empty($icon)) { |
|
149 |
if(!file_exists('./data/pogoda/'.basename($icon))) { |
7954aa
|
150 |
if(substr($icon, 0, 1) == '/') { |
JK |
151 |
$img = 'http://www.google.com'.$img; |
|
152 |
} |
8bd4d9
|
153 |
$img = @file_get_contents($icon); |
JK |
154 |
if($img) { |
|
155 |
file_put_contents('./data/pogoda/'.basename($icon), $img); |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
GGapi::putImage('./data/pogoda/'.basename($icon)); |
|
160 |
$txt = "\n"; |
|
161 |
} |
|
162 |
$condition = (string)$dane->weather->current_conditions->condition['data']; |
|
163 |
GGapi::putRichText($txt.($condition ? $condition."\n" : '').'Temp.: '.($dane->weather->current_conditions->temp_c['data']).'°C'."\n".($dane->weather->current_conditions->humidity['data'])."\n".($dane->weather->current_conditions->wind_condition['data'])); |
|
164 |
|
|
165 |
$num = TRUE; |
|
166 |
foreach($dane->weather->forecast_conditions as $day) { |
|
167 |
GGapi::putRichText("\n\n".($num ? 'Później' : $short2day[(string)$day->day_of_week['data']])."\n", TRUE); |
|
168 |
if(!file_exists('./data/pogoda/'.basename($day->icon['data']))) { |
|
169 |
$img = @file_get_contents($day->icon['data']); |
|
170 |
if($img) { |
|
171 |
file_put_contents('./data/pogoda/'.basename($day->icon['data']), $img); |
|
172 |
} |
|
173 |
} |
|
174 |
GGapi::putImage('./data/pogoda/'.basename($day->icon['data'])); |
|
175 |
GGapi::putRichText("\n".($day->condition['data'])."\n".'Temp. od '.($day->low['data']).'°C do '.($day->high['data']).'°C'); |
|
176 |
$num = FALSE; |
|
177 |
} |
|
178 |
|
|
179 |
} |
|
180 |
|
|
181 |
static function cmd_miasto($name, $arg) { |
|
182 |
$arg = trim(funcs::utfToAscii($arg)); |
|
183 |
if(empty($arg)) { |
|
184 |
$arg = database::get($_GET['from'], 'pogoda', 'miasto'); |
|
185 |
if(!empty($arg)) { |
|
186 |
GGapi::putText('Aktualnie ustawione miasto to: '.$arg); |
|
187 |
return; |
|
188 |
} |
|
189 |
|
|
190 |
$arg = GGapi::getPublicData(); |
|
191 |
$arg = funcs::utfToAscii($arg['city']); |
|
192 |
|
|
193 |
if(empty($arg)) { |
|
194 |
GGapi::putText('Nie podano wymaganego argumentu '); |
|
195 |
GGapi::putRichText('miasto', FALSE, TRUE); |
|
196 |
return; |
|
197 |
} |
|
198 |
|
|
199 |
|
|
200 |
GGapi::putText('Z katalogu publicznego pobrano miasto '.$arg."\n\n"); |
|
201 |
} |
|
202 |
|
|
203 |
$data = @file_get_contents('http://ws.geonames.org/search?name='.urlencode($arg)); |
|
204 |
if($data) { |
|
205 |
$data = simplexml_load_string($data); |
|
206 |
if($data && $data->totalResultsCount > 0) { |
|
207 |
$data = $data->geoname[0]; |
|
208 |
} |
|
209 |
else |
|
210 |
{ |
|
211 |
GGapi::putText('Podane miasto nie zostało odnalezione!'); |
|
212 |
return; |
|
213 |
} |
|
214 |
} |
|
215 |
else |
|
216 |
{ |
|
217 |
GGapi::putText('Wystąpił błąd przy wyszukiwaniu miasta. Spróbuj ponownie później.'); |
|
218 |
return; |
|
219 |
} |
|
220 |
|
|
221 |
if(!$data->geonameId || $data->geonameId=='756135') { |
|
222 |
$data = new SimpleXMLElement('<geoname><name>Warszawa</name><lat>52.25</lat><lng>21.0</lng><geonameId>756135</geonameId><countryCode>PL</countryCode><countryName>Poland</countryName></geoname>'); |
|
223 |
} |
|
224 |
|
|
225 |
GGapi::putText('Miasto zostało ustawione na '.(string)$data->name); |
|
226 |
database::add($_GET['from'], 'pogoda', 'miasto', (string)$data->name); |
|
227 |
database::add($_GET['from'], 'pogoda', 'kraj', (string)$data->countryName); |
|
228 |
database::add($_GET['from'], 'pogoda', 'cc', (string)$data->countryCode); |
|
229 |
database::add($_GET['from'], 'pogoda', 'geo', array('lat' => (string)$data->lat, 'lon' => (string)$data->lng)); |
|
230 |
} |
|
231 |
} |
7954aa
|
232 |
?> |