Skip to content

Instantly share code, notes, and snippets.

@ursm
Created September 1, 2015 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ursm/6119a058d1682e47d806 to your computer and use it in GitHub Desktop.
Save ursm/6119a058d1682e47d806 to your computer and use it in GitHub Desktop.
require 'json'
require 'nokogiri'
require 'open-uri'
def find_complete_row(doc, i)
prev = doc.at_css("#rgn_content4 tbody tr:nth-child(#{i.pred})").css('td').map(&:text)
prev.size == 10 ? prev : find_complete_row(doc, i.pred)
end
html = open('http://wikiwiki.jp/kancolle/?%B2%FE%BD%A4%B9%A9%BE%B3')
doc = Nokogiri::HTML.parse(html, nil, 'EUC-JP')
json = doc.css('#rgn_content4 tbody tr').map.with_index(1) {|tr, i|
cells = tr.css('td').map(&:text)
case cells.size
when 10
weapon, sun, mon, tue, wed, thu, fri, sun, ship, remarks = cells
when 9
prev = find_complete_row(doc, i)
weapon = prev.first
sun, mon, tue, wed, thu, fri, sun, ship, remarks = cells
when 8
prev = find_complete_row(doc, i)
weapon = prev.first
remarks = prev.last
sun, mon, tue, wed, thu, fri, sun, ship = cells
else
raise cells.size
end
wdays = [sun, mon, tue, wed, thu, fri, sun].map.with_index {|o_x, i|
o_x == '〇' ? true : false
}
{
weapon: weapon,
wdays: wdays,
ship: ship,
remarks: remarks
}
}
puts JSON.pretty_generate(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment