14
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.js + StreamingAPIで取得したTweetをMongoDBに流し込む

Posted at

前々からやってるTweet取得をMongoDBに流し込みます。
モジュールはMongooseを使います

##mongoose

Node.jsでMongoDBを使うときに使うモジュールです。MongoDB公式モジュールよりも人気があります。

Mongooseを使ってNode.jsとMongoDBの接続サンプル

とりあえずミニマムっぽいものを書いてみます。

$ npm install mongoose
mongoose.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/n0bisuke');

var Project = mongoose.model('Project', { name: String });

var project = new Project({ name: 'ligblog' });
project.save(function (err) {
  if (err) //失敗時
  console.log('保存成功');
});

QuickStartを参考にして
http://mongoosejs.com/docs/guide.html

mongoose.js
var DB_NAME = 'myapp';

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/'+DB_NAME);

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  console.log('connection success');
});

var Schema   = mongoose.Schema;

var TwitterSchema = new Schema({
	body:  {
		text: String,
		url: String,
		image: String,
		location: {
			lat: String,
			lon: String
		}
	},
	user: {
		twitter_id: String,
		name: String,
		screen_name: String,
		description: String,
		images: String,
		location: {
			lat: String,
			lon: String
		}
	},
	create_at: { type: Date, default: Date.now }
});

mongoose.model('TwModel', TwitterSchema);
var TwModel = mongoose.model('TwModel');
var twmodel = new TwModel();

twmodel.user.name = 'n0bisuke';
twmodel.save(function (err, data) {
  if (err) return console.error(err);
  console.log(data);
});

実行

$ node mongoose.js
connection success
{ __v: 0,
  _id: 547c5e89993ef9e1cd5f7c39,
  create_at: Mon Dec 01 2014 21:26:49 GMT+0900 (JST),
  user: { name: 'n0bisuke', location: {} },
  body: { location: {} } }

##TwitterStreamingAPIから流し込み

例によって、サンプルを試している感じなのでファイル分けとかは無しです。苦笑

mongoose.js
var DB_NAME = 'myapp';
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/'+DB_NAME);

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  console.log('connection success');
});

var Schema   = mongoose.Schema;

var TwitterSchema = new Schema({
	content:  {
		text: String,
		url: String,
		image: String,
		location: {
			lat: String,
			lon: String
		}
	},
	user: {
		twitter_id: String,
		name: String,
		screen_name: String,
		description: String,
		images: String,
		location: {
			lat: String,
			lon: String
		}
	},
	create_at: { type: Date, default: Date.now }
});

mongoose.model('TwModel', TwitterSchema);

/*twitter設定*/
var twitter = require('twitter');

var twit = new twitter({
  consumer_key: 'xx',
  consumer_secret: 'xx',
  access_token_key: 'xx',
  access_token_secret: 'xx'
});

/**/

var TwModel = mongoose.model('TwModel');
var keyword = process.argv[2]; //第一引数
var option = {'track': keyword};

console.log(keyword+'を含むツイートを取得します。');

twit.stream('statuses/filter', option, function(stream) {
	stream.on('data', function (data){
		if(data.lang === 'ja'){
			
			var twmodel = new TwModel();
			twmodel.content = {
				text: data.text
			};

			twmodel.user = {
				screen_name: data.user.screen_name
			};

			twmodel.save(function (err, data) {
			  if (err) return console.error(err);
			  console.log(data);
			});

		}
  });
});

同じように実行してみます。tweetを取得するたびにMongoDBに流し込んでくれます。

MongoShellで確認するとちゃんと保存されています。

14
12
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
14
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?