Skip to content

Commit

Permalink
add scope and improve output
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3n committed Jul 20, 2019
1 parent 4a37369 commit a055b13
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"require": {
"php": "^7.2",
"ext-json": "*",
"symfony/http-client": "^4.3",
"symfony/dom-crawler": "^4.3"
},
Expand Down
19 changes: 15 additions & 4 deletions nusantara
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@ $output->writeln([
'=================================================================',
]);

file_put_contents(__DIR__.'/wilayah.json', json_encode($nusantara->fetch($output)));
$start = microtime(true);
file_put_contents(__DIR__.'/wilayah.json', json_encode($nusantara->fetch(Nusantara::SCOPE_DESA, $output)));
$end = microtime(true);
$second = $end - $start;
$minute = round($second / 60);
$hour = 0;
if ($minute > 0) {
$second = (int) $second - ($minute * 60);
$hour = round($minute / 60);
if ($hour > 0) {
$minute = (int) $minute - ($hour * 60);
}
}

$output->writeln('Menyimpan data.');

$output->writeln('<info>Pemrosesan telah selesai.</info>');
$output->writeln('<comment>Menyimpan data.</comment>');
$output->writeln(sprintf('<comment>Pemrosesan telah selesai dalam "<info>%s</info>" jam "<info>%s</info>" menit "<info>%s</info>" detik</comment>', $hour, $minute, number_format($second, 2)));
77 changes: 48 additions & 29 deletions src/Nusantara.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
*/
class Nusantara
{
public const SCOPE_PROPINSI = 'prop';
public const SCOPE_KABUPATEN = 'kab';
public const SCOPE_KECAMATAN = 'kec';
public const SCOPE_DESA = 'desa';

public const NUSANTARA_URL = 'http://mfdonline.bps.go.id/index.php?link=hasil_pencarian';

public function fetch(OutputInterface $output = null): array
public function fetch(string $scope = self::SCOPE_DESA, OutputInterface $output = null): array
{
$searchKeys = ['a', 'i', 'u', 'e', 'o'];
$client = new CurlHttpClient();
$results = [];

if ($output) {
$output->writeln(sprintf('<comment>Batas pencarian adalah sampai level "<info>%s</info>"</comment>', ucfirst($scope)));
}

foreach ($searchKeys as $searchKey) {
try {
if ($output) {
Expand All @@ -32,7 +41,7 @@ public function fetch(OutputInterface $output = null): array

$response = $client->request('POST', self::NUSANTARA_URL, [
'body' => [
'pilihcari' => 'desa',
'pilihcari' => $scope,
'kata_kunci' => $searchKey,
]
]);
Expand All @@ -44,12 +53,13 @@ public function fetch(OutputInterface $output = null): array
$crawler = new Crawler($response->getContent());
$trs = $crawler->filterXPath('//tr[@class="table_content"]');

$progress = null;
if ($output) {
$output->writeln('<info>Memproses data.</info>');
$progress = new ProgressBar($output, $trs->count());
}

$trs->each(function (Crawler $tr) use (&$results, $output, $progress) {
$trs->each(function (Crawler $tr) use (&$results, $scope, $output, $progress) {
if ($output) {
$progress->advance();
}
Expand All @@ -58,39 +68,48 @@ public function fetch(OutputInterface $output = null): array

$provinceCode = trim($tds->eq(1)->text());
$provinceName = trim($tds->eq(2)->text());

$districtCode = sprintf('%s%s', $provinceCode, trim($tds->eq(3)->text()));
$districtName = trim($tds->eq(4)->text());

$subDistrictCode = sprintf('%s%s', $districtCode, trim($tds->eq(5)->text()));
$subDistrictName = trim($tds->eq(6)->text());

$villageCode = sprintf('%s%s', $subDistrictCode, trim($tds->eq(7)->text()));
$villageName = trim($tds->eq(8)->text());

if (!array_key_exists($provinceCode, $results)) {
$results[$provinceCode] = [
'name' => $provinceName,
'district' => [],
];
}

if (!array_key_exists($districtCode, $results[$provinceCode]['district'])) {
$results[$provinceCode]['district'][$districtCode] = [
'name' => $districtName,
'sub_district' => [],
];
}

if (!array_key_exists($subDistrictCode, $results[$provinceCode]['district'][$districtCode]['sub_district'])) {
$results[$provinceCode]['district'][$districtCode]['sub_district'][$subDistrictCode] = [
'name' => $subDistrictName,
'village' => [],
];
}

if (!array_key_exists($villageCode, $results[$provinceCode]['district'][$districtCode]['sub_district'][$subDistrictCode]['village'])) {
$results[$provinceCode]['district'][$districtCode]['sub_district'][$subDistrictCode]['village'][$villageCode] = $villageName;
if ($scope !== self::SCOPE_PROPINSI) {
$districtCode = sprintf('%s%s', $provinceCode, trim($tds->eq(3)->text()));
$districtName = trim($tds->eq(4)->text());
if (!array_key_exists($districtCode, $results[$provinceCode]['district'])) {
$results[$provinceCode]['district'][$districtCode] = [
'name' => $districtName,
'sub_district' => [],
];
}

if ($scope === self::SCOPE_KECAMATAN) {
$subDistrictCode = sprintf('%s%s', $districtCode, trim($tds->eq(5)->text()));
$subDistrictName = trim($tds->eq(6)->text());
if (!array_key_exists($subDistrictCode, $results[$provinceCode]['district'][$districtCode]['sub_district'])) {
$results[$provinceCode]['district'][$districtCode]['sub_district'][$subDistrictCode] = [
'name' => $subDistrictName,
'village' => [],
];
}
} else if ($scope === self::SCOPE_DESA) {
$subDistrictCode = sprintf('%s%s', $districtCode, trim($tds->eq(5)->text()));
$subDistrictName = trim($tds->eq(6)->text());
if (!array_key_exists($subDistrictCode, $results[$provinceCode]['district'][$districtCode]['sub_district'])) {
$results[$provinceCode]['district'][$districtCode]['sub_district'][$subDistrictCode] = [
'name' => $subDistrictName,
'village' => [],
];
}

$villageCode = sprintf('%s%s', $subDistrictCode, trim($tds->eq(7)->text()));
$villageName = trim($tds->eq(8)->text());
if (!array_key_exists($villageCode, $results[$provinceCode]['district'][$districtCode]['sub_district'][$subDistrictCode]['village'])) {
$results[$provinceCode]['district'][$districtCode]['sub_district'][$subDistrictCode]['village'][$villageCode] = $villageName;
}
}
}
});

Expand Down

0 comments on commit a055b13

Please sign in to comment.