-
Notifications
You must be signed in to change notification settings - Fork 13
/
bmkg.php
212 lines (199 loc) · 11 KB
/
bmkg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* Script ini untuk import data dari BMKG ke database internal anda
* Sehingga jika dibutuhkan, data cuaca tidak perlu tembak lagi ke BMKG
* dari API nya sendiri, BMKG menyediakan sampai 3 hari ke depan
* Jalankan Script ini dari Command Line, gunakan Crontab agar di eksekusi tiap hari
* contoh crontab https://crontab.guru/#0_3_*_*_*
*
* Dibuat oleh Ibnu Maksum @ibnux
* Sumber BMKG: http://data.bmkg.go.id/
* */
date_default_timezone_set("Asia/Jakarta");
include "config.php";
// Propinsi yang tersedia di situs BMKG
$props = array('Aceh','Bali','BangkaBelitung','Banten','Bengkulu','DIYogyakarta','DKIJakarta','Gorontalo','Jambi','JawaBarat',
'JawaTengah','JawaTimur','KalimantanBarat','KalimantanSelatan','KalimantanTengah','KalimantanTimur',
'KalimantanUtara','KepulauanRiau','Lampung','Maluku','MalukuUtara','NusaTenggaraBarat','NusaTenggaraTimur',
'Papua','PapuaBarat','Riau','SulawesiBarat','SulawesiSelatan','SulawesiTengah','SulawesiTenggara','SulawesiUtara',
'SumateraBarat','SumateraSelatan','SumateraUtara');
//Kode Cuaca untuk ambil ikonnya
$kodeCuaca = array(
0 => "Cerah",
1 => "Cerah Berawan",
2 => "Cerah Berawan",
3 => "Berawan",
4 => "Berawan Tebal",
5 => "Udara Kabur",
100 => "Cerah",
101 => "Cerah Berawan",
102 => "Cerah Berawan",
103 => "Berawan",
104 => "Berawan Tebal",
10 => "Asap",
45 => "Berkabut",
60 => "Hujan Ringan",
61 => "Hujan Sedang",
63 => "Hujan Lebat",
80 => "Hujan Lokal",
95 => "Hujan Petir",
97 => "Hujan Petir"
);
//Mulai Looping tiap propinsi
foreach($props as $prop){
//Jika script tidak jalan, mungkin harus ganti url ini
$xml_string = file_get_contents("http://data.bmkg.go.id/datamkg/MEWS/DigitalForecast/DigitalForecast-".$prop.".xml");
$xml = simplexml_load_string($xml_string);
//Ubah array ke Json Structure String
$json = json_encode($xml);
//hapus @
$json = str_replace('"@attributes"','"attributes"',$json);
//Kembalikan menjadi Json, kayanya kerja 2x yah :D
$array = json_decode($json,TRUE);
//Masuk ke objek area
$areas = $array['forecast']['area'];
if(count($areas)>0){
foreach($areas as $area){
$idWilayah = $area['attributes']['id'];
//cek apakah sudah ada di database
if(!$db->has("t_wilayah",['id'=>$idWilayah])){
//Tambahkan ke tabel
$db->insert("t_wilayah",[
'id'=>$idWilayah,
'propinsi'=>$prop,
'kota'=>$area['name'][1],
'kecamatan'=>$area['name'][0],
'lat'=>$area['attributes']['latitude'],
'lon'=>$area['attributes']['longitude']]
);
//Cek lagi apakah sukses atau ngga, jika sukses tentu sudah ada di database
if($db->has("t_wilayah",['id'=>$idWilayah])){
echo $idWilayah." ".$area['attributes']['domain']." ".$area['attributes']['description']." ADDED\n";
}else{
echo $idWilayah." ".$area['attributes']['domain']." ".$area['attributes']['description']." FAILED\n";
}
}else{
echo $idWilayah." ".$area['attributes']['domain']." ".$area['attributes']['description']." EXISTS\n";
}
//parsing ramalan cuaca, data lain tidak diambil seperti arah angin
$params = $area['parameter'];
if(is_array($params) && count($params)>0){
foreach($params as $param){
//Jika data cuaca
if($param['attributes']['id']=='weather'){
//Tambahkan ke database
$times = $param['timerange'];
foreach($times as $tm){
$jam = $tm['attributes']['datetime'];
$y = substr($jam,0,4);
$m = substr($jam,4,2);
$d = substr($jam,6,2);
$h = substr($jam,8,2);
$i = substr($jam,10,2);
$value = (is_array($tm['value']))?$tm['value'][0]:$tm['value'];
if(!$db->has("t_cuaca",['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00"]])){
$db->insert("t_cuaca",
['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00",'kodeCuaca'=>$value,'cuaca'=>$kodeCuaca[$value]]);
if($db->has("t_cuaca",['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00"]])){
echo "jamCuaca $y-$m-$d $h:$i:00 kodeCuaca ".$value." INSERT\n";
}else{
echo "jamCuaca $y-$m-$d $h:$i:00 kodeCuaca ".$value." FAILED\n";
}
}else{
if(!$db->has("t_cuaca",['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00",'kodeCuaca'=>$value]])){
//ada perbedaan, update dong
$db->update("t_cuaca",
['kodeCuaca'=>$value,'cuaca'=>$kodeCuaca[$value]],
['AND'=>['idWilayah'=>$idWilayah, 'jamCuaca'=>"$y-$m-$d $h:$i:00"]]);
echo "jamCuaca $y-$m-$d $h:$i:00 kodeCuaca ".$value." UPDATE\n";
}else{
// isinya sama
echo "jamCuaca $y-$m-$d $h:$i:00 kodeCuaca ".$value." EXISTS\n";
}
}
}
//Jika data kelembaban
}else if($param['attributes']['id']=='hu'){
//Tambahkan ke database
$times = $param['timerange'];
foreach($times as $tm){
$jam = $tm['attributes']['datetime'];
$y = substr($jam,0,4);
$m = substr($jam,4,2);
$d = substr($jam,6,2);
$h = substr($jam,8,2);
$i = substr($jam,10,2);
$value = (is_array($tm['value']))?$tm['value'][0]:$tm['value'];
if(!$db->has("t_cuaca",['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00"]])){
$db->insert("t_cuaca",['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00",'humidity'=>$value]);
if($db->has("t_cuaca",['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00"]])){
echo "jamCuaca $y-$m-$d $h:$i:00 humidity ".$value." INSERT\n";
}else{
echo "jamCuaca $y-$m-$d $h:$i:00 humidity ".$value." FAILED\n";
}
}else{
if(!$db->has("t_cuaca",['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00",'humidity'=>$value]])){
//ada perbedaan, update dong
$db->update("t_cuaca",
['humidity'=>$value],
['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00"]]);
echo "jamCuaca $y-$m-$d $h:$i:00 humidity ".$value." UPDATE\n";
}else{
// isinya sama
echo "jamCuaca $y-$m-$d $h:$i:00 humidity ".$value." EXISTS\n";
}
}
}
//Jika data Temperatur
}else if($param['attributes']['id']=='t'){
//Tambahkan ke database
$times = $param['timerange'];
foreach($times as $tm){
$jam = $tm['attributes']['datetime'];
$y = substr($jam,0,4);
$m = substr($jam,4,2);
$d = substr($jam,6,2);
$h = substr($jam,8,2);
$i = substr($jam,10,2);
$value = $tm['value'];
print_r($value);
if(!$db->has("t_cuaca",['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00"]])){
$db->insert("t_cuaca",['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00",'tempC'=>$value[0][0],'tempF'=>$value[1][0]]);
if($db->has("t_cuaca",['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00"]])){
echo "jamCuaca $y-$m-$d $h:$i:00 temp ".$value[0][0]."-".$value[1][0]." INSERT\n";
}else{
echo "jamCuaca $y-$m-$d $h:$i:00 temp ".$value[0][0]."-".$value[1][0]." FAILED\n";
}
}else{
if(!$db->has("t_cuaca",['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00",'tempC'=>$value[0][0],'tempF'=>$value[1][0]]])){
//ada perbedaan, update dong
$db->update("t_cuaca",
['tempC'=>$value[0],'tempF'=>$value[1]],
['AND'=>['idWilayah'=>$idWilayah,'jamCuaca'=>"$y-$m-$d $h:$i:00"]]);
echo "jamCuaca $y-$m-$d $h:$i:00 temp ".$value[0][0]."-".$value[1][0]." UPDATE\n";
}else{
// isinya sama
echo "jamCuaca $y-$m-$d $h:$i:00 temp ".$value[0][0]."-".$value[1][0]." EXISTS\n";
}
}
}
}
}//foreach($params as $param){
}//if(count($params)>0){
}//foreach($areas as $area){
}//if(count($areas)>0){
}
// GENERATE FILE UNTUK HARI INI,
// HAPUS JIKA TIDAK BUTUH
$wilayah = $db->select("t_wilayah","*",["ORDER"=>"propinsi ASC"]);
//simpan ke file
file_put_contents("./cuaca/wilayah.json",json_encode($wilayah));
foreach($wilayah as $wil){
$id = $wil['id'];
$cuaca = $db->query('SELECT jamCuaca,kodeCuaca,cuaca,humidity,tempC,tempF FROM t_cuaca WHERE DATE(jamCuaca)>=DATE(NOW()) AND idWilayah='.$id.' ORDER BY jamCuaca ASC')->fetchAll(PDO::FETCH_ASSOC);
//simpan ke file
file_put_contents("./cuaca/$id.json",json_encode($cuaca));
}
//KIRIM KE GIT
//HAPUS jika tidak dibutuhkan
echo shell_exec('git add . && git commit -m "Update Cuaca tanggal '.date("d M Y H:i").' " && git push');