Navigation Menu

Skip to content

Commit

Permalink
Added backlink lookups.
Browse files Browse the repository at this point in the history
  • Loading branch information
blatyo committed Apr 17, 2010
1 parent 78db373 commit d3cf1ea
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
22 changes: 15 additions & 7 deletions README.md
@@ -1,7 +1,15 @@
# page_rankr
# PageRankr

Provides an easy way to retrieve Google Page Rank, Alexa Rank, and backlink counts.

## Exampes

### Backlinks
Backlinks are the result of doing a search with a query like "link:www.google.com". The number of returned results indicates how many sites point to that url.

`PageRankr.backlinks 'www.google.com', :google, :bing, :yahoo, :altavista, :alltheweb`
` #=> {:google=>161000, :bing=>208000000, :yahoo=>256300062, :altavista=>110000000, :alltheweb=>55700000}`

## Note on Patches/Pull Requests

* Fork the project.
Expand All @@ -13,12 +21,12 @@ Provides an easy way to retrieve Google Page Rank, Alexa Rank, and backlink coun
* Send me a pull request. Bonus points for topic branches.

## TODO
* Get backlink counts for:
* Google
* Bing
* Yahoo!
* AltaVista
* AllTheWeb
* <strike>Get backlink counts for:</strike>
* <strike>Google</strike>
* <strike>Bing</strike>
* <strike>Yahoo!</strike>
* <strike>AltaVista</strike>
* <strike>AllTheWeb</strike>
* Get Google Page Rank
* Implement Hashing Algorithm
* Get Alexa ranking
Expand Down
13 changes: 13 additions & 0 deletions lib/page_rankr.rb
@@ -0,0 +1,13 @@
path = File.expand_path(File.dirname(__FILE__))
require 'open-uri'
require 'cgi'
require 'hpricot'
require path + '/page_rankr/backlinks'

module PageRankr
class << self
def backlinks(site, *search_engines)
Backlinks.lookup site, *search_engines
end
end
end
29 changes: 29 additions & 0 deletions lib/page_rankr/backlinks.rb
@@ -0,0 +1,29 @@
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"
}

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()"
}

def self.lookup(site, *search_engines)
backlinks = {}
search_engines.each do |engine|
next unless SEARCH_ENGINE_URLS[engine]
doc = open(SEARCH_ENGINE_URLS[engine] + CGI.escape(site)) {|results| Hpricot(results)}
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

0 comments on commit d3cf1ea

Please sign in to comment.