Create a gist now

Instantly share code, notes, and snippets.

Grab some pvp stats in World of Warcraft
require 'nokogiri'
require 'open-uri'
require 'gruff'
# Extend Pie chart class to set custom labels
class LabeledPie < Gruff::Pie
def data(name, data_points = [], options = {})
super(name, data_points, options[:color])
@data.each { |data_array| data_array << options[:label] }
end
private
def slice_class
CustomLabeledSlice
end
class CustomLabeledSlice < ::Gruff::Pie::PieSlice
def label
data_array[3] || super
end
end
end
# URL where we grab the data
url = 'https://worldofwarcraft.com/en-gb/game/pvp/leaderboards'
# Wow Class colors
COLORS = {
'mage' => '#68ccef',
'monk' => '#00ffba',
'priest' => '#ffffff',
'warlock' => '#9382c9',
'deathknight' => '#c41e3b',
'paladin' => '#f48cba',
'warrior' => '#c69b6d',
'rogue' => '#fff468',
'shaman' => '#2359ff',
'druid' => '#ff7c0a',
'demonhunter' => '#a330c9',
'hunter' => '#aad372'
}
%w(2v2 3v3).each do |arena|
# Plus 1 the counter when we found the wow class in the leaderboard
counters = {
'mage' => 0,
'monk' => 0,
'priest' => 0,
'warlock' => 0,
'deathknight' => 0,
'paladin' => 0,
'warrior' => 0,
'rogue' => 0,
'shaman' => 0,
'druid' => 0,
'demonhunter' => 0,
'hunter' => 0
}
tot = 0
# Parse the 10 pages of the leaderboard
10.times do |i|
doc = Nokogiri::HTML(open("#{url}/#{arena}?page=#{i+1}"))
doc.search('a.Character').each do |row|
regexp = counters.keys.reduce(){|x,y| "#{x}|#{y}"}
klass = row[:class].downcase[/#{regexp}/]
if klass
counters[klass] += 1
tot += 1
end
end
end
# Sort counters
counters = counters.sort{|x, y| y[1] <=> x[1]}
# Generate pie graph
g = LabeledPie.new
g.title = "Stats #{arena} updated at #{Time.now.strftime('%d/%m/%Y')} for\nthe first #{tot} best EU players"
g.legend_font_size = 14
g.marker_font_size = 12
g.colors = []
counters.each { |klass, counter| g.colors << COLORS[klass] }
counters.each do |klass, counter|
g.data(klass.capitalize, counter, label: "#{klass.capitalize}: #{(counter*100.0/tot).round}% (#{counter})")
end
g.write("stats_#{arena}.png")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment