]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Cocktail/db.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Cocktail / db.php
1 <?php
2
3 if (!defined('__DIR__')) {
4     define('__DIR__', dirname(__FILE__));
5 }
6
7 // Create database schema
8 echo 'Creating database', PHP_EOL;
9 $file = __DIR__ . '/cocktail.db';
10 if (file_exists($file)) {
11     unlink($file);
12 }
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)');
17
18 // Get raw webtender.com data set
19 echo 'Downloading webtender.com data set', PHP_EOL;
20 $start = 1;
21 do {
22     $file = __DIR__ . '/' . $start . '.html';
23     if (file_exists($file)) {
24         continue;
25     }
26     copy(
27         'http://www.webtender.com/db/browse?level=2&dir=drinks&char=%2A&start=' . $start,
28         $file
29     );
30     if (!isset($limit)) {
31         $contents = file_get_contents($file);
32         preg_match('/([0-9]+) found/', $contents, $match);
33         $limit = $match[1] + (150 - ($match[1] % 150));
34     }
35     echo 'Got records ', $start, ' - ', min($start + 150, $limit), ' of ', $limit, PHP_EOL;
36     $start += 150;
37 } while ($start < $limit);
38
39 // Extract data from data set
40 $start = 1;
41 while ($start < $limit) {
42     echo 'Processing ', $start, ' - ', min($start + 150, $limit), ' of ', $limit, PHP_EOL;
43
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);
52
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));
61     }
62     $db->commit();
63
64     $start += 150;
65 }
66
67 // Clean up
68 echo 'Cleaning up', PHP_EOL;
69 $start = 1;
70 while ($start < $limit) {
71     $file = __DIR__ . '/' . $start . '.html';
72     unlink($file);
73     $start += 150;
74 }