Skip to content

Instantly share code, notes, and snippets.

@zimbatm
Created March 2, 2009 23:32
Show Gist options
  • Save zimbatm/73074 to your computer and use it in GitHub Desktop.
Save zimbatm/73074 to your computer and use it in GitHub Desktop.
Kongregate game rating bot
#!/usr/bin/env ruby
=begin notice
Hi, what's up ?
This is a prototype to rate all games on Kongregate. Since there are 704 pages with 15 pages on each, and that rating a game brings 1 point, you should easily obtain 10k points, which means a level 25 or so (if admin staff doesn't delete your profile).
NOTE: there is still a bug in the rating values
NOTE2: I didn't use this script on my profile
Dependencies: ruby and the mechanize (0.9.1) gem
Have fun,
zimbatm
=end
require 'rubygems'
require 'mechanize'
ENTRY_URL = 'http://www.kongregate.com/latest_games'
USER = 'YOUR_USERNAME'
PASS = 'YOUR_PASSWORD'
def error(msg)
STDERR.puts "Error: #{msg}"
exit 1
end
def get_next_page(agent, page)
link = page.search("ul.pagination li a").last
if link.to_s =~ /next/i
agent.get(link['href'])
end
end
def game_rating(page)
puts elem
md = elem.to_s.match( /(\d+.?\d*)\/\d+/ )
error "Page is not a game" unless md
md.captures.first.to_f
end
def vote_for_game(agent, game_url)
page = agent.get(game_url)
game_title = page.search("#secondary h2").first.to_s.sub("<h2>",'').sub("</h2>",'')
elems = page.search("#game_rating .current-rating")
if elems.size == 2
puts "#{game_title}: already voted"
return false
else
current_rating = elems.first.to_s.match( /(\d+.?\d*)\/\d+/ ).captures.first.to_f
my_vote = current_rating.to_i + 1
game_id = page.search("a.send").first["href"].to_s.match(/game_id=(\d+)/).captures.first
# /ratings?rateable_id=27204&amp;rateable_type=Game&amp;rating=1
res = agent.post("http://www.kongregate.com/ratings?rateable_id=#{game_id}&rateable_type=Game&rating=#{my_vote}", {})
puts "#{game_title}: voted #{my_vote} for #{current_rating}"
puts res.body
exit
true
end
end
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Mac Safari'
# Login
page = agent.get(ENTRY_URL);
login_form = page.forms.find{|f|f.action == "/session.js"}
unless login_form
error "Could not login, page changed"
end
login_form.username = USER
login_form.password = PASS
agent.submit(login_form, login_form.buttons.last)
page = agent.get(ENTRY_URL);
login_form = page.forms.find{|f|f.action == "/session.js"}
error "Could not login for some reason" if login_form
puts "*** Login: OK ***"
start_time = Time.now
stats = Hash.new(0)
page_num = 0
stop = false
trap("INT"){ stop = true }
# Loop on game pages
loop do
page_num += 1
puts "*** Page #{page_num} ***"
for game_url in page.search("#games td.first a").map{|x|x['href']}.uniq
if vote_for_game(agent, game_url)
stats[:voted] += 1
end
stats[:count] += 1
break if stop
end
if stats[:count] % 10 == 0
puts "#{stats[:count]} explored"
end
break if stop
break unless page = get_next_page(agent, page)
end
if stop
puts "Stopped at page #{page_num}"
else
puts "All games rated"
end
puts "Stats: #{stats.inspect}"
puts "Bye."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment