Class: NpbFlash::Crawler
- Inherits:
-
Object
- Object
- NpbFlash::Crawler
- Defined in:
- lib/npb_flash/crawler.rb
Instance Method Summary collapse
- #get_game(id) ⇒ Object
- #get_game_by_team(team) ⇒ Object
- #get_metadata ⇒ Object
-
#initialize(date = Date::today) ⇒ Crawler
constructor
A new instance of Crawler.
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
37 38 39 40 41 42 43 44 |
# File 'lib/npb_flash/crawler.rb', line 37 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
46 47 48 49 50 51 |
# File 'lib/npb_flash/crawler.rb', line 46 def get_game_by_team(team) = .find do |m| m[:home] == team or m[:visitor] == team end get_game([:id]) end |
#get_metadata ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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 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 |