twitterのuserstream/site streamsをRubyで読むためのメモ

まだ作れないけどメモを。

余談

だらだら流れるstreamをどうやってうけるのか、と思ったけど、net/httpsで素直にgetするのね。特殊な技を使うわけじゃないんだなー。

追記

tweetstream gemをインストールしました。

$ gem install tweetstream
Fetching: daemons-1.1.9.gem (100%)
Fetching: eventmachine-1.0.3.gem (100%)
Building native extensions.  This could take a while...
Fetching: http_parser.rb-0.5.3.gem (100%)
Building native extensions.  This could take a while...
Fetching: em-twitter-0.2.2.gem (100%)
Fetching: addressable-2.3.5.gem (100%)
Fetching: em-socksify-0.3.0.gem (100%)
Fetching: cookiejar-0.3.0.gem (100%)
Fetching: em-http-request-1.0.3.gem (100%)
Fetching: yajl-ruby-1.1.0.gem (100%)
Building native extensions.  This could take a while...
Fetching: tweetstream-2.5.0.gem (100%)
Successfully installed daemons-1.1.9
Successfully installed eventmachine-1.0.3
Successfully installed http_parser.rb-0.5.3
Successfully installed em-twitter-0.2.2
Successfully installed addressable-2.3.5
Successfully installed em-socksify-0.3.0
Successfully installed cookiejar-0.3.0
Successfully installed em-http-request-1.0.3
Successfully installed yajl-ruby-1.1.0
Successfully installed tweetstream-2.5.0
10 gems installed

どうやらEventMachineというgemにかなり依存しているようですね。
資料としてのrubydocをリストしておきます。

あとは検索ストリームが

で、全ストリームの一部切り出しが

だということだそうで。
firehoseは一般利用不可。

TwitterJSONの中身

大事なのはtext。ツイートそのもの。

主にuserのなかに情報が入っている。

例として私のテストツイート

の中身を。

{"created_at"=>"Fri Sep 20 15:39:33 +0000 2013",
 "id"=>381080197652426752,
 "id_str"=>"381080197652426752",
 "text"=>"@riocampos てすと。",
 "source"=>
  "<a href=\"http://tapbots.com/tweetbot\" rel=\"nofollow\">Tweetbot for iOS</a>",
 "truncated"=>false,
 "in_reply_to_status_id"=>nil,
 "in_reply_to_status_id_str"=>nil,
 "in_reply_to_user_id"=>56997313,
 "in_reply_to_user_id_str"=>"56997313",
 "in_reply_to_screen_name"=>"riocampos",
 "user"=>
  {"id"=>56997313,
   "id_str"=>"56997313",
   "name"=>"riocampos(りおかんぽす)",
   "screen_name"=>"riocampos",
   "location"=>"Osaka, Japan",
   "url"=>"http://about.me/riocampos",
   "description"=>
    "plz call me rio☆ favorite: cycle road race & figure skate, Mac!, iPhone!, Ruby, Fine-Art, Science, Black/Green Tea. | Gender:♂ facebook: http://on.fb.me/MvEV7P",
   "protected"=>false,
   "followers_count"=>2062,
   "friends_count"=>2258,
   "listed_count"=>191,
   "created_at"=>"Wed Jul 15 12:02:35 +0000 2009",
   "favourites_count"=>2768,
   "utc_offset"=>32400,
   "time_zone"=>"Osaka",
   "geo_enabled"=>true,
   "verified"=>false,
   "statuses_count"=>144260,
   "lang"=>"en",
   "contributors_enabled"=>false,
   "is_translator"=>false,
   "profile_background_color"=>"48E0B7",
   "profile_background_image_url"=>
    "http://a0.twimg.com/profile_background_images/378800000070585874/5e79429ca226e91dd629584d0278d58a.jpeg",
   "profile_background_image_url_https"=>
    "https://si0.twimg.com/profile_background_images/378800000070585874/5e79429ca226e91dd629584d0278d58a.jpeg",
   "profile_background_tile"=>true,
   "profile_image_url"=>
    "http://a0.twimg.com/profile_images/3462273402/f331afb1b57c7d39688229e86de95881_normal.jpeg",
   "profile_image_url_https"=>
    "https://si0.twimg.com/profile_images/3462273402/f331afb1b57c7d39688229e86de95881_normal.jpeg",
   "profile_banner_url"=>
    "https://pbs.twimg.com/profile_banners/56997313/1365179721",
   "profile_link_color"=>"009999",
   "profile_sidebar_border_color"=>"FFFFFF",
   "profile_sidebar_fill_color"=>"EFEFEF",
   "profile_text_color"=>"333333",
   "profile_use_background_image"=>true,
   "default_profile"=>false,
   "default_profile_image"=>false,
   "following"=>nil,
   "follow_request_sent"=>nil,
   "notifications"=>nil},
 "geo"=>nil,
 "coordinates"=>nil,
 "place"=>nil,
 "contributors"=>nil,
 "retweet_count"=>0,
 "favorite_count"=>0,
 "entities"=>
  {"hashtags"=>[],
   "symbols"=>[],
   "urls"=>[],
   "user_mentions"=>
    [{"screen_name"=>"riocampos",
      "name"=>"riocampos(りおかんぽす)",
      "id"=>56997313,
      "id_str"=>"56997313",
      "indices"=>[0, 10]}]},
 "favorited"=>false,
 "retweeted"=>false,
 "filter_level"=>"medium",
 "lang"=>"ja"}