Twitter APIをRubyで使う

スポンサーリンク
スポンサーリンク
ライフスタイル関連のコンテンツ
お金 | 仕事 | 勉強 | プライベート | 健康 |
プログラミング関連のコンテンツ
C言語/C++入門 | Ruby入門 | Python入門 | プログラミング全般

API wrapper for Twitter and Twitter Search API’sをRubyで使う手順。
WindowsXP内に入っているRubyにインストールした。
おもむろに、gem install twitter とすると、nmakeがありませんといってこけました。
ので、gem install twitter –version “= 0.7.0″ とバージョン指定したらインストール成功。
nmakeエラーの時はバージョン指定インストールすればいいのか?(どこかの記事に書いてあったような気がするけど不確実です、要注意。)

スポンサーリンク

>ruby --version
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
 
>gem install twitter
Updating metadata for 1 gems from http://gems.rubyforge
.
complete
ERROR:  could not find twitter locally or in a reposito

gemのバージョンが古いのかも・・・と考え、gemをアップデート。

>gem -v
1.0.1
 
>gem update --system
・・・
・・・
RubyGems system software updated
 
>gem -v
1.3.6

もっかいtwitterライブラリをインストール。

>gem install twitter
When you HTTParty, you must party hard!
Building native extensions.  This could take a while...
ERROR:  Error installing twitter:
        ERROR: Failed to build gem native extension.
・・・
creating Makefile
 
nmake
'nmake' は、内部コマンドまたは外部コマンド、
操作可能なプログラムまたはバッチ ファイルとして認識され
 
・・・

nmakeないよーみたいなエラー。
nmakeをインストールする方法もあるようだけど面倒くさかったので、バージョン指定してインストール。

>gem install twitter --version "= 0.7.0"
When you HTTParty, you must party hard!
Successfully installed json_pure-1.4.3
Successfully installed rubyforge-2.0.4
Successfully installed rake-0.8.7
Successfully installed hoe-2.6.1
Successfully installed mash-0.0.3
Successfully installed httparty-0.4.3
Successfully installed twitter-0.7.0
7 gems installed
Installing ri documentation for json_pure-1.4.3...
Installing ri documentation for rubyforge-2.0.4...
Installing ri documentation for rake-0.8.7...
Installing ri documentation for hoe-2.6.1...
Installing ri documentation for mash-0.0.3...
Installing ri documentation for httparty-0.4.3...
Installing ri documentation for twitter-0.7.0...
Installing RDoc documentation for json_pure-1.4.3...
Installing RDoc documentation for rubyforge-2.0.4...
Installing RDoc documentation for rake-0.8.7...
Installing RDoc documentation for hoe-2.6.1...
Installing RDoc documentation for mash-0.0.3...
Installing RDoc documentation for httparty-0.4.3...
Installing RDoc documentation for twitter-0.7.0...

インストール成功したようなので、irbで確認と。

>irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'twitter'
=> true

続いて、簡単なコードを書いてみる。

の本のP44あたりのRubyコードを参考にして、フォロー/フォロワーを調べてみる。
フォローしている人のうち自分をフォローしてくれていない人の数と一覧を表示するコードです。

# フォローしている人のうち自分をフォローしてくれていない人の数と一覧を表示する
 
require 'rubygems'
require 'twitter'
 
auth = Twitter::HTTPAuth.new('USERNAME', 'PASSWORD');
base = Twitter::Base.new(auth)
 
guilty = base.friend_ids - base.follower_ids
puts "#{guilty.length} : People you follow who do not follow you"
 
printf("%-20s %-10s %-10s\n", "screen_name", "friends", "followers")
puts "-" * 50
guilty.each do |user_id|
  user = base.user(user_id)
  printf("%-20s %-10s %-10s\n", user.screen_name, user.friends_count, user.followers_count)
end

code9.rbとファイル名で保存しコンソールで実行した結果。

>ruby code9.rb
xx : People you follow who do not follow you
screen_name          friends    followers
--------------------------------------------------
hoge                 xxxxx      xxxxx
fuga                 xxxxx      xxxxx
foo                  xxxxx      xxxxx
bar                  xxxxx      xxxxx
baz                  xxxxx      xxxxx

簡単に確認できました。

ついでにPythonのTwitterAPIライブラリもインストールしておいた。
PythonはCygwin環境のしか入ってなかったので、CygwinのPythonに入れました。

http://code.google.com/p/python-twitter/downloads/detail?name=python-twitter-0.6.tar.gz

$ wget http://python-twitter.googlecode.com/files/python-twitter-0.6.tar.gz
$ tar -zxvf python-twitter-0.6.tar.gz
$ cd python-twitter-0.6
$ python setup.py install
 
$ python
>>> import twitter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/twitter.py", line 27, in <module>
    import simplejson
ImportError: No module named simplejson
>>> import simplejson
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named simplejson
 
$ wget http://pypi.python.org/packages/source/s/simplejson/simplejson-2.0.9.tar.gz
$ tar -xzvf simplejson-2.0.9.tar.gz
$ cd simplejson-2.0.9
$ python setup.py install
 
$ python
>>> import twitter
>>> import simplejson

Pythonのバージョンが2.5だったので、simplejsonを入れたらpython-twitterがちゃんと入った。
Python2.6だとsimplejson要らないらしい。
ま、たぶんRubyのしか使わないでしょうけど。

スポンサーリンク
 
スポンサーリンク