golfscrape 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/golfscrape.gemspec +26 -0
- data/lib/golfscrape/client.rb +82 -0
- data/lib/golfscrape/version.rb +3 -0
- data/lib/golfscrape.rb +18 -0
- data/spec/golfscrape/client_spec.rb +37 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/vcr_cassettes/events.yml +602 -0
- data/spec/vcr_cassettes/leaderboard.yml +5190 -0
- data/spec/vcr_cassettes/rankings.yml +956 -0
- data/spec/vcr_setup.rb +8 -0
- metadata +155 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Golfscrape - PGA Tour Data
|
2
|
+
|
3
|
+
Ruby class to get data from the PGA Tour
|
4
|
+
|
5
|
+
Leaderboard from Yahoo
|
6
|
+
|
7
|
+
Rankings from SportsNetwork
|
8
|
+
|
9
|
+
Events from ESPN
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Inside your Gemfile:
|
14
|
+
gem 'golfscrape'
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
### Instantiate a client
|
19
|
+
|
20
|
+
>> @client = Golfscrape::Client.new
|
21
|
+
|
22
|
+
## Examples
|
23
|
+
|
24
|
+
#### Leaderboard
|
25
|
+
|
26
|
+
>> @leaderboard = @client.leaderboard
|
27
|
+
=> [<#Hashie::Mash first_round="56" fourth_round="-" name="Michael Bradley" position="1" second_round="-" strokes="56" third_round="-" thru="16" today="-6" total="-6">, <#Hashie::Mash first_round="61" fourth_round="-" name="Chris Couch" position="T2" second_round="-" strokes="61" third_round="-" thru="17" today="-5" total="-5">, <#Hashie::Mash first_round="57" fourth_round="-" name="Bryce Molder" position="T2" second_round="-" strokes="57" third_round="-" thru="16" today="-5" total="-5">, <#Hashie::Mash first_round="54" fourth_round="-" name="John Rollins" position="T2" second_round="-" strokes="54" third_round="-" thru="15" today="-5" total="-5">...]
|
28
|
+
|
29
|
+
#### Rankings
|
30
|
+
>> @rankings = @client.rankings
|
31
|
+
=> [<#Hashie::Mash avg=nil country=nil events=nil name=nil points=nil rank=nil>, <#Hashie::Mash avg="9.06" country="Eng" events="53" name="Luke Donald" points="480.41" rank="1">, <#Hashie::Mash avg="8.80" country="Eng" events="48" name="Lee Westwood" points="422.59" rank="2">, <#Hashie::Mash avg="7.22" country="Deu" events="49" name="Martin Kaymer" points="353.89" rank="3">, <#Hashie::Mash avg="7.19" country="Nir" events="53" name="Rory McIlroy" points="380.93" rank="4">, <#Hashie::Mash avg="6.50" country="USA" events="41" name="Steve Stricker" points="266.51" rank="5">, <#Hashie::Mash avg="6.01" country="USA" events="45" name="Phil Mickelson" points="270.50" rank="6">, <#Hashie::Mash avg="5.67" country="USA" events="52" name="Matt Kuchar" points="295.08" rank="7">, <#Hashie::Mash avg="5.53" country="Nir" events="55" name="Graeme McDowell" points="304.12" rank="8">, <#Hashie::Mash avg="5.47" country="Aus" events="44" name="Jason Day" points="240.69" rank="9">, <#Hashie::Mash avg="5.24" country="Zaf" events="56" name="Charl Schwartzel" points="293.59" rank="10">, <#Hashie::Mash avg="5.11" country="USA" events="49" name="Dustin Johnson" points="250.37" rank="11">, <#Hashie::Mash avg="4.99" country="Eng" events="44" name="Paul Casey" points="219.57" rank="12">, <#Hashie::Mash avg="4.87" country="USA" events="47" name="Bubba Watson" points="228.66" rank="13">, <#Hashie::Mash avg="4.72" country="Eng" events="52" name="Ian Poulter" points="245.49" rank="14">, <#Hashie::Mash avg="4.64" country="USA" events="51" name="Nick Watney" points="236.54" rank="15">, <#Hashie::Mash avg="4.39" country="Kor" events="53" name="K.J. Choi" points="232.59" rank="16">..]
|
32
|
+
|
33
|
+
#### Events
|
34
|
+
>> @events = @client.events
|
35
|
+
=> [<#Hashie::Mash end_date=Sun Jul 03 00:00:00 -0500 2011 location="Aronimink Golf Club, Newtown Square, PA" name="AT&T National" start_date=Thu Jun 30 00:00:00 -0500 2011>, <#Hashie::Mash end_date=Sun Jul 10 00:00:00 -0500 2011 location="TPC Deere Run, Silvis, IL" name="John Deere Classic" start_date=Thu Jul 07 00:00:00 -0500 2011>, <#Hashie::Mash end_date=Sun Jul 17 00:00:00 -0500 2011 location="Annandale Golf Club, Madison, MS" name="Viking Classic" start_date=Thu Jul 14 00:00:00 -0500 2011>, <#Hashie::Mash end_date=Sun Jul 17 00:00:00 -0500 2011 location="Royal St. Georges Golf Club, Sandwich, England" name="British OpenRoyal St. Georges Golf Club, Sandwich, England" start_date=Thu Jul 14 00:00:00 -0500 2011>...]
|
36
|
+
|
37
|
+
## Copyright
|
38
|
+
|
39
|
+
Contact me if you have any suggestions and feel free to fork it!
|
40
|
+
|
41
|
+
Copyright (c) 2009 Johnny Khai Nguyen, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/golfscrape.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "golfscrape/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "golfscrape"
|
7
|
+
s.version = Golfscrape::VERSION
|
8
|
+
s.authors = ["Johnny Khai Nguyen"]
|
9
|
+
s.email = ["johnnyn@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Ruby class to scrape PGA Tour golf data}
|
12
|
+
s.description = %q{Use this gem to get PGA Tour data for tournaments, world rankings, and the leaderboard}
|
13
|
+
|
14
|
+
s.rubyforge_project = "golfscrape"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'nokogiri'
|
22
|
+
s.add_dependency 'hashie'
|
23
|
+
s.add_development_dependency 'rspec'
|
24
|
+
s.add_development_dependency 'webmock'
|
25
|
+
s.add_development_dependency 'vcr'
|
26
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Golfscrape
|
2
|
+
|
3
|
+
class Client
|
4
|
+
def initialize
|
5
|
+
end
|
6
|
+
|
7
|
+
def leaderboard
|
8
|
+
response = []
|
9
|
+
url = "http://sports.yahoo.com/golf/pga/leaderboard"
|
10
|
+
doc = Nokogiri::HTML(open(url))
|
11
|
+
doc.css('div#leaderboardtable table.sportsTable').first.css('tbody tr').each do |player|
|
12
|
+
begin
|
13
|
+
@position = player.css('td.position').children.last.content.gsub("\n", "")
|
14
|
+
@name = player.css('td.player').css('a').first.children.last.content
|
15
|
+
@first = player.css('td')[2].children.last.content.gsub("\n", "")
|
16
|
+
@second = player.css('td')[3].children.last.content.gsub("\n", "")
|
17
|
+
@third = player.css('td')[4].children.last.content.gsub("\n", "")
|
18
|
+
@fourth = player.css('td')[5].children.last.content.gsub("\n", "")
|
19
|
+
@today = player.css('td')[6].children.last.content.gsub("\n", "")
|
20
|
+
@thru = player.css('td')[7].children.last.content.gsub("\n", "")
|
21
|
+
@total = player.css('td')[8].children.last.content.gsub("\n", "")
|
22
|
+
@strokes = player.css('td')[9].children.last.content.gsub("\n", "")
|
23
|
+
rescue
|
24
|
+
end
|
25
|
+
|
26
|
+
response << Hashie::Mash.new(:position => @position, :name => @name, :first_round => @first, :second_round => @second, :third_round => @third,
|
27
|
+
:fourth_round => @fourth, :today => @today, :thru => @thru, :total => @total, :strokes => @strokes)
|
28
|
+
end
|
29
|
+
|
30
|
+
response
|
31
|
+
end
|
32
|
+
|
33
|
+
def rankings
|
34
|
+
response = []
|
35
|
+
url = "http://www.sportsnetwork.com/merge/tsnform.aspx?c=azcentral&page=golf-m/stand/WORLD-MEN-GOLF-RANK.htm"
|
36
|
+
doc = Nokogiri::HTML(open(url))
|
37
|
+
doc.css('table')[1].css('table tr').each do |player|
|
38
|
+
begin
|
39
|
+
|
40
|
+
unless player.css('td').first.content == "Rank" || player.css('td').first.content.match(/NOTES/)
|
41
|
+
@rank = player.css('td').first.content
|
42
|
+
@name = player.css('td')[1].content
|
43
|
+
@country = player.css('td')[2].content
|
44
|
+
@avg = player.css('td')[3].content
|
45
|
+
@points = player.css('td')[4].content
|
46
|
+
@events = player.css('td')[5].content
|
47
|
+
end
|
48
|
+
|
49
|
+
rescue
|
50
|
+
end
|
51
|
+
|
52
|
+
response << Hashie::Mash.new(:rank => @rank, :name => @name, :country => @country, :avg => @avg, :points => @points, :events => @events)
|
53
|
+
end
|
54
|
+
|
55
|
+
response.uniq
|
56
|
+
end
|
57
|
+
|
58
|
+
def events
|
59
|
+
response = []
|
60
|
+
url = "http://espn.go.com/golf/schedule"
|
61
|
+
doc = Nokogiri::HTML(open(url))
|
62
|
+
events = doc.css('table.tablehead')[2].css('tr')
|
63
|
+
events.each do |event|
|
64
|
+
begin
|
65
|
+
unless events.index(event) == 0 || events.index(event) == 1
|
66
|
+
@dates = event.css('td').css('nobr').first.children.first.content.split(" - ")
|
67
|
+
@start_date = Time.parse(@dates.first)
|
68
|
+
@end_date = Time.parse(@dates.last)
|
69
|
+
@name = event.css('td')[1].children.first.content
|
70
|
+
@location = event.css('td')[1].css('em').first.children.first.content
|
71
|
+
end
|
72
|
+
rescue
|
73
|
+
end
|
74
|
+
|
75
|
+
response << Hashie::Mash.new(:start_date => @start_date, :end_date => @end_date, :name => @name, :location => @location)
|
76
|
+
end
|
77
|
+
|
78
|
+
response.uniq
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
data/lib/golfscrape.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'hashie'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
directory = File.expand_path(File.dirname(__FILE__))
|
8
|
+
|
9
|
+
module Golfscrape
|
10
|
+
|
11
|
+
def self.configure
|
12
|
+
yield self
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
require File.join(directory, 'golfscrape', 'client')
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Golfscrape::Client do
|
4
|
+
context "leaderboard" do
|
5
|
+
use_vcr_cassette 'leaderboard'
|
6
|
+
|
7
|
+
it "should return leaderboard" do
|
8
|
+
@client = Golfscrape::Client.new
|
9
|
+
@leaderboard = @client.leaderboard
|
10
|
+
@leaderboard.last.name.should match /\S+/
|
11
|
+
@leaderboard.size.should_not == 0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "events" do
|
16
|
+
use_vcr_cassette 'events'
|
17
|
+
|
18
|
+
it "should return events" do
|
19
|
+
@client = Golfscrape::Client.new
|
20
|
+
@events = @client.events
|
21
|
+
@events.last.name.should match /\S+/
|
22
|
+
@events.size.should_not == 0
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "rankings" do
|
27
|
+
use_vcr_cassette 'rankings'
|
28
|
+
|
29
|
+
it "should return rankings" do
|
30
|
+
@client = Golfscrape::Client.new
|
31
|
+
@rankings = @client.rankings
|
32
|
+
@rankings.last.name.should match /\S+/
|
33
|
+
@rankings.size.should_not == 0
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|