Class: NpbFlash::Crawler

Inherits:
Object
  • Object
show all
Defined in:
lib/npb_flash/crawler.rb

Instance Method Summary collapse

Constructor Details

#initialize(date = Date::today) ⇒ Crawler

Returns a new instance of Crawler.



8
9
10
# File 'lib/npb_flash/crawler.rb', line 8

def initialize(date = Date::today)
  @date = date
end

Instance Method Details

#get_game(id) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/npb_flash/crawler.rb', line 31

def get_game(id)
  uri = 'http://baseball.yahoo.co.jp/npb/game/' + id + '/text'
  html = open(uri) do |f|
    f.read
  end
  doc = Nokogiri::HTML.parse(html, nil, 'utf-8')
  Game::from_node(doc, id)
end

#get_game_by_team(team) ⇒ Object



40
41
42
43
44
45
# File 'lib/npb_flash/crawler.rb', line 40

def get_game_by_team(team)
  meta = .find do |m|
    m[:home] == team or m[:visitor] == team
  end
  get_game(meta[:id])
end

#get_metadataObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/npb_flash/crawler.rb', line 12

def 
  uri = 'http://baseball.yahoo.co.jp/npb/schedule/?date=' + @date.strftime('%Y%m%d')
  html = open(uri) do |f|
    f.read
  end
  doc = Nokogiri::HTML.parse(html, nil, 'utf-8')
  doc.xpath('//div[@id="gm_sch"]//table[@class="teams"]').map do |g|
    visitor, home = g.xpath('.//tr//th[1]//a/div/@class').map do |s|
      s.text.split(/\s/)[1]
    end
    id = g.xpath('.//table[@class="score"]//tr[2]//a')[0]['href'].match(/[\d]+/)[0]
    {
      id: id,
      home: home,
      visitor: visitor
    }
  end
end