Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Last active August 10, 2017 16:25
Show Gist options
  • Save ammarfaizi2/1ec21f8722611a85be5010b2aa4d7b54 to your computer and use it in GitHub Desktop.
Save ammarfaizi2/1ec21f8722611a85be5010b2aa4d7b54 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Ammar Faizi <[email protected]>
*
* KBBI Grabber.
*/
define("PDO_CONNECT", "mysql:host=localhost;dbname=kamus_bindo;");
define("PDO_USER", "icetea");
define("PDO_PASS", "");
$pdo = new PDO(PDO_CONNECT, PDO_USER, PDO_PASS);
$pdo->exec("DROP TABLE IF EXISTS `kb_kamus`");
$pdo->exec("CREATE TABLE `kb_kamus` (`id` int(11) NOT NULL AUTO_INCREMENT,`kata` varchar(225) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;");
$st = $pdo->prepare("INSERT INTO `kb_kamus` (id, kata) VALUES (:id, :kata);");
$pg = 1;
do {
$a = curl("http://www.kbbi.co.id/daftar-kata?page=".$pg++);
$a = explode('<div class="row"><div class="col-md-2 col-sm-3 col-xs-4">', $a, 2);
if (isset($a[1])) {
$a = explode('</nav>', $a[1])[0];
$a = explode('<nav', $a)[0];
$a = explode("<li>", $a);
$ct = count($a);
for ($i=1; $i < $ct; $i++) {
$w = preg_match_all("#<a href=\"(.+)\">(.*)</a></li>#", $a[$i], $n);
print $n[2][0]." | ".($st->execute([
"id" => null,
"kata" => trim(strtolower(html_entity_decode($n[2][0], ENT_QUOTES, 'UTF-8')), "-")
])?"true":"false")."\n";
}
} else {
// Finished.
break;
}
} while (true);
// Simple curl.
function curl($url)
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true
]);
$out = curl_exec($ch);
curl_close($ch);
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment