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>

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


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
157
158
159
160
161
162
# File 'lib/yahoo_sports/base.rb', line 80

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

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


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/yahoo_sports/base.rb', line 186

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