#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Location
    Arnhem NLD
    Posts
    2
    Rep Power
    0

    Twitter Streamin API crash


    Hello All,

    I want to write a Ruby script that uses the Twitter Streaming API and loads the results into MySQL. The base is ready, I can display the results on screen, but some how after a few lines of tweets, the scripts end with:


    2mysqlorg:60: undefined method `[]' for nil:NilClass (NoMethodError)
    from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:479:in `call'
    from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:479:in `on_decoded_body_data'
    from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:472:in `on_body_data'
    from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:786:in `process_chunk_body'
    from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:534:in `dispatch'
    from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:460:in `receive_data'
    from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
    from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
    from 2mysqlorg:40

    This is the script:

    Code:
    require 'rubygems'
    require 'em-http'
    require 'json'
    require 'mysql'
    
    class LanguageDetector
      URL = "http://www.google.com/uds/GlangDetect"
    
      include EM::Deferrable
    
      def initialize(text)
        request = EM::HttpRequest.new(URL).get({
          :query => {:v => "1.0", :q => text}
        })
    
        # This is called if the request completes successfully (whatever the code)
        request.callback {
          if request.response_header.status == 200
            info = JSON.parse(request.response)["responseData"]
            if info['isReliable']
              self.succeed(info['language'])
            else
              self.fail("Language could not be reliably determined")
            end
          else
            self.fail("Call to fetch language failed")
          end
        }
    
        # This is called if the request totally failed
        request.errback {
          self.fail("Error making API call")
        }
      end
    end
    
    # connect to the MySQL server
    dbh = Mysql.real_connect("localhost", "MyUser", "MyPass", "MyDB")
    
    EM.run {
      username = 'MyTwitterUsername'
      password = 'MyTwitterPassword'
      buffer = ""
    
      http = EventMachine::HttpRequest.new('http://stream.twitter.com/1/statuses/sample.json').post({
        :head => { 'Authorization' => [ username, password ] }
      })
    
      http.callback {
        unless http.response_header.status == 200
          puts "Call failed with response code #{http.response_header.status}"
        end
      }
    
      http.stream do |chunk|
        buffer += chunk
        while line = buffer.slice!(/.+\r\n/)
    	if line!=nil
    	      tweet = JSON.parse(line)
    	      if (tweet['user']['screen_name']!=nil && tweet['text']!=nil)
         			puts tweet['user']['screen_name'] + ": " + tweet['text']
    	      end
    	end
        end
      end
    }
    What am I doing wrong? I am a newbie, so I have no clou!

    Thank you,

    Roland.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Location
    Arnhem NLD
    Posts
    2
    Rep Power
    0
    Anyone???

IMN logo majestic logo threadwatch logo seochat tools logo