public
Created

  • Download Gist
rankclient.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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
#!/usr/bin/env ruby
# encoding: UTF-8
 
require 'yaml'
require 'ya2yaml'
require 'nokogiri'
require 'open-uri'
 
class Rank
attr_accessor :engine
def initialize engine
list = %w{Lycos Google Yahoo!}
raise "unknown search engine!" if !list.include?(engine)
 
# Configuration
host = 'www.somehost.com'
port = '80'
@user = 'user'
@pass = 'password'
@engine = engine
@URL = "http://#{host}:#{port}"
@path = File.expand_path(File.dirname(__FILE__), ".")
end
 
# That's the panic button. When ready to roll pres 'run' :-P
def run
fetch()
array = []
word_list = YAML.load_file('/tmp/howdb_words.yml')
word_list.each do |word|
date = Time.now.utc.to_s
num = rank(word)
hash = {word: word, rank: num, engine: @engine, date: date}
array.push(hash)
end
data = array.ya2yaml
file = ("#{@path}/files/#{@filename}")
File.open(file, 'w') {|f| f.write(data)}
`curl --silent --location --upload-file #{file} --user #{@user}:#{@pass} #{@URL}/restricted/uploads/`
end
 
private
 
# Decide which @engine to use
def rank word
case @engine
when "Lycos"
lycos_ranking(word)
when "Google"
google_ranking(word)
when "Yahoo!"
yahoo_ranking(word)
else
puts "Engine not found. How did you get here???"
end
end
 
# Fetch 'YAML' file from remote location
def fetch
begin
File.open("/tmp/howdb_words.yml", "wb") do |saved_file|
open("#{@URL}/restricted/list", 'rb', :http_basic_authentication=>[@user, @pass]) do |read_file|
@filename = read_file.base_uri.to_s.scan(/\w{5}.yml/)[0]
saved_file.write(read_file.read)
end
end
rescue
puts "Couldn't fetch data"
puts "An error occurred: #{$!}"
end
end
 
# Get google ranking
def google_ranking(item)
word = URI.encode(item) # UTF8toASCII for the web
result = open("http://www.google.co.in/search?q=#{word}", "User-Agent" => "HTTP_USER_AGENT:Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.47 Safari/534.13").read()
doc = Nokogiri::HTML(result)
result = doc.css("#resultStats").text
ok = result.split(' ')[1]
r = ok.gsub(',','.')
return r
end
 
# Get lycos ranking
def lycos_ranking(item)
word = URI.encode(item) # UTF8toASCII for the web
req = open("http://search.lycos.com/web?q=#{word}", "User-Agent" => "HTTP_USER_AGENT:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101 Firefox/15.0.1").read
doc = Nokogiri::HTML(req)
result = doc.css(".searchNumbers").text.split(' ')[4]
e = ''
begin
ranking = result.gsub(',','.')
rescue Exception => e
ranking = 0 # The exception here usually means 'no results' or ranking = 0
end
return ranking
end
 
# Get yahoo ranking
def yahoo_ranking(item)
# word = URI.encode(item)
# TODO
end
end
 
x = Rank.new('Google')
x.run

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.