Skip to content

Instantly share code, notes, and snippets.

@bcoles
Created September 19, 2011 04:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcoles/1225951 to your computer and use it in GitHub Desktop.
Save bcoles/1225951 to your computer and use it in GitHub Desktop.
Alexa Rank - Retrieves the Alexa rank for domain(s)
#!/usr/bin/env ruby
# Alexa Rank
# Retrieves the Alexa rank for domain(s)
# 2011-09-19 # bcoles@gmail.com
##
verbose = true
version = "0.1"
# Usage
if ARGV.size == 0
puts "[*] Alexa Rank v" + version
puts "[*] Usage: #{$0} example.com"
exit 1
end
# Load gems
%w|net/http rexml/document|.each do |gem|
begin
require gem
rescue LoadError
puts "[-] " + gem + " is require"
exit 1
end
end
# Get page rank from Alexa
def get_alexa_rank(url)
r={}
if url =~ /^(https?:\/\/)?([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})/i
begin
domain = "#{$2}"
xml = Net::HTTP.get_response(URI.parse("http://data.alexa.com/data?cli=10&url="+domain)).body
rank = REXML::Document.new(xml).elements['ALEXA/SD/POPULARITY'].attributes['TEXT'].gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
r = { :domain=>domain, :rank=>rank }
rescue
end
end
r
end
# Display page rank(s) for domain(s)
ARGV.each do|domain|
puts "[*] Fetching page rank for " + domain if verbose
r = get_alexa_rank(domain)
if r.empty?
puts "[-] "+domain+" is not ranked with Alexa" if verbose
else
puts "[+] "+r[:domain]+" is ranked "+r[:rank] if verbose
puts r[:domain] +" " + r[:rank] if !verbose
end
end
@bcoles
Copy link
Author

bcoles commented Sep 19, 2011

Example output

With verbose = true

$ ./alexa-rank.rb
[*] Alexa Rank v0.1
[*] Usage: ./alexa-rank.rb example.com

$ ./alexa-rank.rb example.com
[*] Fetching page rank for example.com
[+] example.com is ranked 57,959

With verbose = false

$ ./alexa-rank.rb example.com
example.com 57,959

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment