require 'mechanize'

BASE_URL = 'http://pripara.jp/item/'
agent = Mechanize.new
category_maps = {
  "アクセサリー" => :HairsCorde,
  "ヘアアクセ" => :HairsCorde,
  "トップス" => :TopsCorde,
  "ボトムス" => :BottomsCorde,
  "スカート" => :BottomsCorde,
  "シューズ" => :ShoesCorde,
  "ワンピース" => :OnePieceCorde
}
by_category = {
  HairsCorde: [],
  TopsCorde: [],
  BottomsCorde: [],
  ShoesCorde: [],
  OnePieceCorde: [],
  OtherCorde: []
}

%w(
time1dan.html
kami6dan.html
kami5dan.html
kami4dan.html
kami3dan.html
kami2dan.html
kami1dan.html
dream_kami5dan.html
dream_kami4dan.html
dream_kami3dan.html
dream_kami2dan.html
dream_kami1dan.html
2016promotion.html
kami-charatomoticke.html
dream_dreamparade.html
dream2015_6th.html
dream2015_5th.html
dream2015_4th.html
dream2015_3rd.html
dream2015_2nd.html
dream2015_1st.html
dreamparade.html
2015_6th.html
2015_5th_01.html
2015_5th.html
dream201511_4th.html
2015_4th.html
2015_3rd.html
2015_2nd.html
2015_1st.html
memorial.html
2015limited_time.html
2015_encore.html
2015promotion.html
2015_3rd_03.html
2015_3rd_02.html
2015_3rd_01.html
2014_2nd_12.html
2014_2nd_11.html
2014_2nd.html
2014_1st.html
limited_time.html
3rd_encore.html
encore.html
2015_encore_02.html
promotion.html
cyalume.html
).each do |url|
  page = agent.get("#{BASE_URL}#{url}")
  page.search('div.categoryDetailList div.itemDateBlock').each do |corde|
    codename =  corde.search('h2 span').text.gsub('★','')
    name =  corde.search('h2').text.gsub('★','').gsub(codename, '')
    detail = corde.search('table td')
    brand = detail[2].search('img')
    brand = brand[0][:src].gsub('../img/item/icon_', '').gsub('.jpg', '') unless brand[0].nil?

    corde_data = {
      codename: codename,
      name: name,
      category: detail[0].text.to_s,
      mood: detail[1].text.to_s,
      brand: brand.to_s,
      rarity: detail[3].text.to_s,
      like: detail[4].text.to_s,
      color: detail[5].text.to_s
    }

    category = category_maps[corde_data[:category]]
    category = :OtherCorde if category.nil?
    by_category[category] << corde_data

  end

  sleep 1
end

source_code = ""
by_category.each_pair do |klass, contents|
  source_code += "@#{klass.to_s.downcase}s = []\n"
end
by_category.each_pair do |klass, contents|
  contents.each do |content|
    source_code += "@#{klass.to_s.downcase}s << #{klass.to_s}.new('#{content[:codename]}','#{content[:name]}','#{content[:rarity]}','#{content[:like]}','#{content[:color]}','#{content[:brand]}')\n"
  end
end
print source_code
