LoginSignup
53
54

More than 5 years have passed since last update.

Twitter Streaming API + Node.js (ntwitter)で指定キーワードのツイートを取得する

Last updated at Posted at 2014-11-25

利用モジュール

色々あるみたいだけど検索して最初に出てきたやつを使ってみました。

使うAPIのエンドポイント

これみたいですね -> POST statuses/filter https://dev.twitter.com/streaming/overview/request-parameters#track

リクエストパラメータはtrackを使います。

trackの説明に

Keywords to track. Phrases of keywords are specified by a comma-separated list. See track for more information.

とあったのでtrackがキーワード指定に使うパラメータみたいです。

他にもパラメータ色々ありますな

  • delimited
  • stall_warnings
  • filter_level
  • language
  • follow
  • track
  • locations
  • count
  • with
  • replies
  • stringify_friend_id

実装

LIGincという単語のツイートを取得します。

$ npm i ntwitter
index.js
var twitter = require('ntwitter');
var tw = new twitter({
  consumer_key: 'xx',
  consumer_secret: 'xx',
  access_token_key: 'xx',
  access_token_secret: 'xx'
});

tw.stream('statuses/filter', {'track':'LIGinc'}, function(stream) {
  stream.on('data', function (data) {
    console.log(data);
  });
});
$ node index

起動したら何も表示されないままです。
twitter上で指定した単語(今回はLIGinc)がつぶやかれるとコンソールにツイート情報が表示されます。

{ created_at: 'Tue Nov 25 09:01:48 +0000 2014',
  id: 537169268056416260,
  id_str: '537169268056416257',
  text: 'ligincにてまろ氏がxxしてます。 ()',
  source: '<a href="http://sites.google.com/site/yorufukurou/" rel="nofollow">YoruFukurou</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: 59417367,
     id_str: '59417367',
     name: 'のびすけ/S.Ryousuke',
     screen_name: 'n0bisuke',
     location: 'I\'m at 岩手県立大学',
     url: 'http://liginc.co.jp/life/useful-info/116563',
     description: 'のびすけです。LIG inc.\nhttp://qiita.com/n0bisuke',
.
.
.

53
54
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
54