3 if (!defined('__DIR__')) {
4 define('__DIR__', dirname(__FILE__));
7 // Create database schema
8 echo 'Creating database', PHP_EOL;
9 $file = __DIR__ . '/cocktail.db';
10 if (file_exists($file)) {
13 $db = new PDO('sqlite:' . $file);
14 $db->exec('CREATE TABLE cocktail (name VARCHAR(255), link VARCHAR(255))');
15 $db->exec('CREATE UNIQUE INDEX cocktail_name ON cocktail (name)');
16 $insert = $db->prepare('INSERT INTO cocktail (name, link) VALUES (:name, :link)');
18 // Get raw webtender.com data set
19 echo 'Downloading webtender.com data set', PHP_EOL;
22 $file = __DIR__ . '/' . $start . '.html';
23 if (file_exists($file)) {
27 'http://www.webtender.com/db/browse?level=2&dir=drinks&char=%2A&start=' . $start,
31 $contents = file_get_contents($file);
32 preg_match('/([0-9]+) found/', $contents, $match);
33 $limit = $match[1] + (150 - ($match[1] % 150));
35 echo 'Got records ', $start, ' - ', min($start + 150, $limit), ' of ', $limit, PHP_EOL;
37 } while ($start < $limit);
39 // Extract data from data set
41 while ($start < $limit) {
42 echo 'Processing ', $start, ' - ', min($start + 150, $limit), ' of ', $limit, PHP_EOL;
44 $file = __DIR__ . '/' . $start . '.html';
45 $contents = file_get_contents($file);
46 $contents = tidy_repair_string($contents);
47 libxml_use_internal_errors(true);
48 $doc = new DOMDocument;
49 $doc->loadHTML($contents);
50 libxml_clear_errors();
51 $xpath = new DOMXPath($doc);
53 $cocktails = $xpath->query('//li/a');
54 $db->beginTransaction();
55 foreach ($cocktails as $cocktail) {
56 $name = $cocktail->nodeValue;
57 $name = preg_replace('/ The$|^The |\s*\([^)]+\)\s*| #[0-9]+$/', '', $name);
58 $name = html_entity_decode($name);
59 $link = 'http://www.webtender.com' . $cocktail->getAttribute('href');
60 $insert->execute(array($name, $link));
68 echo 'Cleaning up', PHP_EOL;
70 while ($start < $limit) {
71 $file = __DIR__ . '/' . $start . '.html';