Skip to content

Instantly share code, notes, and snippets.

@RomanStone
Last active May 11, 2020 00:33
Show Gist options
  • Save RomanStone/32148c37ab66e4f15ef603cd0d9b92e8 to your computer and use it in GitHub Desktop.
Save RomanStone/32148c37ab66e4f15ef603cd0d9b92e8 to your computer and use it in GitHub Desktop.
<?php
define('TIME_NOW', time());
define('DS', DIRECTORY_SEPARATOR);
define('BASE_PATH', strtr(__DIR__, '\\', '/'));
clearstatcache();
$pathimpl = function() {
return BASE_PATH . '/' . ltrim(strtr(implode(DS, func_get_args()), '\\', '/'), '/');
};
$populationfile = $pathimpl('population.json');
$cachefile = $pathimpl('.cache');
$is_cache = is_file($cachefile);
$cachesize = filesize($cachefile);
$flag = 0;
if (!$is_cache || !$cachesize) {
touch($cachefile);
clearstatcache();
$flag = 1;
}
$cache_filemtime = filemtime($cachefile);
$cachetime = intval(TIME_NOW - $cache_filemtime);
if (!$flag && $cachetime > 3600) {
$flag = 1;
}
if ($flag > 0) {
$json = file_get_contents("https://api.apify.com/v2/key-value-stores/tVaYRsPHLjNdNBu7S/records/LATEST?disableRedirect=true", 0, stream_context_create(
array(
'http' => array(
'method' => 'GET',
'header' => array(),
'user_agent' => 'PHP/7.3.12',
'content' => '',
'proxy' => '',
'request_fulluri' => FALSE,
'follow_location' => 1,
'max_redirects' => 2,
'protocol_version' => '1.0',
'timeout' => 5,
'ignore_errors' => TRUE
)
)
)
);
if (!$json) { echo "Err[0x01]: [JSON]: NO_DATA\n"; exit(1); }
$json = json_decode($json, true);
if (!$json) { echo "Err[0x02]: [JSON]: DECODE_ERROR\n"; exit(1); }
file_put_contents($cachefile, json_encode($json));
} else {
$json = file_get_contents($cachefile);
if (!$json) { echo "Err[0x03]: [JSON]: DECODE_ERROR\n"; exit(1); }
$json = json_decode($json, true);
if (!$json) { echo "Err[0x04]: [JSON]: DECODE_ERROR\n"; exit(1); }
}
$pjson = json_decode(file_get_contents($populationfile), true);
$array = array();
foreach($pjson as $_data) {
$country = strtolower($_data['country']);
$array[$country]['population'] = intval($_data['population']);
}
$pjson = $array;
$array = array();
foreach($json as $_data) {
$country = strtolower($_data['country']);
$array[$country] = array();
foreach($_data as $k => $v) {
$array[$country][$k] = $v;
}
}
$json = $array;
$array = array();
foreach($json as $country => $data) {
if (isset($pjson[$country])) {
$json[$country]['population'] = $pjson[$country]['population'];
$array[$country] = $json[$country];
}
}
$data = $array;
$json = array();
$array = array();
?><!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body style="width:100%;float:left">
<div style="width:100%;max-width:1200px;float:none;margin: 0 auto;">
<?php
echo "<table style=\"width:100%\">\n";
echo "<caption></caption>\n";
echo "<thead>\n";
echo "<tr>\n";
echo "<th style=\"text-align: left;\">Страна / Country</th>\n";
echo "<th style=\"text-align: left;\">Заражений / Infected</th>\n";
echo "<th style=\"text-align: left;\">Население / Population</th>\n";
echo "<th style=\"text-align: left;\">Процент / Percentage</th>\n";
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
foreach($data as $country => $_data) {
$population =& $_data['population'];
$infected =& $_data['infected'];
$percentage = round(($infected / $population) * 100, 4);
echo "<tr>\n";
echo "<td>{$country}</td>\n";
echo "<td>{$infected}</td>\n";
echo "<td>{$population}</td>\n";
echo "<td>{$percentage}</td>\n";
echo "</tr>\n";
}
echo "</tbody>\n";
echo "<tfoot>\n";
echo "<tr>\n";
echo "<td></td>\n";
echo "</tr>\n";
echo "</tfoot>\n";
echo "</table>\n";
?>
</div>
</body>
</html>
[
{
"country": "Afghanistan",
"population": "35530081"
},
{
"country": "Albania",
"population": "2930187"
},
{
"country": "Algeria",
"population": "41318142"
},
{
"country": "American Samoa",
"population": "55641"
},
{
"country": "Andorra",
"population": "76965"
},
{
"country": "Angola",
"population": "29784193"
},
{
"country": "Anguilla",
"population": "14909"
},
{
"country": "Antarctica",
"population": null
},
{
"country": "Antigua and Barbuda",
"population": "102012"
},
{
"country": "Argentina",
"population": "44271041"
},
{
"country": "Armenia",
"population": "2930450"
},
{
"country": "Aruba",
"population": "105264"
},
{
"country": "Australia",
"population": "24450561"
},
{
"country": "Austria",
"population": "8735453"
},
{
"country": "Azerbaijan",
"population": "9827589"
},
{
"country": "Bahamas",
"population": "395361"
},
{
"country": "Bahrain",
"population": "1492584"
},
{
"country": "Bangladesh",
"population": "164669751"
},
{
"country": "Barbados",
"population": "285719"
},
{
"country": "Belarus",
"population": "9468338"
},
{
"country": "Belgium",
"population": "11429336"
},
{
"country": "Belize",
"population": "374681"
},
{
"country": "Benin",
"population": "11175692"
},
{
"country": "Bermuda",
"population": "61349"
},
{
"country": "Bhutan",
"population": "807610"
},
{
"country": "Bolivia",
"population": "11051600"
},
{
"country": "Bosnia and Herzegovina",
"population": "3507017"
},
{
"country": "Botswana",
"population": "2291661"
},
{
"country": "Bouvet Island",
"population": null
},
{
"country": "Brazil",
"population": "209288278"
},
{
"country": "British Indian Ocean Territory",
"population": null
},
{
"country": "Brunei",
"population": "428697"
},
{
"country": "Bulgaria",
"population": "7084571"
},
{
"country": "Burkina Faso",
"population": "19193382"
},
{
"country": "Burundi",
"population": "10864245"
},
{
"country": "Cambodia",
"population": "16005373"
},
{
"country": "Cameroon",
"population": "24053727"
},
{
"country": "Canada",
"population": "36624199"
},
{
"country": "Cape Verde",
"population": "546388"
},
{
"country": "Cayman Islands",
"population": "61559"
},
{
"country": "Central African Republic",
"population": "4659080"
},
{
"country": "Chad",
"population": "14899994"
},
{
"country": "Chile",
"population": "18054726"
},
{
"country": "China",
"population": "1409517397"
},
{
"country": "Christmas Island",
"population": "2500"
},
{
"country": "Cocos (Keeling) Islands",
"population": "600"
},
{
"country": "Colombia",
"population": "49065615"
},
{
"country": "Comoros",
"population": "813912"
},
{
"country": "Congo",
"population": "5260750"
},
{
"country": "Cook Islands",
"population": "17380"
},
{
"country": "Costa Rica",
"population": "4905769"
},
{
"country": "Croatia",
"population": "4189353"
},
{
"country": "Cuba",
"population": "11484636"
},
{
"country": "Cyprus",
"population": "1179551"
},
{
"country": "Czech Republic",
"population": "10618303"
},
{
"country": "Denmark",
"population": "5733551"
},
{
"country": "Djibouti",
"population": "956985"
},
{
"country": "Dominica",
"population": "73925"
},
{
"country": "Dominican Republic",
"population": "10766998"
},
{
"country": "East Timor",
"population": "1296311"
},
{
"country": "Ecuador",
"population": "16624858"
},
{
"country": "Egypt",
"population": "97553151"
},
{
"country": "El Salvador",
"population": "6377853"
},
{
"country": "England",
"population": null
},
{
"country": "Equatorial Guinea",
"population": "1267689"
},
{
"country": "Eritrea",
"population": "5068831"
},
{
"country": "Estonia",
"population": "1309632"
},
{
"country": "Ethiopia",
"population": "104957438"
},
{
"country": "Falkland Islands",
"population": "2910"
},
{
"country": "Faroe Islands",
"population": "49290"
},
{
"country": "Fiji Islands",
"population": "905502"
},
{
"country": "Finland",
"population": "5523231"
},
{
"country": "France",
"population": "64979548"
},
{
"country": "French Guiana",
"population": "282731"
},
{
"country": "French Polynesia",
"population": "283007"
},
{
"country": "French Southern territories",
"population": null
},
{
"country": "Gabon",
"population": "2025137"
},
{
"country": "Gambia",
"population": "2100568"
},
{
"country": "Georgia",
"population": "3912061"
},
{
"country": "Germany",
"population": "82114224"
},
{
"country": "Ghana",
"population": "28833629"
},
{
"country": "Gibraltar",
"population": "34571"
},
{
"country": "Greece",
"population": "11159773"
},
{
"country": "Greenland",
"population": "56480"
},
{
"country": "Grenada",
"population": "107825"
},
{
"country": "Guadeloupe",
"population": "449568"
},
{
"country": "Guam",
"population": "164229"
},
{
"country": "Guatemala",
"population": "16913503"
},
{
"country": "Guinea",
"population": "12717176"
},
{
"country": "Guinea-Bissau",
"population": "1861283"
},
{
"country": "Guyana",
"population": "777859"
},
{
"country": "Haiti",
"population": "10981229"
},
{
"country": "Heard Island and McDonald Islands",
"population": null
},
{
"country": "Holy See (Vatican City State)",
"population": "1000"
},
{
"country": "Honduras",
"population": "9265067"
},
{
"country": "Hong Kong",
"population": "7364883"
},
{
"country": "Hungary",
"population": "9721559"
},
{
"country": "Iceland",
"population": "335025"
},
{
"country": "India",
"population": "1339180127"
},
{
"country": "Indonesia",
"population": "263991379"
},
{
"country": "Iran",
"population": "81162788"
},
{
"country": "Iraq",
"population": "38274618"
},
{
"country": "Ireland",
"population": "4761657"
},
{
"country": "Israel",
"population": "8321570"
},
{
"country": "Italy",
"population": "59359900"
},
{
"country": "Ivory Coast",
"population": "24294750"
},
{
"country": "Jamaica",
"population": "2890299"
},
{
"country": "Japan",
"population": "127484450"
},
{
"country": "Jordan",
"population": "9702353"
},
{
"country": "Kazakhstan",
"population": "18204499"
},
{
"country": "Kenya",
"population": "49699862"
},
{
"country": "Kiribati",
"population": "116398"
},
{
"country": "Kuwait",
"population": "4136528"
},
{
"country": "Kyrgyzstan",
"population": "6045117"
},
{
"country": "Laos",
"population": "6858160"
},
{
"country": "Latvia",
"population": "1949670"
},
{
"country": "Lebanon",
"population": "6082357"
},
{
"country": "Lesotho",
"population": "2233339"
},
{
"country": "Liberia",
"population": "4731906"
},
{
"country": "Libyan Arab Jamahiriya",
"population": "5605000"
},
{
"country": "Liechtenstein",
"population": "37922"
},
{
"country": "Lithuania",
"population": "2890297"
},
{
"country": "Luxembourg",
"population": "583455"
},
{
"country": "Macao",
"population": "473000"
},
{
"country": "North Macedonia",
"population": "2024000"
},
{
"country": "Madagascar",
"population": "25570895"
},
{
"country": "Malawi",
"population": "18622104"
},
{
"country": "Malaysia",
"population": "31624264"
},
{
"country": "Maldives",
"population": "436330"
},
{
"country": "Mali",
"population": "18541980"
},
{
"country": "Malta",
"population": "430835"
},
{
"country": "Marshall Islands",
"population": "53127"
},
{
"country": "Martinique",
"population": "384896"
},
{
"country": "Mauritania",
"population": "4420184"
},
{
"country": "Mauritius",
"population": "1265138"
},
{
"country": "Mayotte",
"population": "253045"
},
{
"country": "Mexico",
"population": "129163276"
},
{
"country": "Micronesia, Federated States of",
"population": null
},
{
"country": "Moldova",
"population": "4051212"
},
{
"country": "Monaco",
"population": "38695"
},
{
"country": "Mongolia",
"population": "3075647"
},
{
"country": "Montenegro",
"population": "631219"
},
{
"country": "Montserrat",
"population": "5177"
},
{
"country": "Morocco",
"population": "35739580"
},
{
"country": "Mozambique",
"population": "29668834"
},
{
"country": "Myanmar",
"population": "53370609"
},
{
"country": "Namibia",
"population": "2533794"
},
{
"country": "Nauru",
"population": "11359"
},
{
"country": "Nepal",
"population": "29304998"
},
{
"country": "Netherlands",
"population": "17035938"
},
{
"country": "Netherlands Antilles",
"population": "217000"
},
{
"country": "New Caledonia",
"population": "276255"
},
{
"country": "New Zealand",
"population": "4705818"
},
{
"country": "Nicaragua",
"population": "6217581"
},
{
"country": "Niger",
"population": "21477348"
},
{
"country": "Nigeria",
"population": "190886311"
},
{
"country": "Niue",
"population": "1618"
},
{
"country": "Norfolk Island",
"population": "2000"
},
{
"country": "North Korea",
"population": "25490965"
},
{
"country": "Northern Ireland",
"population": null
},
{
"country": "Northern Mariana Islands",
"population": "55144"
},
{
"country": "Norway",
"population": "5305383"
},
{
"country": "Oman",
"population": "4636262"
},
{
"country": "Pakistan",
"population": "197015955"
},
{
"country": "Palau",
"population": "21729"
},
{
"country": "Palestine",
"population": "4920724"
},
{
"country": "Panama",
"population": "4098587"
},
{
"country": "Papua New Guinea",
"population": "8251162"
},
{
"country": "Paraguay",
"population": "6811297"
},
{
"country": "Peru",
"population": "32165485"
},
{
"country": "Philippines",
"population": "104918090"
},
{
"country": "Pitcairn",
"population": "50"
},
{
"country": "Poland",
"population": "38170712"
},
{
"country": "Portugal",
"population": "10329506"
},
{
"country": "Puerto Rico",
"population": "3663131"
},
{
"country": "Qatar",
"population": "2639211"
},
{
"country": "Reunion",
"population": "699000"
},
{
"country": "Romania",
"population": "19679306"
},
{
"country": "Russian Federation",
"population": "143989754"
},
{
"country": "Rwanda",
"population": "12208407"
},
{
"country": "Saint Helena",
"population": "4049"
},
{
"country": "Saint Kitts and Nevis",
"population": "55345"
},
{
"country": "Saint Lucia",
"population": "178844"
},
{
"country": "Saint Pierre and Miquelon",
"population": "6320"
},
{
"country": "Saint Vincent and the Grenadines",
"population": "109897"
},
{
"country": "Samoa",
"population": "196440"
},
{
"country": "San Marino",
"population": "33400"
},
{
"country": "Sao Tome and Principe",
"population": "204327"
},
{
"country": "Saudi Arabia",
"population": "32938213"
},
{
"country": "Scotland",
"population": null
},
{
"country": "Senegal",
"population": "15850567"
},
{
"country": "Seychelles",
"population": "94737"
},
{
"country": "Sierra Leone",
"population": "7557212"
},
{
"country": "Singapore",
"population": "5708844"
},
{
"country": "Slovakia",
"population": "5447662"
},
{
"country": "Slovenia",
"population": "2079976"
},
{
"country": "Solomon Islands",
"population": "611343"
},
{
"country": "Somalia",
"population": "14742523"
},
{
"country": "South Africa",
"population": "56717156"
},
{
"country": "South Georgia and the South Sandwich Islands",
"population": null
},
{
"country": "South Korea",
"population": "50982212"
},
{
"country": "South Sudan",
"population": "12575714"
},
{
"country": "Spain",
"population": "46354321"
},
{
"country": "SriLanka",
"population": "20876917"
},
{
"country": "Sudan",
"population": "40533330"
},
{
"country": "Suriname",
"population": "563402"
},
{
"country": "Svalbard and Jan Mayen",
"population": "3200"
},
{
"country": "Swaziland",
"population": "1008000"
},
{
"country": "Sweden",
"population": "9910701"
},
{
"country": "Switzerland",
"population": "8476005"
},
{
"country": "Syria",
"population": "18269868"
},
{
"country": "Tajikistan",
"population": "8921343"
},
{
"country": "Tanzania",
"population": "57310019"
},
{
"country": "Thailand",
"population": "69037513"
},
{
"country": "The Democratic Republic of Congo",
"population": null
},
{
"country": "Togo",
"population": "7797694"
},
{
"country": "Tokelau",
"population": "1300"
},
{
"country": "Tonga",
"population": "108020"
},
{
"country": "Trinidad and Tobago",
"population": "1369125"
},
{
"country": "Tunisia",
"population": "11532127"
},
{
"country": "Turkey",
"population": "80745020"
},
{
"country": "Turkmenistan",
"population": "5758075"
},
{
"country": "Turks and Caicos Islands",
"population": "35446"
},
{
"country": "Tuvalu",
"population": "11192"
},
{
"country": "Uganda",
"population": "42862958"
},
{
"country": "Ukraine",
"population": "44222947"
},
{
"country": "United Arab Emirates",
"population": "9400145"
},
{
"country": "United Kingdom",
"population": "66181585"
},
{
"country": "United States",
"population": "324459463"
},
{
"country": "United States Minor Outlying Islands",
"population": null
},
{
"country": "Uruguay",
"population": "3456750"
},
{
"country": "Uzbekistan",
"population": "31910641"
},
{
"country": "Vanuatu",
"population": "276244"
},
{
"country": "Venezuela",
"population": "31977065"
},
{
"country": "Vietnam",
"population": "95540800"
},
{
"country": "Virgin Islands, British",
"population": null
},
{
"country": "Virgin Islands, U.S.",
"population": null
},
{
"country": "Wales",
"population": null
},
{
"country": "Wallis and Futuna",
"population": "11773"
},
{
"country": "Western Sahara",
"population": "552628"
},
{
"country": "Yemen",
"population": "28250420"
},
{
"country": "Yugoslavia",
"population": "10640000"
},
{
"country": "Zambia",
"population": "17094130"
},
{
"country": "Zimbabwe",
"population": "16529904"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment