EOF
#▼ メインルーチン -------------------------------------------------------
def main
qu = ENV['QUERY_STRING'].chomp
if qu != nil && /^delete(\d{10})$/ =~ qu then
deldata($1)
end
x = CGI.new
contents = ""
if x["uri"] != nil && /^http/ =~ x["uri"] then
uri = x["uri"].sub(/[\s\r\n\t\f]$/,"")
contents = cont(uri,gettittle(uri))
else
contents = cont
end
html(contents)
end
#▼ ページの内容を取得 -------------------------------------------------------
def gethttp(uri="http://www.ec.hokudai.ac.jp/")
str = ""
port = 80
prrt = 443 if /^https/ =~ uri
uri = uri.sub(/^https?:\/\//,"").chomp
address,pat = uri.split(/\//,2)
pat = "/" + pat.to_s # .to_s をくっつけるのは、nil 対策
begin
str = Net::HTTP.get(address,pat,port)
rescue
html(ErrorMesse)
end
return str
end
#▼ ページのタイトルを取得 -------------------------------------------------------
def gettittle(uri)
str = gethttp(uri)
str = Kconv.toutf8(str)
title = ""
if /]*)?>[\r\n]*(.+)[\r\n]*<\/title>/im =~ str then
title = $2
end
if title.to_s == "" && /[\r\n]*(.+)[\r\n]*<\/h\d>/im =~ str then
title = $2
end
title = title.gsub(/<[^>]*>/,"").gsub(/,"").gsub(/[\r\n\f\t]/,"").gsub(/\s\s+/," ")
title = uri if /^[\s ]*$/ =~ title
return title
end
#▼ ページを出力 -------------------------------------------------------
def html(contents = "")
puts "Content-type: text/html;charset=UTF-8"
puts
puts File.read(TemplateHtml).sub(/\[\[ScriptURI\]\]/,ScriptURI).sub(/\[\[MAXDATA\]\]/,MaxData.to_s).sub(/\[\[MAIN\]\]/,contents)
exit 0
end
#▼ データ保存/呼び出し -------------------------------------------------------
def cont(newuri="",newtitle="")
wflag = false
db = DBM.new("links",DBmode)
unless newuri == "" || newuri == nil then
n = 1
for key in db["keylist"].to_s.split(/\t/).sort.reverse do
if db[key] == nil || /^#{ newuri }\t/ =~ db[key] || n > MaxData then
db.delete(key)
db["keylist"] = db["keylist"].to_s.sub(/#{ key }/,"").sub(/\t\t+/,"\t").sub(/^\t/,"")
end
n += 1
end
newkey = Time.now.to_i.to_s
db[ newkey ] = "#{ newuri }\t#{ newtitle }"
db["keylist"] = db["keylist"].to_s + newkey + "\t"
wflag = true
end
str = "\n"
for key in db["keylist"].to_s.split(/\t/).sort.reverse do
# next unless db[d]
uri,title = db[key].split(/\t/)
str += "\t
" + linkhtml(uri,title) + " "
unless false then
str += "(×) "
end
str += "#{ uri }" + "
\n"
end
db.close
mkrdf if wflag
return str + "\n"
end
#▼ データの削除 -------------------------------------------------------
def deldata(key)
return unless /^\d{10}$/ =~ key
db = DBM.new("links",DBmode)
unless /#{ key }/ =~ db["keylist"] then
db.close
return
end
uri, = db[key].split(/\t/)
db.delete(key)
db["keylist"] = db["keylist"].to_s.sub(/#{ key }/,"").sub(/\t\t+/,"\t").sub(/^\t/,"")
db.close
mkrdf
end
#▼ リンクhtml作成 -------------------------------------------------------
def linkhtml(uri="",title="")
if title == "" || title == nil then
title = uri.gsub(/<[^>]*>/,"").gsub(/,"").gsub(/[\r\n\f\t]/,"").gsub(/\s\s+/," ")
end
return "#{title}"
end
#▼ rdf ファイル生成 -------------------------------------------------------
def mkrdf
head = ""
body = ""
db1 = DBM.new("links",DBmode)
for key in db1["keylist"].to_s.split(/\t/).sort.reverse do
next if db1[key] == nil
head += " \n"
uri,title = db1[key].split(/\t/)
title = title.to_s.gsub(/</,"<").gsub(/>/,">").gsub(/ /," ").gsub(/&/,"&").gsub(/&(([A-Za-z]|#\d+)+);/){"&" + $1 + ";"}
body += " #{ title }#{ uri }#{ Time.at( key.to_i ).to_a[3..5].reverse.join("-") }\n"
end
db1.close
rss = File.read("./temp.rdf").sub(/\[\[SCRIPTURI\]\]/,ScriptURI)
rss = rss.sub(/\[\[HEAD\]\]/,head).sub(/\[\[BODY\]\]/,body)
f = File.open("./link.rdf","w")
f.print rss
f.close
end
#▼ プログラム実行 -------------------------------------------------------
main