Tech Tips

  1. プログラミング
  2. 360 view

[Ruby]英語のツイートを Twitter Streaming API から取得してmongoDBに入れてみる

Fluentdのtwitterプラグインが動かないので、
rubyでtwitter streaming APIを使ってmongoDBにインサートするプログラムを書いてみた。
日本語のツイートはエンコードでエラーが出てうまく取得できなかったので、英語のツイートを取得してみる。
# -*- coding: utf-8 -*-

require 'tweetstream'
require 'mongo'

# Connect Mongo
connection = Mongo::Connection.new('localhost')
db = connection.db('tweets')
collection = db.collection('timeline_log')

# Twitter Streaming API Setting
TweetStream.configure do |config|
  config.consumer_key       = '***'
  config.consumer_secret    = '***'
  config.oauth_token          = '***'
  config.oauth_token_secret = '***'
  config.auth_method         = :oauth
end

# Insert Japanease tweets to mongodb
client = TweetStream::Client.new
client.sample do |status|
  if status.user.lang == 'en' && status.user.followers_count >;= 1000
    name = status.user.name
    text = status.text

    r = /^[a-zA-Z0-9 ,.]*$/
    if r =~ text && r =~ name
      doc = {'name' => name, 'tweet' => text, 'time' => Time.now}
      id = collection.insert(doc)
    end
  end
end

記号もエンコードの関係でエラーがでたから、とりあえず英数字のみ許可してみた。
mongoDBにはこんな感じで入る。
{ "_id" : ObjectId("528d9aa7b7178f4c26088e74"), "name" : "ayy lmao", "tweet" : "IM SO TIRED OF THIS BULLSHIT ASS HISTORY CLASS UGHHHHHH", "time" : ISODate("2013-11-21T05:31:19.765Z") }
{ "_id" : ObjectId("528d9ab6b7178f4c26088e75"), "name" : "4pound", "tweet" : "I have this IDGAFness about me", "time" : ISODate("2013-11-21T05:31:34.740Z") }
{ "_id" : ObjectId("528d9ab6b7178f4c26088e76"), "name" : "khaki ramirez", "tweet" : "so please", "time" : ISODate("2013-11-21T05:31:34.741Z") }
{ "_id" : ObjectId("528d9ab6b7178f4c26088e77"), "name" : "", "tweet" : "RT @Dashokayyo: RT\"@MrSmoothNerd: I don't spit game at the ladies.\n\nI'm just myself with the ladies.\n\nAnd that's all the game\na real nigga …", "time" : ISODate("2013-11-21T05:31:34.757Z") }
{ "_id" : ObjectId("528d9ab6b7178f4c26088e78"), "name" : "ReinDeiR", "tweet" : "oH OHH\nOH \nOoOOOHH \nTHE SHOW WAS CALLED \"BETWEEN THE LIONS\"\nBC YOU /READ BETWEEEN THE LINES/\nOH", "time" : ISODate("2013-11-21T05:31:34.793Z") }
{ "_id" : ObjectId("528d9ac7b7178f4c26088e79"), "name" : "Cutter Smith", "tweet" : "turn down for what", "time" : ISODate("2013-11-21T05:31:51.722Z") }
{ "_id" : ObjectId("528d9ac8b7178f4c26088e7a"), "name" : "Lil Haiti ", "tweet" : "Dream big", "time" : ISODate("2013-11-21T05:31:52.728Z") }
{ "_id" : ObjectId("528d9acdb7178f4c26088e7b"), "name" : "Do It For Me", "tweet" : "i laugh a lot but i didn mean to hurt ya feelins.", "time" : ISODate("2013-11-21T05:31:57.751Z") }
  【1000円以上送料無料】Twitter APIリファレンスガイドブック Twitterアプリを開発する/池田成樹【RCP】
価格:3,150円(税込、送料込)

プログラミングの最近記事

  1. PlatformIO IDE for VSCode を使用して VSCode で Ardu…

  2. ROS Docker イメージで発生した GPG error の解消方法

  3. Streamlit で訪れた国を色づけした世界地図を作成できるアプリケーションを作成してみ…

  4. M5Stack Core2 for AWS – ESP32 IoT開発キットで…

  5. D3.js v7 で点・線・テキスト・ツールチップ・ズームを設定する方法

関連記事

PAGE TOP