public
Last active

Twitter Streaming API to MongoDB.

  • Download Gist
streaming.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# coding: utf-8
 
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
 
require 'rubygems'
require 'net/https'
require 'openssl'
require 'uri'
require 'json'
require 'mongo'
 
USERNAME = ""
PASSWORD = ""
 
con = Mongo::Connection.new
db = con.db('twitter')
tweets = db.collection('tweets')
 
uri = URI.parse('https://stream.twitter.com/1/statuses/sample.json')
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
https.verify_depth = 5
 
https.start do |h|
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth(USERNAME, PASSWORD)
 
h.request(request) do |response|
response.read_body do |chunk|
parsed = JSON.parse(chunk) rescue next
tweets.insert(parsed)
end
end
end

I am grateful you made this public gist THANKS for the help.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.