Created
June 7, 2014 15:19
-
-
Save alibitek/a728ddd104f60706b572 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'net/https' | |
| require 'openssl' | |
| require 'uri' | |
| require 'json' | |
| require 'mongo' | |
| USERNAME = "" | |
| PASSWORD = "" | |
| con = Mongo::Connection.new | |
| db = con.db('twitter') | |
| # TODO: Create capped collection for better performance | |
| # https://github.com/joslynesser/mongo-twitter-streaming | |
| tweets = db.collection('tweets') | |
| uri = URI.parse('https://stream.twitter.com/1.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment