2004年07月11日
2004年07月11日
referer_log解析
#!/usr/bin/ruby
def unescape(str)
str.gsub(/\+/, ' ').gsub(/%([0-9a-fA-F]{2})/){ [$1.hex].pack("c") }
end
def check_keyword(q,url)
a,b = url.split(/\?/,2)
return "" if b==nil
url = b
url.split(/[&;]/).each { |x|
key, val = x.split(/=/,2).collect{|x|unescape(x)}
if key == q then return val else next end
}
return ""
end
SEARCHS = [ [/\.google\./,"q"] ,[/\.google\./,"as_q"],[/\.google\./,"as_epq"] ,[/yahoo\.co\.jp/,"q"] ,[/infoseek\.co\.jp/,"qt"] ,[/excite\.co\.jp\/search/,"search"] ,[/nifty\.com\/cgi-bin\/search/,"search"],[/nifty\.com\/cgi-bin\/search/,"Text"],[/search\.goo\.ne\.jp/,"MT"],[/search\.yahoo\./,"p"],[/search\?/,"q"],[/search.naver\?/,"query"],[/search\.biglobe\./,"q"],[/search\.msn/,"q"],[/search\.lycos\./,"qt"],[/search\.aol/,"query"],[/\.excite\.co\.jp\/search/,"s"] ]
def getsearch()
return searchs
end
class My_url
def initialize()
@referer = Hash.new(0)
@search = []
end
def add_search(url,keyword)
@search.push([keyword,url])
end
def add_referer(url)
@referer[url]+=1
end
def printhtml()
@referer.each do |k,d|
print "#{d}:<a href='#{k}'>#{k}</a><br/>\n"
end
print "searches:\n"
@search.each do |a|
print "<a href='#{a[1]}'>#{a[0]}</a>\n"
end
end
def add(url)
for x in SEARCHS
next unless url =~ x[0]
p = check_keyword(x[1],url)
if p!=""
add_search(url,p)
return
end
end
add_referer(url)
end
end
sitetable = Hash.new
before = ""
while fn = ARGV.shift
file = open(fn,"r")
while line=file.gets()
from,to = line.split(/-> /,2)
if /^http/ =~ from then before=from else from=before end
before = "" unless to =~ /mizuy/
next if before==""
next if from =~ /triaez\.kaisei\.org\/~mizuy/
if sitetable[to]==nil
sitetable[to]=My_url.new
end
sitetable[to].add(from)
end
end
print "<?xml version='1.0' encoding='UTF-8'?>\n"
print "<html><body>\n"
print "<h2>referer</h2>\n"
for key in sitetable.keys.sort!
print "<h4><a href='http://triaez.kaisei.org#{key}'>#{key}</a></h4>\n"
sitetable[key].printhtml()
end
print "</body></html>\n\n"
- 入力はreferer_log(LogFormat "%{Referer}i -> %U")
- 自サイトのページひとつごとに、どこから飛んできたか表示
- サーチエンジンっぽかったらキーワードを表示。とりあえずUTF-8のみ対応
昔の日記システムのデータとか消しまくってるから、リンク切れでくるのが多数。もうしわけない。 約1年分くわせたらやたら重い。生成時間1m,ファイルサイズ2M
サーチエンジンごとに文字コード指定してキーワード文字化けしないようにしないと。
scala
Tutorialしか読んでない時点でのまとめ
- コンパイラはjava classファイルを掃きだす。Javaのライブラリはすべて使える。
- 数や関数もオブジェクト
- 無名関数あり
- 型推論あり
- オペレータオーバーロードあり
- Genericsあり
- Haskellみたいな(?)パターンマッチングあり(配列とかcase class)
- XMLのための記法あり
オブジェクト指向かつ、関数プログラミングっぽい記法もできる。 javaのライブラリを使いたいけどjava言語は嫌いって場合にはいいツールかも。
試験
自転車
最近帰り道に給油ならぬ給気をしないと動かなかった自転車、どうやら空気をいれる部分のゴムチューブが摩耗していた模様。 買って1ヶ月なのに。 というわけで、ダ○エー自由○丘店の自転車屋で買う時は注意が必要です。
とりかえたから明日からは大丈夫だろう。
dt2dxml.rb
hnsっぽい入力フォーマットでこの日記のxmlを記述する。
$KCODE = "u"
class Diary
CDATAELEM = ["source","quote"]
def initialize(outfile)
@stack = []
@file = outfile
end
def printheader()
@file.print "<?xml version='1.0' encoding='UTF-8'?>\n"
end
def mode()
@stack[@stack.length-1]
end
def in(element)
@stack.push(element)
if CDATAELEM.index(element)==nil
@file.print "<#{element}>\n"
else
@file.print "<#{element}><![CDATA[\n"
end
end
def out()
elem = @stack.pop
if CDATAELEM.index(elem)==nil
@file.print "</#{elem}>\n"
else
@file.print "]dummy]></#{elem}>\n" # <- dummyとる
end
end
def outwhile(element)
while mode()!=element
out()
end
end
def ischildof(elem)
return @stack.index(elem)!=nil
end
def exit()
while @stack.length>0
out()
end
end
def printe(line)
@file.print(line)
end
end
filename = ARGV[0]
/diary(\d\d\d\d)(\d\d)(\d\d)\.dt$/ =~ filename
outfn = ARGV[1]
out = open(outfn,"w")
print "from #{filename} to #{outfn}\n"
diary = Diary.new(out)
diary.printheader()
diary.in("diary-individual")
diary.in("diary")
out.print <<HERE
<date><year>#{$1}</year><month>#{$2}</month><day>#{$3}</day></date>
HERE
fin = open(filename,"r")
endtarget = ""
while line=fin.gets()
print line
if /^TOPIC\s+LINK\s+(\S+)\s+(.+)$/ =~ line
diary.outwhile("diary")
diary.in("topic")
diary.printe("<title><link href='#{$1}'>#{$2}</link></title>")
diary.in("body")
elsif /^TOPIC\s+LINK\s+(\S+)\s*$/ =~ line
diary.outwhile("diary")
diary.in("topic")
diary.printe("<title><link href='#{$1}'>#{$1}</link></title>")
diary.in("body")
elsif /^TOPIC\s+(.+)$/ =~ line
diary.outwhile("diary")
diary.in("topic")
diary.printe("<title>#{$1}</title>")
diary.in("body")
elsif !diary.ischildof("body")
next
else
if (diary.mode=="ol" || diary.mode=="ul" || diary.mode=="source" || diary.mode=="quote") && Regexp.new("^#{endtarget}$") =~ line
diary.outwhile("body")
elsif /^SOURCE\s*<<(\w+)/ =~ line
endtarget = $1
diary.outwhile("body")
diary.in("source")
elsif /^QUOTE\s*<<(\w+)/ =~ line
endtarget = $1
diary.outwhile("body")
diary.in("quote")
elsif /^OL\s*<<(\w+)/ =~ line
endtarget = $1
diary.outwhile("body")
diary.in("ol")
elsif /^UL\s*<<(\w+)/ =~ line
endtarget = $1
diary.outwhile("body")
diary.in("ul")
elsif /^LINK\s+(\S+)\s+(.+)$/ =~ line
out.print "<link href='#{$1}'>#{$2}</link>"
elsif /^LINK\s+(\S+)\s*$/ =~ line
out.print "<link href='#{$1}'>[#{$1}]</link>"
elsif /^$/ =~ line
diary.outwhile("body") if diary.mode()=="p"
else
case diary.mode()
when "body"
diary.in("p")
diary.printe(line)
when "source","quote"
diary.printe(line)
when "ol","ul"
diary.printe(" <li>#{line}</li>\n")
when "p"
diary.printe(line)
else
print "error"
end
end
end
end
diary.exit()
今日はよくコードを書いた。...