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 = '') ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# File 'lib/yahoo_sports/base.rb', line 42

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)
    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.status = g.status.strip if g.status
        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 sport == 'mlb' then
            gm.date = Time.parse(Time.new.strftime('%Y') + g.date_src[2,4])
        else
            gm.date = Time.parse(g.date_src[0,8])
        end
        
        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.extra = $1 if g.extra_src =~ /TV: (.*)/
        
        next if not state.empty? and state != gm.state
        games << gm
    
    }
    
    return games

end

.get_team_stats(sport, str) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/yahoo_sports/base.rb', line 126

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