debug option for crawler
authorChristian Weiske <cweiske@cweiske.de>
Thu, 11 Feb 2016 07:43:01 +0000 (08:43 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 11 Feb 2016 07:43:01 +0000 (08:43 +0100)
README.rst
bin/crawl.php
src/phinde/Crawler.php

index 40f8c55f245650500c79c3f8a825e27a9ce0d2d5..3d10657a381b155ae8d76808a5ebad7ce8de1ca2 100644 (file)
@@ -32,6 +32,7 @@ Dependencies
 - PHP 5.5+
 - elasticsearch 2.0
 - gearman
+- Console_CommandLine
 - Net_URL2
 
 
index e9a6218cac2e5ba4b6e9489a7befbcf5926b5ea3..0d57bb3232f8a5490de75a81a00b6b73ccad1708 100755 (executable)
@@ -3,12 +3,33 @@
 namespace phinde;
 require_once __DIR__ . '/../src/init.php';
 
-if ($argc < 2) {
-    echo "No URL given\n";
-    exit(1);
+$cc = new \Console_CommandLine();
+$cc->description = 'phinde URL crawler';
+$cc->version = '0.0.1';
+$cc->addOption(
+    'showLinksOnly',
+    array(
+        'short_name'  => '-s',
+        'long_name'   => '--show-links',
+        'description' => 'Only show which URLs were found',
+        'action'      => 'StoreTrue',
+        'default'     => false
+    )
+);
+$cc->addArgument(
+    'url',
+    array(
+        'description' => 'URL to crawl',
+        'multiple'    => false
+    )
+);
+try {
+    $res = $cc->parse();
+} catch (\Exception $e) {
+    $cc->displayError($e->getMessage());
 }
 
-$url = $argv[1];
+$url = $res->args['url'];
 $url = Helper::addSchema($url);
 if (!Helper::isUrlAllowed($url)) {
     echo "Domain is not allowed; not crawling\n";
@@ -17,6 +38,7 @@ if (!Helper::isUrlAllowed($url)) {
 
 try {
     $crawler = new Crawler();
+    $crawler->setShowLinksOnly($res->options['showLinksOnly']);
     $crawler->crawl($url);
 } catch (\Exception $e) {
     echo $e->getMessage() . "\n";
index f3158aa07223979239f41bca742979d159f1aa2c..9b148789437bac97dbdf897048487e48b2ca0121 100644 (file)
@@ -6,6 +6,11 @@ class Crawler
     protected $es;
     protected $queue;
 
+    /**
+     * If the links only should be shown, not queued
+     */
+    protected $showLinksOnly = false;
+
     static $supportedIndexTypes = array(
         'application/atom+xml'  => '\\phinde\\LinkExtractor\\Atom',
         'application/xhtml+xml' => '\\phinde\\LinkExtractor\\Html',
@@ -22,7 +27,11 @@ class Crawler
     {
         $res       = $this->fetch($url);
         $linkInfos = $this->extractLinks($res);
-        $this->enqueue($linkInfos);
+        if ($this->showLinksOnly) {
+            $this->showLinks($linkInfos);
+        } else {
+            $this->enqueue($linkInfos);
+        }
     }
 
     protected function fetch($url)
@@ -70,5 +79,21 @@ class Crawler
             }
         }
     }
+
+    protected function showLinks($linkInfos)
+    {
+        foreach ($linkInfos as $linkInfo) {
+            echo $linkInfo->url . "\n";
+            if ($linkInfo->title) {
+                echo '  title: ' . $linkInfo->title . "\n";
+                echo '  source: ' . $linkInfo->source . "\n";
+            }
+        }
+    }
+
+    public function setShowLinksOnly($showLinksOnly)
+    {
+        $this->showLinksOnly = $showLinksOnly;
+    }
 }
 ?>