setup: check json before dropping current index
[phinde.git] / bin / crawl.php
1 #!/usr/bin/env php
2 <?php
3 namespace phinde;
4 require_once __DIR__ . '/../src/init.php';
5
6 $cc = new \Console_CommandLine();
7 $cc->description = 'phinde URL crawler';
8 $cc->version = '0.0.1';
9 $cc->addOption(
10     'showLinksOnly',
11     array(
12         'short_name'  => '-s',
13         'long_name'   => '--show-links',
14         'description' => 'Only show which URLs were found',
15         'action'      => 'StoreTrue',
16         'default'     => false
17     )
18 );
19 $cc->addArgument(
20     'url',
21     array(
22         'description' => 'URL to crawl',
23         'multiple'    => false
24     )
25 );
26 try {
27     $res = $cc->parse();
28 } catch (\Exception $e) {
29     $cc->displayError($e->getMessage());
30 }
31
32 $url = $res->args['url'];
33 $url = Helper::addSchema($url);
34 if (!Helper::isUrlAllowed($url)) {
35     echo "Domain is not allowed; not crawling\n";
36     exit(2);
37 }
38
39 try {
40     $crawler = new Crawler();
41     $crawler->setShowLinksOnly($res->options['showLinksOnly']);
42     $crawler->crawl($url);
43 } catch (\Exception $e) {
44     echo $e->getMessage() . "\n";
45     exit(10);
46 }
47 ?>