Skip to content

Instantly share code, notes, and snippets.

@xavdid
Created November 5, 2014 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xavdid/bbadad738ea63263eb74 to your computer and use it in GitHub Desktop.
Save xavdid/bbadad738ea63263eb74 to your computer and use it in GitHub Desktop.
Make a roster from info pulled from quidditch-reference.com
require 'open-uri'
require 'nokogiri'
require 'pp'
require 'json'
# currently hardcoded for the MWRC event
def pull_players(team_code)
doc = Nokogiri::HTML(open("http://www.quidditch-reference.com/tournament/160/#{team_code}"))
rows = doc.xpath("//table//tr[position() > 1]")
team_name = doc.css("h2")[0].children[3].to_s
# generate a random team id with any letter or number
team_id = [('A'..'Z'), ('a'..'z'), (0..9)].map{|a| a.to_a}.flatten.shuffle[0..10].join
players = []
rows.each do |row|
player = {}
player[:number] = row.children[1].children[0].to_s
n = row.children[3].children[0].children[0].to_s.split(', ')
player[:fname] = n[1]
player[:lname] = n[0]
player[:team_name] = team_name
player[:team_id] = team_id
players << player
end
players
end
teams = []
root = Nokogiri::HTML(open("http://www.quidditch-reference.com/tournament/160/"))
rows = root.xpath("//table//tr[position() > 1]")
rows.each do |row|
team = {}
# these specifically address the structure on quidditch-reference
team[:name] = row.children[1].children[0].children[0].to_s
team[:code] = row.children[1].children[2].children[1].attributes['href'].value
team[:players] = pull_players(team[:code])
teams << team
end
File.open('mwrc_rosters.json','wb+') do |f|
# formats with 4space indents instead of 2
f.write(JSON.pretty_generate(teams,{indent: " "}))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment