使用 tweetstream 守护进程写入数据库

发布于 2024-11-08 14:35:36 字数 901 浏览 0 评论 0原文

我正在尝试将与关键字匹配的所有推文写入我的数据库。我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装纯掩盖桑 2024-11-15 14:35:36

你如何调用守护进程?

您需要提供一个命令(启动/停止..)

例如:

rails runner "TweetStream::Daemon.new('tracker').track('ladygaga') { |status| do_something }" start

这将在后台启动作业

How are you calling the Daemon?

You need to supply a command (start/stop..)

For example:

rails runner "TweetStream::Daemon.new('tracker').track('ladygaga') { |status| do_something }" start

This would start the job in the background

你好,陌生人 2024-11-15 14:35:36

您的第一种方法是最好的方法,您需要从命令行运行“deamon”,但由于您想要使用rails和activerecord,因此您需要将rails环境引导到脚本中。

您需要执行以下操作:

#!/usr/bin/env ruby
# encoding: utf-8

ENV["RAILS_ENV"] ||= "development"

root  = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require File.join(root, "config", "environment")

require 'tweetstream'

p "Initializing daemon..."

TweetStream.configure do |config|
  config.consumer_key       = 'your-consumer_key'
  config.consumer_secret    = 'your-consumer_secret'
  config.oauth_token        = 'your-oauth_token'
  config.oauth_token_secret = 'your-oauth_token_secret'
  config.auth_method        = :oauth
end

terms = ['ladygaga']

daemon = TweetStream::Daemon.new('tracker',
  :log_output => true,
  :backtrace  => true,
)

daemon.on_inited do
  ActiveRecord::Base.connection.reconnect!
  p "Listening..."
end

daemon.on_error do |message|
  puts "on_error: #{message}"
end

daemon.on_reconnect do |timeout, retries|
  puts "on_reconnect: #{timeout}, #{retries}"
end

daemon.on_limit do |discarded_count|
  puts "on_limit: #{skip_count}"
end

daemon.track(terms) do |status|
  # put here your model.create code!
  # Tweet.create!( :uid => status.id, ... )
end

要运行脚本,只需键入:

ruby scrip-name.rb run

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:

#!/usr/bin/env ruby
# encoding: utf-8

ENV["RAILS_ENV"] ||= "development"

root  = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require File.join(root, "config", "environment")

require 'tweetstream'

p "Initializing daemon..."

TweetStream.configure do |config|
  config.consumer_key       = 'your-consumer_key'
  config.consumer_secret    = 'your-consumer_secret'
  config.oauth_token        = 'your-oauth_token'
  config.oauth_token_secret = 'your-oauth_token_secret'
  config.auth_method        = :oauth
end

terms = ['ladygaga']

daemon = TweetStream::Daemon.new('tracker',
  :log_output => true,
  :backtrace  => true,
)

daemon.on_inited do
  ActiveRecord::Base.connection.reconnect!
  p "Listening..."
end

daemon.on_error do |message|
  puts "on_error: #{message}"
end

daemon.on_reconnect do |timeout, retries|
  puts "on_reconnect: #{timeout}, #{retries}"
end

daemon.on_limit do |discarded_count|
  puts "on_limit: #{skip_count}"
end

daemon.track(terms) do |status|
  # put here your model.create code!
  # Tweet.create!( :uid => status.id, ... )
end

To run the script just type:

ruby scrip-name.rb run
喵星人汪星人 2024-11-15 14:35:36

我假设这是一个更大的 Rails 应用程序的一部分。如果是这样,问题 1 是如果 Tweet.new 是标准的 activerecord 对象,则它不会将任何内容持久保存到数据库中。尝试 Tweet.create 其次,我不确定脚本是否一定会知道推文(如果它是一个活动记录),而无需拉入 Rails 应用程序(可能通过包含environment.rb 文件)。

类似的东西:

ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/application"
Rails.application.require_environment!

如果这不起作用,您可以尝试仅包含活动记录,这里有一个描述它的问题和答案:

如何在 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. Try Tweet.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:

ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/application"
Rails.application.require_environment!

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文