-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather_dust_only.php
155 lines (121 loc) · 4.9 KB
/
gather_dust_only.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
<?php
include_once ('config/database.php');
//include_once ('header.php');
class DustClass{
public function getDustName(){
//fetch the table names
$db = new Database();
$conn = $db->getConnection();
//$sql = "SELECT * FROM tbl_dust_data;";
//more readable
$sql ="SELECT `NAME_HGL`, `NAME_DUST` FROM `province`";
$result = mysqli_query($conn, $sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($this->conn));
exit();
}
$row = $result->fetch_assoc();
if($row > 0){
while($row = $result->fetch_assoc())
{
$data[] = $row;
}
}
mysqli_close($conn);
return($data);
}
public function NeedUpdate($first_data){
$enc = urlencode($first_data['NAME_DUST']);
$api_url = "http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnRltmMesureDnsty?sidoName=".$enc."&pageNo=1&numOfRows=200&ServiceKey=5rngHQO75rpCVULQ4UNMIXpRWSMzzTduA%2F2N47nrYL3Tc4sHHCAJ3F0k61NC05iQLdrR20%2Bur41a9nEPmpVhtQ%3D%3D&&ver=1.3";
$xmlstring = simplexml_load_file($api_url);
$json = json_encode($xmlstring);
$array = json_decode($json,TRUE);
$res['dataTime'] = $array['body']['items']['item'][0]['dataTime'];
$db = new Database();
$conn = $db->getConnection();
$sql ="SELECT `IDX` FROM `tbl_dust_data` WHERE `IDX_DATETIME` = '".$res['dataTime']."'";
$result = mysqli_query($conn, $sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($this->conn));
exit();
}
mysqli_close($conn);
$row = $result->fetch_assoc();
if($row > 0)
return FALSE;
else
return TRUE;
}
public function insertDustDataNew($theData, $prov_name){
$db = new Database();
$conn = $db->getConnection();
//var_dump($theData);
for($i=0; $i<sizeof($theData)-1; $i++){
//find the key
$sql = "SELECT `IDX` FROM `tbl_full_location` WHERE `Phase_3` LIKE '%".$theData[$i]['city']."%'";
$result = mysqli_query($conn, $sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($this->conn));
exit();
}
$row = $result->fetch_assoc();
if(!$row){
continue;
}
//var_dump($row);
$sql ="INSERT INTO `tbl_dust_data`(`IDX_DATETIME`, `ITEMCODE`, `LOCATION`, `VALUE_1`)
VALUES ('".$theData['dataTime']."','PM10','".$row['IDX']."','".$theData[$i]['val']."')";
//var_dump($sql);
$result = mysqli_query($conn, $sql);
if (!$result) {
printf("Error: %s\n", mysqli_error(conn));
exit();
}
//var_dump($sql);
}
mysqli_close($conn);
}
public function getSingleProvince($single_province_name){
//print_r($single_province_name['NAME_DUST']);
$enc = urlencode($single_province_name['NAME_DUST']);
//$api_url = "http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnRltmMesureDnsty?sidoName=%EB%B6%80%EC%82%B0&pageNo=1&numOfRows=10&ServiceKey=5rngHQO75rpCVULQ4UNMIXpRWSMzzTduA%2F2N47nrYL3Tc4sHHCAJ3F0k61NC05iQLdrR20%2Bur41a9nEPmpVhtQ%3D%3D&&ver=1.3";
//$api_url = "http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnRltmMesureDnsty?sidoName=서울&pageNo=1&numOfRows=10&ServiceKey=5rngHQO75rpCVULQ4UNMIXpRWSMzzTduA%2F2N47nrYL3Tc4sHHCAJ3F0k61NC05iQLdrR20%2Bur41a9nEPmpVhtQ%3D%3D&&ver=1.3";
$api_url = "http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnRltmMesureDnsty?sidoName=".$enc."&pageNo=1&numOfRows=200&ServiceKey=5rngHQO75rpCVULQ4UNMIXpRWSMzzTduA%2F2N47nrYL3Tc4sHHCAJ3F0k61NC05iQLdrR20%2Bur41a9nEPmpVhtQ%3D%3D&&ver=1.3";
$xmlstring = simplexml_load_file($api_url);
$json = json_encode($xmlstring);
$array = json_decode($json,TRUE);
//print_r($enc);
//print_r($api_url);
//print_r(sizeof($array['body']['items']['item']));
//single time
//if($first == TRUE){
$res['dataTime'] = $array['body']['items']['item'][0]['dataTime'];
//}
for($i=0 ; $i < sizeof($array['body']['items']['item']); $i++){
$res[$i]['city'] = $array['body']['items']['item'][$i]['stationName'];
$res[$i]['val'] = $array['body']['items']['item'][$i]['pm10Value'];
}
return ($res);
}
}
$api = new DustClass();
$province_names = $api->getDustName();
//print_r($province_names);
//$i=0;
//$res[$province_names[$i]['NAME_HGL']] = $api->getSingleProvince($province_names[$i]);
//$api->insertDustDataNew($res[$province_names[$i]['NAME_HGL']], $province_names[$i]['NAME_HGL']);
if($api->NeedUpdate($province_names[0])){
//fetch data
for($i=0; $i<sizeof($province_names)-1; $i++){
//$temp = $province_names[$i];
//fetch
$res[$province_names[$i]['NAME_HGL']] = $api->getSingleProvince($province_names[$i]);
$api->insertDustDataNew($res[$province_names[$i]['NAME_HGL']], $province_names[$i]['NAME_HGL']);
//sleep(1.2);
}
print_r("Data updated");
}else{
print_r("Latest data already exist");
}
//print_r($res);
?>