public
Created

戦コレDB のデータを CSV に落とす。

  • Download Gist
senkoredb2csv.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 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 37 38 39 40 41 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
#!/usr/bin/env ruby
# coding: utf-8
 
require 'active_support/all'
require 'open-uri'
require 'nokogiri'
require 'cgi'
require 'ir_b'
 
def parse(no, body)
doc = Nokogiri::HTML(body)
 
text = doc.text
 
sub, name = doc.to_s.scan(/<\/span>(?:<br>)?(?:\[(.*)?\])?(.*?)<span/)[2]
return unless name
 
name = CGI.unescapeHTML(name)
 
type = doc.search('span')[1].text rescue nil
type_a = { '炎' => 'R', '水' => 'B', '風' => 'G' }[type]
num = text.scan(/No\.(\d+)/)[0][0].to_i
gender = text.scan(/♂|♀/)[0]
rare = text.scan(/S?(?:レア|ノーマル)\+?/)[0]
rare = rare.sub(/レア/,"R").sub(/ノーマル/,"N") unless rare.empty?
magic = text.scan(/魔力(\d+)/).join
atk, matk = text.scan(/攻撃力(\d+)⇒(\d*)/)[0]
deff, mdeff = text.scan(/防御力(\d+)⇒(\d*)/)[0]
skill = doc.at("div.skill span a").try(:text)
 
[no, type, num, gender, rare, sub, name, magic, atk, deff, matk, mdeff, skill, type_a]
end
 
def direct(no)
$stderr.puts "getting http://enedo.velvet.jp/senkore/sk_data.php?no=#{no}"
body = open("http://enedo.velvet.jp/senkore/sk_data.php?no=#{no}", 'r:cp932'){|f| f.read.encode("utf-8") }
parse(no, body)
end
 
 
def output_csv(from, to, out = $stdout)
to ||= from
(from..to).each_with_index do |no, idx|
sleep(rand(5) + 5) unless idx == 0
result = direct(no)
out.puts result.join(',') if result
end
end
 
def main(args)
case args.length
when 0
from = 1
to = 890
when 1
from = args[0].to_i
to = nil
else
from = args[0].to_i
to = args[1].to_i
end
 
from = 1 if from.zero?
to = from if to.nil? || to.zero?
 
output_csv(from, to)
end
 
main(ARGV)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.