三ヶ月と少しぶりに Termtter v2.2.2 を 9/5 にリリースした。
ターミナルから以下のコマンドを投入すればインストールされる。
1 | gem install termtter |
Termtter はターミナルベースの Twitter クライアントである。説明はウェブページやソースコード及びドキュメントを参照してほしい。
今回の目玉となる変更点は Fluentd プラグインを追加したことである。これにより Termtter で収集したすべてのツイートを Fluentd により構造化データとして扱うことができるようになる。
以前に Automatic Ruby と Fluentd を連携できるようにしたが、やっていることはそれと同じである。
Fluentd でツイートを収集するための設定はこんな感じになる。
1 2 3 4 5 6 | config.plugins.fluentd.host = localhost config.plugins.fluentd.port = 9999 config.plugins.fluentd.tag = "twitter.statuses" Termtter::Client.init do |t| t.plug 'fluentd' end |
Termtter と連携する Fluentd の設定は自由に書いて良い。たとえば以下はツイートを MongoDB に貯める例である。
1 2 3 4 5 6 7 8 9 10 11 12 | <source> type forward port 9999 </source> <match twitter.statuses> type mongo database twitter host localhost port 27017 tag_mapped flush_interval 1s </match> |
これでツイートが MongoDB に蓄積されるようになる。試しに MongoDB のシェルから確認してみよう。
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | > show dbs; twitter 0.015625GB > use twitter switched to db twitter > show collections; system.indexes twitter.statuses > db.twitter.statuses.findOne() { "_id" : ObjectId("522828fe1c2a362fed000001"), "created_at" : "Thu Sep 05 04:00:11 +0000 2013", "id" : NumberLong("375468376442679296"), "id_str" : "375468376442679296", "text" : "本屋さん行きまう", "source" : "<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>", "truncated" : false, "in_reply_to_status_id" : null, "in_reply_to_status_id_str" : null, "in_reply_to_user_id" : null, "in_reply_to_user_id_str" : null, "in_reply_to_screen_name" : null, "user" : { "id" : 6060612, "id_str" : "6060612", "name" : "すあま", "screen_name" : "su_ama", "location" : "窓際", "description" : "祭りなら、いま終わったよ。いま/可愛いアイコンは豊井さん(@1041uuu )作", "url" : "http://t.co/KlxnsXtUW6", "entities" : { "url" : { "urls" : [ { "url" : "http://t.co/KlxnsXtUW6", "expanded_url" : "http://miau0w0.blog.fc2.com/", "display_url" : "miau0w0.blog.fc2.com", "indices" : [ 0, 22 ] } ] }, "description" : { "urls" : [ ] } }, "protected" : false, "followers_count" : 188, "friends_count" : 236, "listed_count" : 12, "created_at" : "Tue May 15 11:57:09 +0000 2007", "favourites_count" : 10958, "utc_offset" : 32400, "time_zone" : "Tokyo", "geo_enabled" : true, "verified" : false, "statuses_count" : 45912, "lang" : "en", "contributors_enabled" : false, "is_translator" : false, "profile_background_color" : "A99761", "profile_background_image_url" : "http://a0.twimg.com/profile_background_images/774457615/46014c10fd014b8b2ad48cc7b7b874f6.gif", "profile_background_image_url_https" : "https://si0.twimg.com/profile_background_images/774457615/46014c10fd014b8b2ad48cc7b7b874f6.gif", "profile_background_tile" : true, "profile_image_url" : "http://a0.twimg.com/profile_images/378800000378314117/acebd1ce013d1531d84906739e020142_normal.jpeg", "profile_image_url_https" : "https://si0.twimg.com/profile_images/378800000378314117/acebd1ce013d1531d84906739e020142_normal.jpeg", "profile_banner_url" : "https://pbs.twimg.com/profile_banners/6060612/1376271960", "profile_link_color" : "D51821", "profile_sidebar_border_color" : "FFFFFF", "profile_sidebar_fill_color" : "452E2F", "profile_text_color" : "9B1A21", "profile_use_background_image" : true, "default_profile" : false, "default_profile_image" : false, "following" : true, "follow_request_sent" : null, "notifications" : null }, "geo" : null, "coordinates" : null, "place" : null, "contributors" : null, "retweet_count" : 0, "favorite_count" : 0, "entities" : { "hashtags" : [ ], "symbols" : [ ], "urls" : [ ], "user_mentions" : [ ] }, "favorited" : false, "retweeted" : false, "lang" : "ja", "time" : ISODate("2013-09-05T06:47:24Z") } |
きちんと JSON 形式でツイートが収集されていることがわかる。
他にも例えば fluent-plugin-webhdfs を利用して Hadoop にツイートを収集、あとは MapReduce で自在に分析といったこともできるようになる。それ以外にもツイートを Amazon S3 に収集しても大変便利である。