Skip to content

Instantly share code, notes, and snippets.

@dakatsuka
Created October 31, 2010 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dakatsuka/656397 to your computer and use it in GitHub Desktop.
Save dakatsuka/656397 to your computer and use it in GitHub Desktop.
Twitter Streaming API to MongoDB.
# 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
@dakatsuka
Copy link
Author

Fixed.

@tayeke
Copy link

tayeke commented Oct 31, 2012

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