Create a gist now

Instantly share code, notes, and snippets.

RankyPanky SERP Checker
require 'sinatra'
require 'nokogiri'
require 'open-uri'
get '/serp_checker' do
"<form action='/ranked' method='post'>
<label for='keyword'>Keyword</label>
<textarea name='keyword' id='keyword' type='text' /></textarea>
<label for='url'>URL</label>
<input name='url' id='url' type='text' />
<input type='submit' value='Go!' />
</form>"
end
def clean_up_keywords(str)
str.gsub("\n", ",").delete("\r").split(',')
end
def clean_up_list(arr)
arr.reject(&:empty?).each(&:lstrip!)
end
def make_strings_url_friendly(arr)
arr.each do |e|
e.gsub!(" ", "+")
end
end
def clean_url(url)
url.gsub!("www.", "")
url.gsub!("http://","")
if url.end_with?("/") == true
url.chop
end
end
def make_urls(arr)
arr.map {|e| "http://www.google.com/search?num=10&q=" + e}
end
def assoc_results_keywords(keywords, url)
association = {}
keywords.each_with_index do |keyword, index|
results = []
page = open(url[index],
"User-Agent" => "Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) " +
"Gecko/20110324 Firefox/4.2a1pre")
Nokogiri::HTML(page).search("cite").each do |e|
puts e.inner_text
results << e.inner_text
end
association[keyword] = results
end
association
end
post '/ranked' do
site_url = clean_url(params[:url])
dirty_list = clean_up_keywords(params[:keyword])
clean_list = clean_up_list(dirty_list)
url_ready_list = make_strings_url_friendly(clean_list)
url_list = make_urls(url_ready_list)
assoc_results_keywords(clean_list, url_list).to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment