以前、Ruby で Twitter のタイムラインとかユーザ情報とか OAuth 認証とかについて書きましたが、今回はその追加記録です。
自分がフォローしているユーザや自分をフォローしてくれてるユーザの一覧(ユーザ名とか説明とか)はずっと取得して管理しているのですが、ツイート数やフォロー・フォロワー数を管理していなかったので、機能を追加してみました。
その時のテスト用スクリプトです。 OAuth 認証も不要な部分なので、非常に簡単です。
Ruby スクリプト
以下は非常に簡単なテスト用スクリプトです。 実際は、MySQL に登録したり、登録済みユーザと実際のユーザと突合処理をしたり等しているので、もっともっと複雑です。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -- coding: utf-8 --
require 'twitter'
class TwitterGetUser
begin
client = Twitter::Client.new
# テスト用ユーザID配列
# ( 数値形式のユーザID )
ary_user = [ 11111111, 22222222 ]
res = client.users( ary_user )
res.each do |userinfo|
puts "id : #{userinfo['id']}"
puts "name : #{userinfo['name']}"
puts "screen_name : #{userinfo['screen_name']}"
puts "location : #{userinfo['location']}"
puts "description : #{userinfo['description']}"
puts "url : #{userinfo['url']}"
puts "statuses_count : #{userinfo['statuses_count']}"
puts "friends_count : #{userinfo['friends_count']}"
puts "followers_count : #{userinfo['followers_count']}"
puts "created_at : #{userinfo['created_at']}"
puts ""
end
rescue => e
# エラーメッセージ
str_msg = "[EXCEPTION] " + e.to_s
STDERR.puts( str_msg )
exit 1
end
end
Ruby on Rails での閲覧画面
通常は、各種Rubyスクリプトをサーバでcron起動(1時間毎)して情報を取得しているのですが、Ruby on Rails で閲覧用に画面を作成しています。 以下は、ツイート数・フォロー数・フォロワー数部分。
参考
Twitter API を使用すれば、意外と何でもできます。 今回取得するようにしたツイート数に関して言うなら、「ツイート数が0件のアカウントにフォローされたら自動的にブロックするようにする」などの事が意外と簡単に実現できます。
以上。