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



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

def get_game(id)
  uri = 'http://baseball.yahoo.co.jp/npb/game/' + id + '/text'
  begin
    doc = Nokogiri::HTML(open(uri), uri, 'utf-8')
  rescue OpenURI::HTTPError => e
    return nil
  end
  Game::from_node(doc, id)
end

#get_game_by_team(team) ⇒ Object



48
49
50
51
52
53
# File 'lib/npb_flash/crawler.rb', line 48

def get_game_by_team(team)
  meta = .find do |m|
    m[:home] == team or m[:visitor] == team
  end
  meta.nil? ? nil : 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
30
31
32
33
34
35
36
# File 'lib/npb_flash/crawler.rb', line 12

def 
  uri = 'http://baseball.yahoo.co.jp/npb/schedule/?date=' + @date.strftime('%Y%m%d')
  begin
    doc = Nokogiri::HTML(open(uri), uri, 'utf-8')
  rescue OpenURI::HTTPError => e
    return nil
  end
   = 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
    ref = g.xpath('.//table[@class="score"]//tr[2]//a')
    if ref.size == 1
      id = ref[0]['href'].match(/[\d]+/)[0]
      {
        id: id,
        home: home,
        visitor: visitor
      }
    else
      nil
    end
  end
  .compact
end