3 åã§åºæ¥ã Twitter BOT
BOT ã¨ã¯ Twitter ã®èªåãã¹ãããã°ã©ã ã®ãã¨ã§ãã主ã«ãã£ã©ã¯ã¿ã¼ãå人ã®åè¨éãå®æçã«ã¤ã¶ãããã®ãå¤ãããã§ããç§ã¯ãã¼ã¿ã¼ã»ãã©ãã«ã¼ã® BOT ããã©ãã¼ãã¦ããã®ã§ãããå¿ã«é¿ãåè¨ãã½ã¤ãã½ã¤ã表ãã¦ã¨ã¦ãé¢ç½ãã§ãã(^^)
ãµã¨ããã® BOT ã®ããã¿ã¯æ¥ã ã®è±åèªå¦ç¿ã«çãããã®ã§ã¯?ãã¨æãã以åããä½æãã¦ããè±åèªå¸³ã Twitter ã§å®æçã«ã¤ã¶ãã BOT ã¹ã¯ãªããã ruby ã§ä½æãã¦ã¿ã¾ãããUbuntu 9.04 + ruby 1.9 + twitter gem 0.6.15 ã®ç°å¢ã§åä½ç¢ºèªãã¦ãã¾ãã
â ã¤ã³ã¹ãã¼ã«(ruby, rubygems, twitter gem)
$ sudo apt-get install ruby1.9 rubygems1.9 $ sudo ln -s /usr/bin/ruby1.9 /usr/bin/ruby $ sudo gem install twitter $ gem list -d twitter *** LOCAL GEMS *** twitter (0.6.15) Author: John Nunemaker Rubyforge: http://rubyforge.org/projects/twitter Homepage: http://github.com/jnunemaker/twitter Installed at: /var/lib/gems/1.9.0 wrapper for the twitter api
$ cat kyagi_ew_bot.rb #!/usr/bin/ruby $LOAD_PATH.push("/usr/lib/ruby/1.9.0/xmlrpc/") require 'rubygems' require 'twitter' # è¾æ¸ãã¡ã¤ã« dict="/home/kyagi/prv/english/dict.org" # ãã¹ãããéé period=600 # ã¢ã«ã¦ã³ãåã¨ãã¹ã¯ã¼ã username='Twitter ã¢ã«ã¦ã³ã' password='Twitter ã¢ã«ã¦ã³ãã®ãã¹ã¯ã¼ã' httpauth = Twitter::HTTPAuth.new(username, password) client = Twitter::Base.new(httpauth) File.open(dict) do |file| # org-mode ã®è±åèªå¸³ã®ãã©ã¼ãããã«åããã¦ã¡ãã£ã¨ã ããã¼ã¹ãã while line = file.gets if (line =~ /\*/) header = line.split(/ /) message = header[1 .. -2] line = file.gets message.push(line) message.push(header[-1]) client.update(message.join.to_s) sleep period end end end $ ruby kyagi_ew_bot.rb
â åèªå¸³ãã¡ã¤ã«
$ head /home/kyagi/prv/english/dict.org * tied up :20090624: - She is so tied up with work that she can't go out for lunch. * skyscraper :20090624: - There are numerous skyscrapers along the high way. * pull together :20090621: - By pulling together, they were able to finish the work. * regime :20090621:
â åä½ç¢ºèª
kyagi_ew_botã¨ããæ°ãã BOTç¨ ã®ã¢ã«ã¦ã³ããä½æãã¦ãã® BOT ããã°ã©ã ããåèªå¸³ã®ã¨ã³ããªã 10 åééã§æµãããã«ãã¦ãã¾ããã¡ã¤ã³ã®ã¢ã«ã¦ã³ããããã®ã¢ã«ã¦ã³ãããã©ãã¼ããã°å®äºã§ããããã§èªåã§ä½æããè±åèªã Twitter ãéãã¦å¾©ç¿ã§ããããã«ãªãã¾ãããã¨ã¦ã便å©ã§æ°ã«å
¥ã£ã¦ãã¾ãã(^^)
ä¸ã®ä¾ã§ã¯ç§ã®è±åèªå¸³ã®ãã©ã¼ãããããã¼ã¹/å å·¥ãã¦åºåãã¦ãã¾ãããæ®éã«ãåè¨ãã¡ã¤ã«ãããä¸è¡ä¸è¨ãã§ä½æããã°ãã®ã¾ã¾ puts ããã° OK ã§ããIO.readlines ã§é åã«æ ¼ç´ããã㨠rand ãªã©ã§ã©ã³ãã ã«åºåãçæãã¦ãããã¨æãã¾ãã
â ãã®ä»
$LOAD_PATH ã追å ãã¦ããã®ã¯ base64.rb ã«ãã¹ãéãããã§ãã以ä¸ã®ã¨ã©ã¼ãåºãããã§ããããã©ã«ã㧠/usr/lib/ruby/1.9.0 é
ä¸ã«ã¯ãªã«ã¼ã·ãã§éã£ã¦ããããªã¨æãã¨éãã¿ããã§ãã
$ ruby kyagi_ew_bot.rb /var/lib/gems/1.9.0/gems/oauth-0.3.6/lib/oauth/helper.rb:2:in `require': no such file to load -- base64 (LoadError) (...snip...) $ ruby -e '$LOAD_PATH.each do |x| puts x; end' | grep '/usr/lib/' /usr/lib/ruby/vendor_ruby/1.9.0 /usr/lib/ruby/vendor_ruby/1.9.0/i486-linux /usr/lib/ruby/vendor_ruby /usr/lib/ruby/1.9.0 /usr/lib/ruby/1.9.0/i486-linux /usr/lib/ruby/1.9/i386-linux