【问题标题】:Chronic parsing Twitter dates to integer将 Twitter 日期长期解析为整数
【发布时间】:2011-02-04 17:19:07
【问题描述】:

我再次在 Rails 中处理日期格式问题。我正在尝试将 ActiveRecord 中的日期与在 cron rake 中创建推文的日期进行比较,但我遇到了我不理解的错误。

"Wed Feb 02 23:04:18 +0000 2011"
1296687858

2009-08-31 07:00:00 UTC
1251698400
survived

"Wed Feb 02 22:31:33 +0000 2011"
1296685893

2009-08-31 07:00:00 UTC
1251698400
survived

"Wed Feb 02 22:24:00 +0000 2011"
1296685440

2009-08-31 07:00:00 UTC
1251698400
survived

"Wed Feb 02 20:40:57 +0000 2011"
1296679257

2009-08-31 07:00:00 UTC
1251698400
survived

"Fri Jan 28 22:17:23 +0000 2011"
1296253043

2009-08-31 07:00:00 UTC
1251698400
survived

rake aborted!
can't convert String into Integer
/Users/phil/Sites/travlrapp.com/lib/tasks/cron.rake:29:in `[]'
/Users/phil/Sites/travlrapp.com/lib/tasks/cron.rake:29:in `fetch_tweets'
/Users/phil/Sites/travlrapp.com/lib/tasks/cron.rake:27:in `each'
/Users/phil/Sites/travlrapp.com/lib/tasks/cron.rake:27:in `fetch_tweets'
/Users/phil/Sites/travlrapp.com/lib/tasks/cron.rake:12:in `each'
/Users/phil/Sites/travlrapp.com/lib/tasks/cron.rake:12:in `fetch_tweets'
/Users/phil/Sites/travlrapp.com/lib/tasks/cron.rake:160
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/bin/rake:19:in `load'
/usr/bin/rake:19

这里的第 29 行是 puts tweet['created_at'].inspect,但如果我删除它,它将在它之后不久的 if 语句中出现致命错误。

desc "Automated Tasks"
task :cron => :environment do

    # TASK FUNCTIONS
    def fetch_tweets
        users = User.find(
            :all,
            :include => [ :authentications, :current_trip ],
            :conditions => 'trips.id > 0'
        )

        users.each do |user|

            auth = user.authentications.find(:first, :conditions => { :provider => 'twitter' })

            # Only bother if they are connected to twitter
            if user.current_trip and auth and auth['token'] and auth['secret'] then

                # Exchange our oauth_token and oauth_token secret for the AccessToken instance.
                access_token = Twitter.connect(auth['token'], auth['secret'])

                # use the access token as an agent to get the home timeline
                response = access_token.request(:get, "http://api.twitter.com/1/statuses/user_timeline.json")

                tweets = ActiveSupport::JSON.decode(response.body)

                tweets.each do |tweet|

                    puts tweet['created_at'].inspect
                    puts Chronic::parse(tweet['created_at']).to_i
                    puts ''

                    puts user.current_trip.start_date
                    puts Chronic::parse( user.current_trip.start_date ).to_i

                    # has coordinates and was tweeted since the start date
                    if tweet['coordinates'] != nil and (Chronic::parse(tweet['created_at']).to_i) >= (Chronic::parse(user.current_trip.start_date).to_i)

                        coords = tweet['coordinates']['coordinates'];

                        entry = Entry.new
                        entry.text = tweet['text']
                        entry.resource = "http://twitter.com/#{tweet['user']['screen_name']}/status/#{tweet['id']}"
                        entry.source = 'twitter'
                        entry.user_id = user.id
                        entry.trip_id = user.current_trip.id
                        entry.latitude = coords[1]
                        entry.longitude = coords[0]
                        entry.published_at = tweet['created_at']
                        entry.unique = 'twitter-' + (tweet['id']).to_s

                        puts entry.inspect if entry.save

                    end

                    puts 'survived'
                    puts ''

                end
            end

        end # users.each
    end

现在让我感到奇怪的是,它似乎完美地转换了两个日期,然后显示 unix 时间进行比较。那么为什么它会在某个时候出现致命错误呢?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 chronic


    【解决方案1】:

    此时的推文格式不正确。你期待一个数组,但它不是 - 来自 json 的整个推文记录表示为一个字符串。

    只需在tweets.each do |tweet| 下方添加一个if tweet.respond_to?(:each)(与匹配的end)。

    【讨论】:

    • 所有推文都是“哈希”,有两个项目是“数组”。 d.pr/xrhc 我试过“next if tweet.class== 'Array'”,但没用。
    【解决方案2】:

    修复了这个问题:

    next unless tweet and tweet.class == Hash
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 2017-01-03
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多