[
'url' => $_gitlink.'time_series_19-covid-Confirmed.csv',
'path' => 'data/covid_confirmed.csv'
],
'deaths' => [
'url' => $_gitlink.'time_series_19-covid-Deaths.csv',
'path' => 'data/covid_deaths.csv'
],
'recovered' => [
'url' => $_gitlink.'time_series_19-covid-Recovered.csv',
'path' => 'data/covid_recovered.csv'
]
];
$_start_index = 40;
if($_start_index < 4) { $_start_index = 4; }
$sync_output = sync_data();
$data = [];
foreach($_files as $k => $v) {
$ts = file_get_contents($v['path']);
$ts = condition_csv($ts);
$data[$k] = parse_ts_csv($ts);
}
//print_r($data,1);
//$data['current'] = $data['confirmed'];
$data['current'] = get_current_factors($data);
function get_current_factors($data) {
//Get numbers of people that were infected on each day.
foreach($data['confirmed']['series'] as $country => $states) {
foreach($states as $state => $d) {
foreach($states[$state]['data'] as $k => $v) {
$infected = (int) $data['confirmed']['series'][$country][$state]['data'][$k];
$cured = (int) $data['recovered']['series'][$country][$state]['data'][$k];
$dead = (int) $data['deaths']['series'][$country][$state]['data'][$k];
$data['current']['series'][$country][$state]['data'][$k] = ($infected-($cured));
}
}
}
return $data['current'];
}
?>
COVID-19
'.$sync_output.'';
//echo '
'.print_r($data, 1).'
';
//echo '
'.json_encode($data, JSON_PRETTY_PRINT).'
';
//echo '
'.$ts.'
';
?>
[], 'series' => [] ]) {
global $_start_index;
$lines = explode("\n", $s);
$hdr = explode(",",$lines[0]);
foreach($hdr as $hk => $hv) {
if($hk > $_start_index) {
$return['labels'][] = $hv;
}
}
foreach($lines as $row => $csv) {
if($row > 0) {
$l = explode(",",$csv);
if(!isset($return['series'][$l[1]])) { $return['series'][$l[1]] = []; }
if(strlen($l[0]) < 1) {
$key = 'country';
} else {
$key = $l[0];
}
$return['series'][$l[1]][$key] = ['coords'=>[$l[2],$l[3]], 'data'=>[]];
foreach($l as $k => $v) {
if($k > $_start_index) {
$return['series'][$l[1]][$key]['data'][] = $v;
}
}
}
}
return $return;
}
function condition_csv($r) {
return str_replace(['"', ', '], ['', '|'], $r);
}
function sync_data($return = NULL) {
global $_files;
$hours_til_sync = 6;
foreach($_files as $k => $v) {
$sync = false;
if(file_exists($v['path'])) {
$return .= $v['path'].' exists! Checking last update time.'.PHP_EOL;
//time interval for deletion to occur...
$x = (3600*$hours_til_sync); //6 hours
//timestamp
$current_time = time();
//timestamp
$file_creation_time = filemtime($v['path']);
//extract difference
$difference = $current_time - $file_creation_time;
//if difference = $x...then delete file
if ($difference >= $x) {
$return .= $v['path'].' needs to be updated!'.PHP_EOL;
unlink($v['path']);
$sync = true;
} else {
$return .= $v['path'].' is still fresh. Skipping update!'.PHP_EOL;
}
} else {
$sync = true;
$return .= $v['path'].' does not exist. Downloading file!'.PHP_EOL;
}
if($sync) {
$csv = file_get_contents($v['url']);
if (!file_exists($v['path'])) { touch($v['path']); }
file_put_contents($v['path'], $csv);
$return .= $v['path'].' has been updated!'.PHP_EOL;
}
}
return $return;
}
?>