使用 tweetstream 守护进程写入数据库
我正在尝试将与关键字匹配的所有推文写入我的数据库。我在 tracker.rb
中设置了以下内容:
require 'rubygems'
require 'tweetstream'
TweetStream::Daemon.new('Bill Gates','money','Twitter Tracker').track('ladygaga') do |status|
Tweet.new(:content => status.text)
end
但没有任何反应。我在这里做错了什么?
提前致谢
更新: 我将所有内容放入名为 twitter.rake
的 .rake
文件中,并使用 $ rake scrap
启动恶魔:
task :scrap => :environment do
desc "Run Twitter Scraper"
TweetStream::Client.new('TWITTER_USER','TWITTER_PASS').track('ladygaga') do |status|
Tweet.create(:user_id => status.user.id, :user_screen_name => status.user.screen_name, :user_profile_image_url => status.user.profile_image_url, :status_text => status.text, :status_id => status.id)
puts "[#{status.user.screen_name}] #{status.text}"
end
end
I am trying to write all tweets that matches a keyword to my database. I have set up the following in tracker.rb
:
require 'rubygems'
require 'tweetstream'
TweetStream::Daemon.new('Bill Gates','money','Twitter Tracker').track('ladygaga') do |status|
Tweet.new(:content => status.text)
end
But nothing happens. What am I doing wrong here?
Thanks in advance
Update:
I put everything in a .rake
file called twitter.rake
and start the demon with $ rake scrap
:
task :scrap => :environment do
desc "Run Twitter Scraper"
TweetStream::Client.new('TWITTER_USER','TWITTER_PASS').track('ladygaga') do |status|
Tweet.create(:user_id => status.user.id, :user_screen_name => status.user.screen_name, :user_profile_image_url => status.user.profile_image_url, :status_text => status.text, :status_id => status.id)
puts "[#{status.user.screen_name}] #{status.text}"
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你如何调用守护进程?
您需要提供一个命令(启动/停止..)
例如:
这将在后台启动作业
How are you calling the Daemon?
You need to supply a command (start/stop..)
For example:
This would start the job in the background
您的第一种方法是最好的方法,您需要从命令行运行“deamon”,但由于您想要使用rails和activerecord,因此您需要将rails环境引导到脚本中。
您需要执行以下操作:
要运行脚本,只需键入:
Your first approach was the best one, you need to run "deamon" from the command line, but since you want to user rails and the activerecord you need to bootstrap the rails environment in to the script.
You need to do something like this:
To run the script just type:
我假设这是一个更大的 Rails 应用程序的一部分。如果是这样,问题 1 是如果
Tweet.new
是标准的 activerecord 对象,则它不会将任何内容持久保存到数据库中。尝试Tweet.create
其次,我不确定脚本是否一定会知道推文(如果它是一个活动记录),而无需拉入 Rails 应用程序(可能通过包含environment.rb 文件)。类似的东西:
如果这不起作用,您可以尝试仅包含活动记录,这里有一个描述它的问题和答案:
如何在 Rails 之外的 ruby 脚本中使用 ActiveRecord?
I'm assuming this is part of a larger rails application. If so, issue 1 is that
Tweet.new
will not persist anything to the database if it is a standard activerecord object. TryTweet.create
Secondly I'm not sure if the script will necessarily know about the Tweet if its an activerecord without also pulling in the rails app, possibly by including the environment.rb file.Something like:
If that doesn't work you could try just including active record theres a question and answer here that describes it:
How to use ActiveRecord in a ruby script outside Rails?