Class: YahooSports::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo_sports/base.rb

Direct Known Subclasses

MLB, NBA, NFL, NHL

Class Method Summary collapse

Class Method Details

.get_homepage_games(sport, state = "") ⇒ Array<OpenStruct>

Get the scoreboard games for the given sport. Includes recently completed, live and upcoming games.

Source: sports.yahoo.com/<sport>

Game struct has the following keys:

game.date   # date of game; includes time if preview
game.team1  # visiting team
game.team2  # home team
game.score1 # team1's score, if live or final
game.score2 # team2's score, if live or final
game.state  # live, final or preview
game.tv     # TV station showing the game, if preview and available

Example:

#<OpenStruct state="final", score1="34", date=Thu Nov 26 00:00:00 -0500 2009, score2="12", team1="Green Bay", team2="Detroit">

Parameters:

  • sport (String)

    sport to list, can be one of [“mlb”, “nba”, “nfl”, “nhl”]

  • state (String) (defaults to: "")

    Optionally filter for the given state (“live”, “final”, or “preview”)

Returns:

  • (Array<OpenStruct>)

    list of games



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/yahoo_sports/base.rb', line 74

def self.get_homepage_games(sport, state = "")

    sport.downcase!
    if sport !~ /^(nba|nhl|nfl|mlb)$/ then
        raise sprintf("Invalid param for 'sport' = '%s'", sport)
    end

    state.downcase! if not state.empty?
    if not state.empty? and state !~ /^(live|final|preview)$/ then
        raise sprintf("Invalid param for 'state' = '%s'", state)
    end

    html = YahooSports.fetchurl("http://sports.yahoo.com/#{sport}/proxy/html/scorethin")
    if not html then
        raise 'Error fetching url'
    end

    sports_game = Scraper.define do

        array :teams
        array :scores

        process "li.odd, li.even, li.live", :date_src => "@id"
        process "li.odd, li.even, li.live", :class_src => "@class"
        process "li.link-box", :extra_src => :text

        process "td.team>a", :teams => :text
        process "td.score", :scores => :text

        process "li.status>a", :status => :text


        result :date_src, :teams, :scores, :status, :class_src, :extra_src

    end

    sports = Scraper.define do

        array :games

        process "ul.game-list>li", :games => sports_game

        result :games

    end

    games_temp = sports.scrape(html)
    games = []
    return games if games_temp.nil?

    games_temp.each { |g|

        gm = OpenStruct.new
        gm.team1 = g.teams[0].strip if g.teams[0]
        gm.team2 = g.teams[1].strip if g.teams[1]
        gm.score1 = g.scores[0].strip if g.scores[0]
        gm.score2 = g.scores[1].strip if g.scores[1]

        if g.class_src.include? ' ' then
            gm.state = g.class_src[ g.class_src.index(' ')+1, g.class_src.length ].strip
        else
            gm.state = g.class_src.strip
        end

        gm.tv = $1 if g.extra_src =~ /TV: (.*)/

        status = g.status.strip if g.status
        time_str = (gm.state == "preview" ? " #{status}" : "")

        if sport == 'mlb' then
            gm.date = Time.parse(Time.new.strftime('%Y') + g.date_src[2,4] + time_str)
        else
            gm.date = Time.parse(g.date_src[0,8] + time_str)
        end

        next if not state.empty? and state != gm.state
        games << gm

    }

    return games

end

.get_team_stats(sport, str) ⇒ OpenStruct

Retrieves team information for the team in the given sport

Source: sports.yahoo.com/<sport>/teams/<team>

Team struct has the following keys:

team.name       # full team name
team.standing   # current standing
team.position   # position in the conference
team.last5      # previous games results
team.next5      # upcoming scheduled games
team.live       # struct describing in-progress game, if available

Games in the last5 and next5 lists have the following keys:

game.date       # date of game
game.team       # full team name
game.status     # score for completed games (e.g. "L 20 - 23") or "preview"
game.away       # boolean value indicating an away game

Parameters:

  • sport (String)

    sport to list, can be one of [“mlb”, “nba”, “nfl”, “nhl”]

  • str (String)

    3-letter team code or partial team name

Returns:

  • (OpenStruct)

    team info



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/yahoo_sports/base.rb', line 180

def self.get_team_stats(sport, str)

    sport.downcase!
    if sport !~ /^(nba|nhl|nfl|mlb)$/ then
        raise sprintf("Invalid param for 'sport' = '%s'", sport)
    end

    str.downcase!
    (team, html) = find_team_page(sport, str)
    if html.nil? then
        raise sprintf("Can't find team '%s'", str)
    end

    info = get_team_info(html)
    last5, next5 = get_scores_and_schedule(html)
    live_game = get_live_game(info.name, html)

    return OpenStruct.new({:name => info.name,
                           :standing => info.standing,
                           :position => info.position,
                           :last5 => last5,
                           :next5 => next5,
                           :live  => live_game})
end