Skip to content

Instantly share code, notes, and snippets.

@ota42y
Created March 15, 2014 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ota42y/9566929 to your computer and use it in GitHub Desktop.
Save ota42y/9566929 to your computer and use it in GitHub Desktop.
rubyでtwitterのツイートをmongodbに流し込む ref: http://qiita.com/ota42y/items/da31335243373d19a9a2
# mongodb用
gem install mongo bson_ext
# twitter用
gem install tweetstream
TweetStream.configure do |tweet_config|
tweet_config.consumer_key = consumer_key
tweet_config.consumer_secret = consumer_secret
tweet_config.oauth_token = oauth_token
tweet_config.oauth_token_secret = oauth_token_secret
tweet_config.auth_method = :oauth
end
TweetStream::Client.new.sample do |status|
p status.text
end
connection = Mongo::Connection.new
db = connection.db('twitter_database') #db名
col = db.collection('twittr_table') #テーブル名
col.insert({test: "test"})
# -*- coding: utf-8 -*-
require 'yaml'
require 'tweetstream'
require 'mongo'
class TwitterWorker
def initialize(config)
TweetStream.configure do |tweet_config|
tweet_config.consumer_key = config["consumer_key"]
tweet_config.consumer_secret = config["consumer_secret"]
tweet_config.oauth_token = config["oauth_token"]
tweet_config.oauth_token_secret = config["oauth_token_secret"]
tweet_config.auth_method = :oauth
end
@client = TweetStream::Client.new
connection = Mongo::Connection.new
db = connection.db('twitter_jp')
@jp_col = db.collection('twitter_jp')
db = connection.db('twitter_nojp')
@nojp_col = db.collection('twitter_nojp')
end
def start
@client.sample do |status|
if /[ぁ-んァ-ヴ]+/u =~ status.text then
@jp_col.insert(status.to_h)
else
@nojp_col.insert(status.to_h)
end
end
end
end
worker = TwitterWorker.new(config = YAML.load_file("twitter.yml"))
worker.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment