Wikiquoteを適当に1つ表示するスクリプト

Wikiquote.orgから、適当な格言・箴言・引用をランダムに引っ張ってきて表示するRubyスクリプトwikiquote」を書いた。

#!/usr/local/bin/ruby 
require 'open-uri' 
require 'hpricot'
 
def quote(year,date)
  q = []

  doc = Hpricot(open("http://en.wikiquote.org/wiki/#{date}"))
  doc.search("dl").each do |dl|
    dd = dl.search("dd")
    dd.search("ul").remove
    q << dd.inner_html.strip
  end

  puts q[year].gsub(/<[^>]+>/,"").gsub("~","--")
  puts

end
 
def fortune
  srand
  years = (0..4).to_a
  months = %w(January February March April May June July August September October November December)
  date = months[rand(12).to_i] + "_" + (rand(28).to_i + 1).to_s
  return years[rand(5).to_i], date
end

d = fortune
quote(*d)

HTMLをいじくるライブラリ「Hpricot」を使う練習になったし、なんというか、気の利いたフレーズがいっぱい表示されてうれしい。

$ wikiquote 
Don't start an argument with somebody who has a microphone when you
don't. They'll make you look like chopped liver. -- Harlan Ellison
(born 27 May 27 1934)

$ wikiquote 
You know, "power corrupts, and absolute power corrupts absolutely"?
It's the same with powerlessness. Absolute powerlessness corrupts
absolutely. Einstein said everything had changed since the atom was
split, except the way we think. We have to think anew. -- Studs Terkel
(recent death)

$ wikiquote 
Near the snow, near the sun, in the highest fields,
See how these names are feted in the waving grass
And by the streamers of the white cloud
And whispers of the wind in the listening sky.
The names of those who in their lives fought for life,
Who wore at their hearts the fire's centre.
Born of the sun, they travelled a short while toward the sun
And left the vivid air signed with their honour.
-- Stephen Spender --
(born 28 February 1909)

$ wikiquote 
Even if we accept, as the basic tenet of true democracy, that one
moron is equal to one genius, is it necessary to go a further step and
hold that two morons are better than one genius? -- Le&#243; Szil&#225;rd (born
11 February 1898)

$ wikiquote 
There is no slavery but ignorance. Liberty is the child of
intelligence. -- Robert G. Ingersoll

$ wikiquote 
Seize the moments of happiness, love and be loved! That is the only
reality in the world, all else is folly. It is the one thing we are
interested in here. -- Leo Tolstoy

$ wikiquote 
Love works magic.
It is the final purpose
Of the world story,
The Amen of the universe.
-- Novalis --

$ wikiquote 
A time is marked not so much by ideas that are argued about as by
ideas that are taken for granted. The character of an era hangs upon
what needs no defense. Power runs with ideas that only the crazy would
draw into doubt. The "taken for granted" is the test of sanity... In
these times, the hardest task for social or political activists is to
find a way to get people to wonder again about what we all believe is
true. The challenge is to sow doubt. -- Lawrence Lessig (born 3 June
1961)

これを毎日2度ぐらいポストするだけのTwitterのボットを作ってみたい。

あ、Ruby 1.9.1の標準のgemではHpricotは怪しいみたいで、GitHubのwhyさんのところから持ってくる必要があった。

sudo gem install why-hpricot --source http://gems.github.com