This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
233 lines (199 loc) · 7.06 KB
/
functions.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
function week_day_name($i) {
switch ($i) {
case 0:
$push = "Domingo";
break;
case 1:
$push = "Segunda-feira";
break;
case 2:
$push = "Terça-feira";
break;
case 3:
$push = "Quarta-feira";
break;
case 4:
$push = "Quinta-feira";
break;
case 5:
$push = "Sexta-feira";
break;
case 6:
$push = "Sábado";
break;
}
return $push;
}
function utils_days($date_start, $date_end, $holydays = []) {
$start = strtotime($date_start);
$end = strtotime($date_end);
$days_quantity = 0;
while ($start <= $end) {
$day_weekend = (date('D', $start) === 'Sat' || date('D', $start) === 'Sun');
$day_holyday = (count($holydays) && in_array(date('Y-m-d', $start), $holydays));
$start += 86400; // 86400 quantidade de segundos em um dia
if ($day_weekend || $day_holyday) {
continue;
}
$days_quantity++;
}
return $days_quantity;
}
function minutes_to_readable($minutes) {
$zero = new DateTime('@0');
$offset = new DateTime('@' . $minutes * 60);
$diff = $zero->diff($offset);
$hour = $diff->format('%h');
$minute = $diff->format('%i');
$readable_hour = ($hour > 1) ? 'horas' : 'hora';
$readable_minute = ($minute > 1) ? 'minutos' : 'minuto';
$push = (($hour >= 1) ? $hour.' '.$readable_hour.(($minute >= 1) ? ' e ' : '') : '').(($minute >= 1) ? $minute.' '.$readable_minute : '');
return $push;
}
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
function get_events_by_date($date, $schedule, $url) {
$week_day = date('w', strtotime($date));
echo '---'.PHP_EOL;
echo 'Date: '.$date.' ('.week_day_name($week_day).')'.PHP_EOL;
if(get_http_response_code($url.$date) == '200'){
$source = file_get_contents($url.$date);
$dom = new DOMDocument();
libxml_use_internal_errors(TRUE);
$dom->loadHTML($source);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$rows = $xpath->query('//li[@class="item-compromisso-wrapper"]');
$events = [];
$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
$mysqli->set_charset("utf8");
$i = 1;
foreach($rows as $data) {
$title = $xpath->query('.//h4[contains(@class, "compromisso-titulo")]', $data)[0]->nodeValue;
if( trim($title) == 'Sem compromisso oficial' ) {
echo 'Sem compromisso oficial'.PHP_EOL;
continue;
}
if( isset($xpath->query('.//div[contains(@class, "compromisso-participantes")]', $data)[0]) ) {
$participants = trim($xpath->query('.//div[contains(@class, "compromisso-participantes")]//ul', $data)[0]->nodeValue);
$title = $title.'; '.$participants;
}
$title = preg_replace('/\s+/', ' ', $title);
$hours = $xpath->query('.//div[@class="horario"]', $data)[0]->nodeValue;
$hours = explode('-', $hours);
$hour_start = trim(str_replace('h',':',$hours[0])).':00';
if( !isset($hours[1]) ) {
$hour_end = '00:00:00';
$interval = 0;
} else {
$hour_end = trim(str_replace('h',':',$hours[1])).':00';
$time1 = new DateTime($hour_start);
$time2 = new DateTime($hour_end);
$diff = $time1->diff($time2);
$interval = ($diff->days * 24 * 60) + ($diff->h * 60) + $diff->i;
}
if( isset($xpath->query('.//div[@class="compromisso-local"]', $data)[0]) ) {
$place = $xpath->query('.//div[@class="compromisso-local"]', $data)[0]->nodeValue;
} else {
$place = null;
}
$event = "SELECT * FROM `events` WHERE `date` = '".$date."' AND `week_day` = ".$week_day." AND `hour_start` = '".$hour_start."' AND `hour_end` = '".$hour_end."' AND `interval` = '".$interval."' AND `title` = '".$title."' AND `place` = '".$place."' AND `schedule_id` = '".$schedule."' LIMIT 1;";
$event_query = mysqli_query($mysqli, $event);
if (mysqli_num_rows($event_query) > 0) {
echo '= Event already registered'.PHP_EOL;
} else {
echo '+ Insert event'.PHP_EOL;
$query = "INSERT INTO `events` (
`date`,
`week_day`,
`hour_start`,
`hour_end`,
`interval`,
`title`,
`place`,
`schedule_id`
) VALUES (
'".$date."',
".$week_day.",
'".$hour_start."',
'".$hour_end."',
'".$interval."',
'".$title."',
'".$place."',
'".$schedule."'
);";
mysqli_query($mysqli, $query);
}
echo '['.$i.']: '.$title.PHP_EOL;
echo 'Hours: '.$hour_start.' ~ '.$hour_end.PHP_EOL;
echo 'Interval: '.$interval.' minutes'.PHP_EOL;
echo 'Place: '.$place.PHP_EOL;
$i++;
}
} else {
echo 'Invalid source'.PHP_EOL;
}
}
function query_schedule($schedule = null) {
$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
$mysqli->set_charset("utf8");
$array = [];
$events = "SELECT * FROM `events` WHERE `schedule_id` = '".$schedule."'";
$events_query = mysqli_query($mysqli, $events);
while($data = mysqli_fetch_array($events_query)) {
$date = $data['date'];
$week_day = $data['week_day'];
$hour_start = $data['hour_start'];
$hour_end = $data['hour_end'];
$interval = $data['interval'];
$title = $data['title'];
$place = $data['place'];
$push = array(
'date' => $date,
'week_day' => $week_day,
'week_day_readable' => week_day_name($week_day),
'hour_start' => $hour_start,
'hour_end' => $hour_end,
'minutes' => $interval,
'minutes_readable' => minutes_to_readable($interval),
'event_description' => (empty($title)) ? null : $title,
'event_local' => (empty($place)) ? null : $place
);
array_push($array, $push);
}
$json = json_encode($array, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
file_put_contents(dirname(__FILE__).'/api/'.$schedule.'.json', $json);
$push = fopen(dirname(__FILE__).'/api/'.$schedule.'.csv', 'w');
$header = array(
'date',
'week_day',
'week_day_readable',
'hour_start',
'hour_end',
'minutes',
'minutes_readable',
'event_description',
'event_local'
);
fputcsv($push, $header);
foreach($array as $data) {
$row = array(
$data['date'],
$data['week_day'],
$data['week_day_readable'],
$data['hour_start'],
$data['hour_end'],
$data['minutes'],
$data['minutes_readable'],
$data['event_description'],
$data['event_local']
);
fputcsv($push, $row);
}
fclose($push);
}