使用 tweetstream 守护进程写入数据库 [英] write to database with tweetstream daemon

查看:37
本文介绍了使用 tweetstream 守护进程写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所有与关键字匹配的推文写入我的数据库.我在 tracker.rb 中设置了以下内容:

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?

提前致谢

更新:我把所有东西都放在一个名为 twitter.rake.rake 文件中,然后用 $ rake scrap 启动恶魔:

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

推荐答案

您的第一种方法是最好的方法,您需要从命令行运行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.

你需要做这样的事情:

#!/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

这篇关于使用 tweetstream 守护进程写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆