#!/usr/pkg/bin/ruby require 'nkf' require 'pp' require 'pstore' require 'date' require "cgi" require './tagcloud' class Entry attr_accessor :year attr_accessor :title attr_accessor :cast attr_accessor :place attr_accessor :sheet attr_accessor :open attr_accessor :start def initialize @year = "" @title = "" @cast = "" @place = "" @sheet = "" @open = "" @start = "" end end class EntryList < Array def initialize end def add( entry ) self << entry end def entryeach( year ) self.each { |e| if year == nil || year == '' yield e else y = e.year.split( '/' )[ 0 ] if y == year yield e end end } end end class EventData def initialize( eventfile, cachefile ) @cachefile = cachefile @eventfile = eventfile @lastupdate = nil end def lastupdate @lastupdate end def entries @entries = nil db = PStore.new( @cachefile ) db.transaction do updated = db[ :updated ] eventfileupdated = File.mtime( @eventfile ) if updated && updated > eventfileupdated @entries = db[ :entries ] @lastupdate = updated else @entries = EntryList.new File.open( @eventfile ) { |file| file.each { |line| # l = NKF::nkf( '-s', line ) # puts l # prm = l.split( /,/, 7 ) prm = line.split( /,/, 7 ) entry = Entry.new entry.year = prm[ 0 ] || "" entry.title = prm[ 1 ] || "" entry.cast = prm[ 2 ] || "" entry.place = prm[ 3 ] || "" entry.sheet = prm[ 4 ] || "" entry.open = prm[ 5 ] || "" entry.start = prm[ 6 ] || "" @entries << entry } } db[ :updated ] = eventfileupdated db[ :entries ] = @entries @lastupdate = eventfileupdated end return @entries end end end ## of EventData class Summary def initialize( eventdata ) @eventdata = eventdata @entries = @eventdata.entries end def year( y ) contents = "" contents << header contents << getentries( y ) contents << footer contents end def header < EOS end def footer < EOS end def getentries( year ) contents = "" @entries.entryeach( year ) { |e| contents << "" contents << "\n" } return contents end end ## of Summary class Total def initialize( eventdata, cachefile ) @eventdata = eventdata @entries = @eventdata.entries @cachefile = cachefile @lastupdate = @eventdata.lastupdate end def lastupdate @lastupdate end def year( y ) yr = "" if y == nil || y == "" yr = "全部" else yr = "#{y}年" end contents = "" contents << header( yr ) contents << getentries( y ) contents << footer contents end def header( range ) <
日付 内容 出演者 場所 開場時刻 開演時刻
" contents << e.year contents << "" contents << e.title contents << "" contents << e.cast contents << "" contents << e.place contents << "" contents << e.sheet contents << "" contents << e.open contents << "" contents << e.start contents << "
EOS end def footer < EOS end def counting( year ) count = nil db = PStore.new( @cachefile ) db.transaction do db[ year ] ||= [] updated = db[ :updated ] eventupdated = @eventdata.lastupdate if updated && updated > eventupdated count = db[ year ] @lastupdate = updated else count = {} count.default = 0 @entries.entryeach( year ) { |e| casts = e.cast.split( / / ) casts.each { |c| count[ c ] += 1 } } db[ :updated ] = eventupdated db[ year ].push( count ) @lastupdate = eventupdated end end return count end def getentries( year ) count = counting( year ) ar = count.to_a ar.sort! { |a, b| b[1] <=> a[1] } contents = "" ar.each { |c| contents << "" contents << "\n" } return contents end end ## of Total class Tagcloud def initialize( eventdata, cachefile, total ) @eventdata = eventdata @entries = @eventdata.entries @cachefile = cachefile @total = total end def year( y ) yr = "" if y == nil || y == "" yr = "全部" else yr = "#{y}年" end contents = "" contents << header( yr ) contents << getentries( y ) contents << footer contents end def header( range ) < EOS end def footer "" end def getentries( year ) count = @total.counting( year ) tags = [] count.each { |cast, count| tags << Hash[ :tag => cast, :count => count ] } cloud = tagcloud( tags, year ) return cloud end def tagcloud( tags, year ) cloud = nil db = PStore.new( @cachefile ) db.transaction do db[ year ] ||= [] updated = db[ :updated ] totalupdated = @total.lastupdate if updated && updated > totalupdated cloud = db[ year ] @lastupdate = updated else cloud = TagCloud.new tags.each { |t| cloud.add( t[:tag], "http://www.google.co.jp/search?q=#{t[:tag]}", t[:count]) } db[ :updated ] = totalupdated db[ year ].push( cloud ) end end return cloud.html_and_css( 50 ) end end ## of Tagcloud class Page def initialize( cgi ) @cgi = cgi end def create cgi = CGI.new("html3") cgi.out() do cgi.html() do cgi.head{ cgi.title{"miwarin's event list"} } + cgi.body() do mode = cgi[ 'mode' ] year = cgi[ 'year' ] if mode == '' buildmenu else buildcontents( mode, year ) end end end end end ## create def buildmenu start = 1998 nowyear = Date.today.year summaries = "" summaries << "summay
" totals = "" totals << "total
" tags = "" tags << "tagcloud
" start.step( nowyear ) { |year| sum = "./index.rb?mode=summary&year=#{year}" summaries << "#{year}
" tl = "./index.rb?mode=total&year=#{year}" totals << "#{year}
" tc = "./index.rb?mode=tagcloud&year=#{year}" tags << "#{year}
" } sum = "./index.rb?mode=summary" summaries << "all
" tl = "./index.rb?mode=total" totals << "all
" tc = "./index.rb?mode=tagcloud" tags << "all
" menus = "" menus << "top
" menus << "
" menus << summaries menus << "
" menus << totals menus << "
" menus << tags menus << "
" return menus end ## of buildmenu def buildcontents( mode, year ) data = EventData.new( "./event.csv", "./event.db" ) builder = createbuilder( data, mode, year ) contents = builder.year( year ) return contents end ## of buildcontents def createbuilder( data, ope, year ) contentsbuilder = nil case ope when 'summary' contentsbuilder = Summary.new( data ) when 'total' contentsbuilder = Total.new( data, "./total.db" ) when 'tagcloud' contentsbuilder = Tagcloud.new( data, "./tag.db", Total.new( data, "./total.db" ) ) end return contentsbuilder end ## of createbuilder end ## of Page def main cgi = CGI.new page = Page.new( cgi ) page.create end main
出演者 回数
" contents << c[ 0 ] contents << "" contents << c[ 1 ].to_s contents << "