-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrapNcdcWebsite.php
92 lines (82 loc) · 2.98 KB
/
ScrapNcdcWebsite.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
<?php
namespace App\Services;
use Stringy\StaticStringy;
class ScrapNcdcWebsite extends BaseService
{
const NCDC_WEBSITE = 'https://covid19.ncdc.gov.ng/';
const SEARCH_CONTENT = ['<td>', '</td>', '<tr>', '</tr>'];
public function run()
{
return $this->keyStateWithStats();
}
/**
* @return array
* NB
* $cc stands for confirmed cases
* $ac stands for active cases
* $rc stands for recovered cases
* $dc stands for dead cases
*/
protected function keyStateWithStats()
{
$keyed_cases = [];
$total_confirmed_cases = 0;
$total_active_cases = 0;
$total_recovered_cases = 0;
$total_dead_cases = 0;
$cases = $this->removeHtmlTableCharacters();
foreach ($cases as $case) {
$exploded = explode(' ', $case);
$keyed_cases[] = [
$exploded[0] => [
'confirmed_cases' => $cc = (integer) str_replace(',', '', $exploded[1]),
'active_cases' => $ac = (integer)str_replace(',', '', $exploded[2]),
'recovered_cases' => $rc = (integer)str_replace(',', '', $exploded[3]),
'dead_cases' => $dc = (integer)str_replace(',', '', $exploded[4])
]
];
$total_confirmed_cases = $total_confirmed_cases + $cc;
$total_active_cases = $total_active_cases + $ac;
$total_recovered_cases = $total_recovered_cases + $rc;
$total_dead_cases = $total_dead_cases + $dc;
}
$keyed_cases[] = [
'total' => [
'total_confirmed_cases' => $total_confirmed_cases,
'total_active_cases' => $total_active_cases,
'total_recovered_cases' => $total_recovered_cases,
'total_dead_cases' => $total_dead_cases
]
];
return $keyed_cases;
}
protected function removeHtmlTableCharacters()
{
$removed_trs = [];
$table_case_focus = $this->tableCaseFocus();
$string_content = StaticStringy::between($table_case_focus, '<tbody>', '</tbody>');
$exploded_content = explode('</tr> <tr>', $string_content);
foreach ($exploded_content as $content) {
$removed_trs[] = trim(str_replace(self::SEARCH_CONTENT, '', $content));
}
return $removed_trs;
}
protected function curlSite()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, self::NCDC_WEBSITE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
protected function tableCaseFocus()
{
$html = $this->curlSite();
$start_point = strpos($html, '<table id="custom1">');
$end_point = strpos($html, '</table>', $start_point);
$length = $end_point-$start_point;
$html = substr($html, $start_point, $length);
return $this->removeTrailingCharacters($html);
}
}