Method: CobwebModule::Crawl#process_links

Defined in:
lib/crawl.rb


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
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/crawl.rb', line 110

def process_links &block

  # set the base url if this is the first page
  set_base_url @redis

  @cobweb_links = CobwebLinks.new(@options)
  if within_queue_limits?
    document_links = ContentLinkParser.new(@options[:url], content.body, @options).all_links(:valid_schemes => [:http, :https])
    #get rid of duplicate links in the same page.
    document_links.uniq!
    
    # select the link if its internal
    internal_links = document_links.select{ |link| @cobweb_links.internal?(link) }

    # if the site has the same content for http and https then normalize to http 
    if @options[:treat_https_as_http]
      internal_links.map!{|link| link.gsub(/^https/, "http")}
    end

    # reject the link if we've crawled it or queued it
    internal_links.reject! { |link| already_handled?(link)}

    lock("internal-links") do
      internal_links.each do |link|
        if within_queue_limits? && !already_handled?(link)
          if status != CobwebCrawlHelper::CANCELLED
            yield link if block_given?
            unless link.nil?
              @redis.sadd "queued", link
              increment_queue_counter
            end
          else
            debug_puts "Cannot enqueue new content as crawl has been cancelled."
          end
        end
      end
    end

    if @options[:store_inbound_links]
      document_links.each do |link|
        uri = URI.parse(link).normalize
        @redis.sadd("inbound_links_#{Digest::MD5.hexdigest(uri.to_s)}", url)
      end
    end
  end
end