Class: CobwebCrawler

Inherits:
Object
  • Object
show all
Defined in:
lib/cobweb_crawler.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CobwebCrawler

Returns a new instance of CobwebCrawler.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cobweb_crawler.rb', line 7

def initialize(options={})
  @options = options
  
  @statistic = {}
  
  @options[:redis_options] = {:host => "127.0.0.1"} unless @options.has_key? :redis_options
  crawl_id = Digest::MD5.hexdigest(DateTime.now.inspect.to_s)

  @redis = NamespacedRedis.new(Redis.new(@options[:redis_options]), "cobweb-#{crawl_id}")
  
  @cobweb = Cobweb.new(@options)
end

Instance Method Details

#crawl(base_url, crawl_options = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cobweb_crawler.rb', line 20

def crawl(base_url, crawl_options = {}, &block)
  @options[:base_url] = base_url unless @options.has_key? :base_url
  
  @crawl_options = crawl_options
  
  @absolutize = Absolutize.new(@options[:base_url], :output_debug => false, :raise_exceptions => false, :force_escaping => false, :remove_anchors => true)
  
  @redis.sadd "queued", base_url
  crawl_counter = @redis.scard("crawled").to_i
  queue_counter = @redis.scard("queued").to_i

  while queue_counter>0 && (@options[:crawl_limit].to_i == 0 || @options[:crawl_limit].to_i > crawl_counter)      
    thread = Thread.new do

      url = @redis.spop "queued"
      crawl_counter = @redis.scard("crawled").to_i
      queue_counter = @redis.scard("queued").to_i
      
      @options[:url] = url
      unless @redis.sismember("crawled", url.to_s)
        begin
          Stats.update_status("Requesting #{url}...")
          content = @cobweb.get(url)
          Stats.update_status("Processing #{url}...")

          if @statistic[:average_response_time].nil?
            @statistic[:average_response_time] = content[:response_time].to_f
          else
            @statistic[:average_response_time] = (((@statistic[:average_response_time] * crawl_counter) + content[:response_time].to_f) / (crawl_counter + 1))
          end
    
          @statistic[:maximum_response_time] = content[:response_time] if @statistic[:maximum_response_time].nil? || @statistic[:maximum_response_time] < content[:response_time]
          @statistic[:minimum_response_time] = content[:response_time] if @statistic[:minimum_response_time].nil? || @statistic[:minimum_response_time] > content[:response_time]
    
          if @statistic[:average_length]
            @statistic[:average_length] = (((@statistic[:average_length].to_i*crawl_counter) + content[:length].to_i) / (crawl_counter + 1))
          else
            @statistic[:average_length] = content[:length].to_i
          end
    
          @statistic[:maximum_length] = content[:length].to_i if @statistic[:maximum_length].nil? || content[:length].to_i > @statistic[:maximum_length].to_i
          @statistic[:minimum_length] = content[:length].to_i if @statistic[:minimum_length].nil? || content[:length].to_i < @statistic[:minimum_length].to_i
          @statistic[:total_length] = @statistic[:total_length].to_i + content[:length].to_i

          if content[:mime_type].include?("text/html") or content[:mime_type].include?("application/xhtml+xml")
            @statistic[:page_count] = @statistic[:page_count].to_i + 1
            @statistic[:page_size] = @statistic[:page_size].to_i + content[:length].to_i
          else
            @statistic[:asset_count] = @statistic[:asset_count].to_i + 1
            @statistic[:asset_size] = @statistic[:asset_size].to_i + content[:length].to_i
          end
          
          @statistic[:total_redirects] = 0 if @statistic[:total_redirects].nil?
          @statistic[:total_redirects] += content[:redirect_through].count unless content[:redirect_through].nil?
          
          @statistic[:crawl_counter] = crawl_counter
          @statistic[:queue_counter] = queue_counter
          
          mime_counts = {}
          if @statistic.has_key? :mime_counts
            mime_counts = @statistic[:mime_counts]
            if mime_counts.has_key? content[:mime_type]
              mime_counts[content[:mime_type]] += 1
            else
              mime_counts[content[:mime_type]] = 1
            end
          else
            mime_counts = {content[:mime_type] => 1}
          end
          @statistic[:mime_counts] = mime_counts

          status_counts = {}
        
          if @statistic.has_key? :status_counts
            status_counts = @statistic[:status_counts]
            if status_counts.has_key? content[:status_code].to_i
              status_counts[content[:status_code].to_i] += 1
            else
              status_counts[content[:status_code].to_i] = 1
            end
          else
            status_counts = {content[:status_code].to_i => 1}
          end
          @statistic[:status_counts] = status_counts

          @redis.sadd "crawled", url.to_s
          @redis.incr "crawl-counter" 
          
          content[:links].keys.map{|key| content[:links][key]}.flatten.each do |content_link|
            link = content_link.to_s
            unless @redis.sismember("crawled", link)
              puts "Checking if #{link} matches #{@options[:base_url]} as internal?" if @options[:debug]
              if link.to_s.match(Regexp.new("^#{@options[:base_url]}"))
                puts "Matched as #{link} as internal" if @options[:debug]
                unless @redis.sismember("crawled", link) || @redis.sismember("queued", link)
                  puts "Added #{link.to_s} to queue" if @options[:debug]
                  @redis.sadd "queued", link
                  crawl_counter = @redis.scard("crawled").to_i
                  queue_counter = @redis.scard("queued").to_i
                end
              end
            end
          end
          
          crawl_counter = @redis.scard("crawled").to_i
          queue_counter = @redis.scard("queued").to_i
          Stats.update_statistics(@statistic)
          Stats.update_status("Completed #{url}.")
          puts "Crawled: #{crawl_counter.to_i} Limit: #{@options[:crawl_limit].to_i} Queued: #{queue_counter.to_i}" if @options[:debug] 
     
          yield content, @statistic if block_given?

        rescue => e
          puts "!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!"
          ap e
          ap e.backtrace
        end
      else
        puts "Already crawled #{@options[:url]}" if @options[:debug]
      end
    end
    thread.join
  end
  @statistic
end