| 著作一覧 |
#!/usr/local/bin/ruby -Ks
require 'open-uri'
require 'cgi'
require 'iconv'
class GooDic
class NulLog
def write(s)
end
def close
end
def flush
end
end
class Entry
def initialize(title, desc)
@title = title
@desc = desc
end
attr_reader :title, :desc
end
def initialize
@log = $DEBUG ? File.open('log.txt', 'wb') : NulLog.new
@entries = {}
end
def add(word)
query = CGI::escape(word)
s = ''
items = []
open("http://dictionary.goo.ne.jp/search.php?&kind=all&MT=#{query}&PT=webtu&from=webtu",
'Referer' => "http://search.goo.ne.jp/web.jsp?MT=#{query}&STYLE=web&IE=UTF-8&from=gootop") do |h|
h.each_line do |line|
@log.write line
begin
s << Iconv.conv('sjis', 'euc-jp', line.strip)
rescue
s << line.strip
end
end
end
@log.flush
s.gsub(' ', ' ').scan(/<h2 class="ch04"><span>(.+?)<\/h2>.+?<div.+?>(.+?)<\/div>/) do |title, cont|
items << Entry.new(remove_tag(title), remove_tag(cont))
end
@entries[word] = items
end
def [](key)
@entries[key]
end
def size
@entries.size
end
def each(&proc)
@entries.each &proc
end
def remove_tag(s)
s.gsub('</li>', "\n").gsub(/\[<a.+>\]/, '').gsub /<.+?>/, ''
end
end
if __FILE__ == $0
if ARGV.length == 0
STDERR.puts 'usage: ruby dic.rb word [more words ...]'
exit 1
end
dic = GooDic.new
ARGV.each do |word|
dic.add word
end
dic.each do |k, c|
c.each do |e|
puts '----------------------------------------'
puts e.title
puts '----------------------------------------'
puts e.desc
end
end
end
コンソールが利用する文字コ−ドはシフトJISと前提(Windows用なので)。
ジェズイットを見習え |