macOSでTwitterStreamを使うために必要なこと
Twitterが提供するAPIには、RESTとstreamingの二種類があるらしい。このうち、rubyでstreaming APIを利用するには、TwitterStreamというgemが必要だ。
だが、TwitterStreamの依存gem(?)の中に曲者がいて、標準的にインストールしただけでは動かない。犯人を名指しで言うと、それはeventmachineである。
ここまで読んで対応策が分かった方は、以下は読む必要はない。
少し脱線するが、macOSはEl Capitan 10.11から、OpenSSLの開発向けファイルを削除した。AppleはOpenSSLから自身が開発したSecure Transportに移行したのだ。だが、macOSがBSD UNIXの流れをくむOSであることに変わりはなく、UNIXの資産を動かすことができることにも変わりはない。OpenSSLに依存するものを除けば…。
実は、eventmachineもOpenSSLに依存するのだ。だが、嫌らしいことにOpenSSLがなくてもgemとしてビルドは出来てしまう。(当然といえば当然だが)そして、次のスクリプトを動かすと
require 'tweetstream' require 'yaml' # Twitter設定情報の読み込み keys = YAML.load_file('./config.yml') # Consumer key, Secretの設定 CONSUMER_KEY = keys["api_key"] CONSUMER_SECRET = keys["api_secret"] # Access Token Key, Secretの設定 ACCESS_TOKEN_KEY = keys["access_token"] ACCESS_SECRET = keys["access_token_secret"] TweetStream.configure do |config| config.consumer_key = CONSUMER_KEY config.consumer_secret = CONSUMER_SECRET config.oauth_token = ACCESS_TOKEN_KEY config.oauth_token_secret = ACCESS_SECRET config.auth_method = :oauth end TweetStream::Client.new.sample do |status| puts "#{status.user.screen_name}: #{status.text}" end
次のエラーが出る。
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Encryption not available on this event-machine Abort trap: 6
そこでおもむろにOpenSSLをインストールすることになる。入れたのは1.02系の最新版(本稿執筆時点で1.02j)である。
./Configure darwin64-x86_64-cc --prefix=/usr/local/openssl shared; make depend; sudo make install
よし!これで行けるはずだ。
sudo gem uninstall eventmachine sudo gem install eventmachine
ruby twitterstream.rb #さっきのスクリプト名
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Encryption not available on this event-machine Abort trap: 6
orz
気を取り直して調べたところ、eventmachineのインストール時に次のようにしなければならないことが分かった。
sudo gem uninstall eventmachine sudo gem install eventmachine -- --with-cppflags=-I/usr/local/openssl/include
今度こそ!
ruby twitterstream.rb #さっきのスクリプト名
おおっ!(世界中のツイートがターミナルに溢れた)
以上。
参考サイト
TwitterStream/eventmachineでエラー
Crashes when request is received OS X 10.11 #8
この投稿へのトラックバック
トラックバックはありません。
- トラックバック URL
この投稿へのコメント