技術ネタを書けるような人間になりたいという心情を詠んだ句

Twitter Userstream

Twitterクライアント作ってみたくなった。
数分ごとにTL取得するのもいいけど、やっぱUserstream使いたいっしょ。

というわけでgemを探す。

いろいろあるみたいだけど、twitter-streamに決定(100%気分)
というかgem探す度に思うけどみんな名前が似通っててアレですよね。
tweetstreamとかtwitter-userstreamとか。

それはさておきインストール。

$ sudo gem install twitter-stream -s http://gemcutter.org

はい、エラー。g++がありませんとのこと。てことで、

$ sudo apt-get install g++

もいちどgemを叩く。

$ sudo gem install twitter-stream -s http://gemcutter.org
(中略)
Successfully installed eventmachine-1.0.0
Successfully installed simple_oauth-0.1.9
Successfully installed http_parser.rb-0.5.3
Successfully installed twitter-stream-0.1.16
4 gems installed

よしよしOKですね。
ライブラリのソース見たりググったりしてこんなコードになりました。(こちらのページが無かったら死んでた。果てしない感謝。)

#! /usr/bin/ruby
#-*- coding: utf-8 -*-

# twitter_jsonstream.rb

require 'twitter/json_stream'
require 'json'

KEY   = "*****"
SEC   = "*****"
TOKEN = "*****"
TSEC  = "*****"

EventMachine::run do 
  conf = Hash.new
  conf[:host] = "userstream.twitter.com"
  conf[:path] = "/1.1/user.json"

  conf[:oauth] = Hash.new
  conf[:oauth][:consumer_key]    = KEY
  conf[:oauth][:consumer_secret] = SEC
  conf[:oauth][:access_key]      = TOKEN
  conf[:oauth][:access_secret]   = TSEC

  stream = Twitter::JSONStream.connect(conf)
  
  stream.each_item do |item|
    puts JSON.parse(item)["text"]
  end
end

えいっ

$ ruby ./twitter_jsonstream.rb
terminate called after throwing an instance of 'std::runtime_error'
  what():  Encryption not available on this event-machine
中止 (コアダンプ)

ヾ(´゚Д゚`;)ゝ エーット・・・

なんかよくわからんエラーでた。
どうやらさっき一緒にインストールされたeventmachine様がご機嫌ナナメのようですね。

よかろう。再インストールしてやる。

$ sudo gem uninstall eventmachine
$ sudo gem install eventmachine

はい。これで上手くいきました。
キチガイなTLが流れてきます。(オイ
話によると、接続を保つためにたまーに空行が送られてくるそうな。
そのへんは後で考慮しましょう。

はてなのシンタックスハイライト便利ですね~。