昨日今日カウンター
はじめに
トータル:![]()
昨日:![]()
今日:![]()
昨日、今日、トータルと表示できるカウンターです。
counter.cgi
dataフォルダに毎日のカウントファイルを置きます。
パラメータ "m" によって以下の処理をします。
- "t"
- 今日のファイルのカウントを表示します。
- "y"
- 昨日のファイルのカウントを表示します。
- [0-9]{8}
- 日付とみなし、その日のカウントを表示します。
- なし
- トータルと今日の分をカウントアップし、トータルのカウントを表示します。
#!/usr/bin/ruby -Ke
require "cgi"
require "gifcat"
def countup( filename )
c = 0
if File.exists?( filename )
File.open( filename, "r+" ) do |file|
file.flock( File::LOCK_EX )
c = file.read.chomp.to_i + 1
file.rewind
file.truncate( 0 )
file.write c.to_s
end
else
um = File.umask( 0060 )
File.open( filename, "w" ) do |file|
file.write "1"
end
File.umask( um )
c = 1
end
c
end
def count( filename = nil )
c = 0
if filename
begin
File.open( filename, "r" ) do |file|
c = file.read.chomp.to_i
end
rescue
end
else
c = countup( "counter.txt" )
countup( "data/#{Time.now.strftime( "%Y%m%d" )}.txt" )
end
c = c.to_s
c = "0" * ( 7 - c.size ) + c
c = "l" + c + "r"
gif = GIFCat.new
gif.read_files( *c.split( "" ).collect{ |n| "image/#{n}.gif" } )
gif.to_gif
end
cgi = CGI.new
print "Pragma: no-cache\n"
print "Cache-Control: no-cache\n"
print "Expires: Thu, 01 Dec 1994 16:00:00 GMT\n"
cgi.out( "image/gif" ) do
case cgi["m"].first
when "y"
count( "data/#{(Time.now - 24*60*60).strftime( "%Y%m%d" )}.txt" )
when "t"
count( "data/#{Time.now.strftime( "%Y%m%d" )}.txt" )
when /([0-9]{8})/
count( "data/#{$1}.txt" )
else
count()
end
end
require "cgi"
require "gifcat"
def countup( filename )
c = 0
if File.exists?( filename )
File.open( filename, "r+" ) do |file|
file.flock( File::LOCK_EX )
c = file.read.chomp.to_i + 1
file.rewind
file.truncate( 0 )
file.write c.to_s
end
else
um = File.umask( 0060 )
File.open( filename, "w" ) do |file|
file.write "1"
end
File.umask( um )
c = 1
end
c
end
def count( filename = nil )
c = 0
if filename
begin
File.open( filename, "r" ) do |file|
c = file.read.chomp.to_i
end
rescue
end
else
c = countup( "counter.txt" )
countup( "data/#{Time.now.strftime( "%Y%m%d" )}.txt" )
end
c = c.to_s
c = "0" * ( 7 - c.size ) + c
c = "l" + c + "r"
gif = GIFCat.new
gif.read_files( *c.split( "" ).collect{ |n| "image/#{n}.gif" } )
gif.to_gif
end
cgi = CGI.new
print "Pragma: no-cache\n"
print "Cache-Control: no-cache\n"
print "Expires: Thu, 01 Dec 1994 16:00:00 GMT\n"
cgi.out( "image/gif" ) do
case cgi["m"].first
when "y"
count( "data/#{(Time.now - 24*60*60).strftime( "%Y%m%d" )}.txt" )
when "t"
count( "data/#{Time.now.strftime( "%Y%m%d" )}.txt" )
when /([0-9]{8})/
count( "data/#{$1}.txt" )
else
count()
end
end