Sha256: 574faf876dcb1a1ae61fec6a217b84b681de0f22069c99b832f2e7aa36f8a5f2

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

require 'open-uri'
require 'cgi'
require 'nokogiri'

module PageRankr
  class Backlinks
    SEARCH_ENGINE_URLS = {
      :google    => "http://www.google.com/search?q=link%3A",
      :bing      => "http://www.bing.com/search?q=link%3A",
      :yahoo     => "http://siteexplorer.search.yahoo.com/search?p=",
      :altavista => "http://www.altavista.com/web/results?q=link%3A",
      :alltheweb => "http://www.alltheweb.com/search?q=link%3A",
      :alexa     => "http://data.alexa.com/data?cli=10&dat=snbamz&url="
    }

    SEARCH_EGNINE_PATHS = {
      :google    => "//p[@id='resultStats']/b[3]/text()",
      :bing      => "//span[@class='sb_count']/text()",
      :yahoo     => "//ol[@id='results-tab']/li[2]/a/text()",
      :altavista => "//a[@class='lbl']/text()",
      :alltheweb => "//span[@class='ofSoMany']/text()",
      :alexa     => "//linksin/@num"
    }

    def self.lookup(site, *search_engines)
      backlinks = {}
      search_engines.each do |engine|
        next unless SEARCH_ENGINE_URLS[engine]
        doc = Nokogiri::HTML(open(SEARCH_ENGINE_URLS[engine] + CGI.escape(site)))
        count = doc.at(SEARCH_EGNINE_PATHS[engine]).to_s
        count = count.gsub('1-10', '').gsub(/[a-zA-Z,\s\(\)]/, '')
        backlinks[engine] = count.to_i
      end
      backlinks
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
PageRankr-1.0.0 lib/page_rankr/backlinks.rb