ababup1192
4/16/2014 - 8:29 AM

minoru_last_live_day

#!/usr/bin/ruby
# -*- mode:ruby; coding:utf-8 -*-

require 'nokogiri'
require 'open-uri'
require 'date'

def getLastLiveDay

	# ミノルのアーカイブページ
	url = 'http://justinust.com/minoru/'

	charset = nil
	html = open(url) do |f|
	  charset = f.charset # 文字種別を取得
	  f.read # htmlを読み込んで変数htmlに渡す
	end

	# htmlをパース(解析)してオブジェクトを生成
	# アーカイブ候補
	arcive_candidacies = []
	arcives = []
	topic_ary = []

	doc = Nokogiri::HTML.parse(html, nil, charset)

	doc.xpath('//h3/a').each do|elm|
		hash = Hash.new()
		hash["topic"] = elm.parent.parent.parent.xpath('table[@class="chat"]//tr')
		hash["date"] = elm.text
		arcive_candidacies.push(hash)
	end

	(arcive_candidacies[0])["topic"].each do|elm|
		# トピック収集
		topic = elm.xpath('td[2]').text
		if !topic.empty?
			topic_ary.push(topic)
		end
	end

	arcive_candidacies.each do |arc|
		# 正規表現でアーカイブの日取得
		date = arc["date"]
		if date =~ /(\d+.\d+.\d+)\s.+アーカイブ/
			arcives.push($1)
		end
	end
	last_date_text = arcives.first.split("/")
	last_date = Date::new(last_date_text[0].to_i, last_date_text[1].to_i, last_date_text[2].to_i)
	today = Date::today
	diff = (today - last_date).to_i
	topics = topic_ary.join(",")
	if diff == 0
		"最終ミノ配日 #{arcives.first} 本日配信がありました。配信時Topic「#{topics}」"
	else
		"最終ミノ配日 #{arcives.first} #{diff}日間配信がありません。配信時Topic「#{topics}」"
	end
end

puts getLastLiveDay