Sunday, November 02, 2008

collect Golf's World Rankings on your own with new Ruby module!

...Official Golf World Ranking, OGWR...
wrote some Ruby code in a Module that grabs the official golf world rankings...
use the data as you wish in your own application...
released as open source...
enables anyone to take the active OGWR data and do with it what you wish
(E.g. store to a database each week, hold in your database to related to players on your website, etc).

...it's decent for a start, planning on continuing to make it more versatile and useful...
You can find the code on GitHub at the links above.
#!/usr/bin/env ruby -w
# created by Mark Holton (holtonma@gmail.com)
# copy as much as you want to
# 10-29-2008
# purpose: scrape the official world golf ranking, and present it in a more
# usable form (Array of ostruct's)
# using Hpricot, open-uri
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'ostruct'
module OGWR
VERSION = '0.1.0'
class PageFetcher
def fetch(ogwr_url, page_num)
#page_num indicates range of 50n: page 1 >> 1-50, page 2 >> 51-100, page 3 >> 101-150...
data = Hpricot(open(ogwr_url)).search("table:nth-child(5)")
players = [] #init
start_rank = -1 + 50*(page_num-1) #(there are 2 empty trs to start the world ranking) #instead of a counter, I should extract this from page
(data/"td:nth-child(2)").each do |x|
playa = OpenStruct.new
playa.fname = x.search("a").inner_html.split(" ")[0]
playa.lname = x.search("a").inner_html.split(" ")[1]
playa.rank = start_rank
puts "#{playa.fname} #{playa.lname} #{playa.rank}"
players << playa
start_rank += 1
end
#clean this line up:
players.pop; players.reverse!; players.pop; players.pop; players.reverse! #remove one extra row at end, 2 extra at beginning
players
end
end
end
view raw ogwr.rb hosted with ❤ by GitHub

2 comments:

Ideation Fashion said...

Good sharing of this nice post.
hermes belt

Ideation Fashion said...

Wonderful job. Thanks for sharing such a good post.
black birkin bag