<?php
# related article: http://newtoypia.blogspot.tw/2014/08/osm-mashup.html

# wget -O /tmp/capitals.htm https://www.tripmondo.com/magazine/facts-and-statistics/list-of-capitals-and-countries/
# php capitals.txt /tmp/capitals.htm > capitals.json
require_once 'QueryPath/QueryPath.php';
header('Content-Type: application/json');
$home = getenv("HOME");
$qp = htmlqp($argv[1]);
$out = [];
foreach ($qp->find("tbody tr") as $item) {
    $flag = $item->find("td img")->attr("src");
    $country = $item->parent()->next()->text();
    $capital = $item->next()->text();
    $lat = preg_replace('/,/', '.', $item->next("td:nth-of-type(4)")->text());
    $lon = preg_replace('/,/', '.', $item->next()->text());
    array_push($out, array(
	'country' => $country,
	'capital' => $capital,
	'flag' => $flag,
	'lat' => $lat,
	'lon' => $lon
    ));
}
echo json_encode($out, JSON_PRETTY_PRINT);
?>
