HTTP :: Parser :: Rails和Twitter Streaming API出错

问题描述 投票:3回答:1

我目前正在构建一个应用程序(Rails 3.1rc6),它涉及从Twitter Streaming API过滤数据,但在收到流后出现问题。

我尝试过同时使用Twitter-Stream gem和tweetstream gem,但两者都有同样的问题,因为tweetstream依赖于Twitter-Stream,所以并不令人惊讶。

我可以非常精确地获取流,并使用以下代码在tracker.rb文件中打印相关信息,我计划将其作为守护程序运行:

require 'rubygems'
require 'twitter/json_stream'
require 'json'

EventMachine::run {
  stream = Twitter::JSONStream.connect(
    :path    => '/1/statuses/filter.json?track=rails',
    :auth    => 'user:password'
  )

  stream.each_item do |item|
    @result = JSON.parse(item)
    puts @result["text"]

  end

 #Handle Errors...

由于我打算过滤某些单词并将它们存储在数据库中,但只要我尝试使用ActiveRecord做任何事情,我就会收到以下错误

Tom-Liveseys-MacBook-Air:twitter_stream Tom$ ruby ./lib/daemons/tracker.rb
String
/Library/Ruby/Gems/1.8/gems/twitter-stream-0.1.14/lib/twitter/json_stream.rb:121:in `<<': Could not parse data entirely            (HTTP::Parser::Error)
from /Library/Ruby/Gems/1.8/gems/twitter-stream-0.1.14/lib/twitter/json_stream.rb:121:in `receive_data'
from /Library/Ruby/Gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /Library/Ruby/Gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
from ./lib/daemons/tracker.rb:5

有谁知道为什么我不能对数据做任何有用的事情? 我已经检查过JSON是否正确解析了它,而@result["text"]返回正确的字符串。

谢谢,

汤姆

ruby-on-rails-3 activerecord twitter
1个回答
0
投票

好的,我弄明白为什么我会收到错误。 我试图将tweet id作为整数抓取并将其插入到pg数据库(在heroku上),它不喜欢它。

也许错误来自尝试用大整数读取该属性。 无论如何,我将列类型更改为字符串并改为抓取tweet["id_str"]属性。 现在很棒。

© www.soinside.com 2019 - 2024. All rights reserved.