Created
October 31, 2010 10:18
-
-
Save dakatsuka/656397 to your computer and use it in GitHub Desktop.
Twitter Streaming API to MongoDB.
This file contains 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
# 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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed.