Created
March 15, 2014 13:06
-
-
Save ota42y/9566929 to your computer and use it in GitHub Desktop.
rubyでtwitterのツイートをmongodbに流し込む ref: http://qiita.com/ota42y/items/da31335243373d19a9a2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mongodb用 | |
gem install mongo bson_ext | |
# twitter用 | |
gem install tweetstream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
connection = Mongo::Connection.new | |
db = connection.db('twitter_database') #db名 | |
col = db.collection('twittr_table') #テーブル名 | |
col.insert({test: "test"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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