http://www.largevocalmix.jp/diary/2006/04/02
オフィシャルの生写真リリーススケジュールからBerryz工房と℃-uteを抜き出してRSSを作ってみた。
毎日午前9時と午後6時にhelloprojectphoto.rdfを出力して午後のRSSで確認する運用でしばらく様子をみませう。とりあえず全ての発売タイトルを補足できているのでHTMLの構造が変わらなければ大丈夫カモ?
● helloprojectphoto_generator.rb ------------------------------------------------------------ #!/home/username/opt/ruby/bin/ruby -Ks #------------------------------------------------------------ # ハロー!プロジェクトオフィシャルショップの生写真リリース # スケジュールからBerryz工房と℃-uteを抜き出してRSS1.0で出力する。 #------------------------------------------------------------ require 'net/http' require 'rss/1.0' require 'rss/content' require 'rss/maker' require 'uconv' require 'date' #------------------------------------------------------------ # 定数の定義 # ・定数は大文字で始まる #------------------------------------------------------------ Berryz = %w(工房 清水佐紀 熊井友理奈 須藤茉麻 菅谷梨沙子 夏焼雅 徳永千奈美 嗣永桃子) Cute = %w(℃-ute 梅田えりか 矢島舞美 村上愛 中島早貴 鈴木愛理 岡井千聖 萩原舞 有原栞菜) RssFilePath = '/home/username/www/meegle/helloprojectphoto.rdf' DayOfWeek = %w(日 月 火 水 木 金 土) #------------------------------------------------------------ # クラスの定義 # ・読み書き可能なインスタンス変数は attr_accessor で定義できる # ・Class.new で initialize が呼び出される # ・インスタンス変数は initialize で初期化する #------------------------------------------------------------ class Photo attr_accessor :release_date, :title, :type, :price, :comment def initialize(item) @release_date = /発売日:.+?タイトル/.match(item)[0].gsub(/発売日:|タイトル|\//, '') @title = /タイトル:.+?種別/.match(item)[0].gsub(/タイトル:|種別/, '') @type = /種別:.+?価格/.match(item)[0].gsub(/種別:|価格/, '') @price = /価格:.+?●/.match(item)[0].gsub(/価格:|●/, '') @comment = /●.+?コメント:/.match(item)[0].gsub(/コメント:/, '') @comment += /コメント:.+?$/.match(item)[0].gsub(/コメント:/, '') end def to_content_encoded parsed_date = Date.parse(@release_date) content_encoded = "<dt>発売日</dt><dd>#{parsed_date.year}年#{parsed_date.month}月#{parsed_date.day}日(#{DayOfWeek[parsed_date.wday]})</dd>" content_encoded += "<dt>タイトル</dt><dd>#{@title}</dd>" content_encoded += "<dt>種別</dt><dd>#{@type}</dd>" content_encoded += "<dt>価格</dt><dd>#{@price}</dd>" content_encoded += "<dt>コメント</dt><dd>#{@comment}</dd>" return "<dl>" + content_encoded + "</dl>" end def to_s output = '[発売日]' + @release_date output += '[タイトル]' + @title output += '[種別]' + @type output += '[価格]' + @price output += '[コメント]' + @comment return output end end #------------------------------------------------------------ # メソッドの定義 # ・戻り値は最後に評価した式になる # ・return で直接戻り値を返せる #------------------------------------------------------------ # Berryz工房と℃-uteだけ買う def favorite(item) Berryz.each do |name| if item.include?(name) then return true end end Cute.each do |name| if item.include?(name) then return true end end return false end #------------------------------------------------------------ # 処理の開始 #------------------------------------------------------------ # HTMLファイルを取得する url = URI.parse('http://www.helloproject.com/museum/photo_contents.html') req = Net::HTTP::Get.new(url.path) req['User-Agent'] = 'Meegle/1.0 (http://www.largevocalmix.jp/meegle/)' res = Net::HTTP.start(url.host, url.port) do |http| http.request(req) end # 発売タイトル毎に分ける photo_list = Array.new res.body.gsub(/<!--↓-->.+?<!--↑.-->/m) do |matched| trim_tag = matched.gsub(/<!--.+?-->/, '') trim_tag = trim_tag.gsub(/<.+?>/, '') photo = String.new trim_tag.each_line do |line| strip_line = line.strip if not strip_line.empty? then photo += strip_line end end photo = photo.gsub(/ /, '') if favorite(photo) then photo_list.push(Photo.new(photo)) end end sorted_photo_list = photo_list.sort_by{|a| a.release_date}.reverse # RSSを構築する rss = RSS::Maker.make("1.0") do |maker| maker.channel.about = 'http://www.largevocalmix.jp/meegle/helloprojectphoto.rdf' maker.channel.title = 'ハロー!プロジェクトオフィシャルショップ 生写真リリース' maker.channel.description = 'Berryz工房と℃-uteの生写真リリーススケジュールです' maker.channel.link = 'http://www.helloproject.com/museum/' anchor_counter = 1 sorted_photo_list.each do |photo| anchor = photo.release_date + '.' + anchor_counter.to_s.rjust(2, '0') anchor_counter += 1 item = maker.items.new_item item.link = 'http://www.helloproject.com/museum/photo_contents.html' + '#' + anchor item.title = photo.title item.content_encoded = photo.to_content_encoded end end # RSSをファイルに出力する File.open(RssFilePath, 'w') do |writer| writer.puts Uconv.sjistou8(rss.to_s) end File.chmod(0604, RssFilePath)