Reputation: 53
I am very new to ruby and RoR. I would like to save incoming tweets that are currently being displayed on my terminal. Could you explain why the tweets are not being saved?
I run " ruby mine_tweets.rb" in the terminal and the the "puts" of the status.text appears, but there are no entries in the database.
require 'rubygems'
require 'tweetstream'
puts "searching for turkey....should write to database"
TweetStream.configure do |config|
config.consumer_key = 'XXXXXXX'
config.consumer_secret = 'XXXXXXX'
config.oauth_token = 'XXXXXXX'
config.oauth_token_secret = 'XXXXXXX'
config.auth_method = :oauth
end
# search for "turkey" and will print text and screen name and should store other values in db
TweetStream::Client.new.track('turkey') do |status|
puts status.text + " FROM: @" + status.user.screen_name
Tweet.create(:user_id => status.user.id, :tweet_text => status.text, :screen_name =>status.user.screen_name)
Tweet.save!
end
@client = TweetStream::Client.new
@client.on_delete do |status_id, user_id|
Tweet.delete(status_id)
end
class Tweet < ActiveRecord::Base
attr_accessible :screen_name, :tweet_text, :user_id
end
Upvotes: 0
Views: 771
Reputation: 21
I recently struggled through this myself. Here is what I found:
In order to save to a database, you need to :
Here is how i was able to save tweets from the Tweet Stream api into a sqlite local database. (I highly recommend switching directly to Postgres since Heroku doesn't support sqlite).
require 'sqlite3'
require 'tweetstream'
KEY = 'xxx'
SECRET = 'xxx'
TOKEN = 'xxx'
TOKEN_SECRET = 'xxx'
SEARCH_TERMS = "your search terms"
begin
db = SQLite3::Database.open "./db/tweets.db"
db.execute "CREATE TABLE IF NOT EXISTS store_tweets(
id, INTEGER PRIMARY KEY,
tweetid TEXT,
text TEXT,
screen_name TEXT,
userid TEXT,
user_name TEXT,
profile_image_url TEXT,
language,
created_at TEXT,
received_at TIMESTAMPS)"
TweetStream.configure do |config|
config.consumer_key = KEY
config.consumer_secret = SECRET
config.oauth_token = TOKEN
config.oauth_token_secret = TOKEN_SECRET
config.auth_method = :oauth
end
TweetStream::Client.new.track(SEARCH_TERMS) do |status|
puts "#{status.text}"
db.execute( "INSERT INTO store_tweets(
'tweetid',
'text',
'screen_name',
'userid',
'user_name',
'profile_image_url',
'language')
VALUES (?, ?, ?, ?, ?, ?, ?)",
[status[:id]],
[status.text],
[status.user.screen_name],
[status.user[:id]],
[status.user.name],
[status.user.profile_image_url],
[status.lang])
end
rescue SQLite3::Exception => e
puts "Exception occured"
puts e
ensure
db.close if db
end
Upvotes: 0