19
Consuming the Twitter Streaming API with Ruby and MongoDB Jeff Linwood Lone Star Ruby Conference V August 12, 2011 http://www.jefflinwood.com @jefflinwood Friday, August 12, 2011

Consuming the Twitter Streaming API with Ruby and MongoDB

Embed Size (px)

DESCRIPTION

A talk on connecting to the Twitter Streaming API and storing the tweets in MongoDB with Ruby.

Citation preview

Page 1: Consuming the Twitter Streaming API with Ruby and MongoDB

Consuming the Twitter Streaming API with Ruby and MongoDB

Jeff LinwoodLone Star Ruby Conference V

August 12, 2011http://www.jefflinwood.com

@jefflinwood

Friday, August 12, 2011

Page 2: Consuming the Twitter Streaming API with Ruby and MongoDB

Goals

• Watch for any tweet that contains certain keywords

• Store those tweets into the MongoDB database

Friday, August 12, 2011

Page 3: Consuming the Twitter Streaming API with Ruby and MongoDB

Demo (@aplusk)

Friday, August 12, 2011

Page 4: Consuming the Twitter Streaming API with Ruby and MongoDB

Twitter Streaming API+ Mongo DB

• Twitter Streaming API

• MongoDB

• Not a web application

Friday, August 12, 2011

Page 5: Consuming the Twitter Streaming API with Ruby and MongoDB

The many APIs of Twitter

• Twitter Streaming API

• User Streams

• Site Streams (for the big boys)

• REST API

• Search API

Friday, August 12, 2011

Page 6: Consuming the Twitter Streaming API with Ruby and MongoDB

Twitter Streaming API

• Keywords

• Users

• Locations

Great photo is by rachel_thecathttp://www.flickr.com/photos/23209605@N00/2786126623/

Friday, August 12, 2011

Page 7: Consuming the Twitter Streaming API with Ruby and MongoDB

TweetStream gem by Michael Bleighhttps://github.com/intridea/tweetstream

Friday, August 12, 2011

Page 8: Consuming the Twitter Streaming API with Ruby and MongoDB

Connecting to the Twitter Streaming API• JSON responses

• HTTP Basic Authentication

• One stream per account (dev/prod)

• Leave it open!

• Don’t constantly reconnect, and if you do, back off

Friday, August 12, 2011

Page 9: Consuming the Twitter Streaming API with Ruby and MongoDB

Limitations of Twitter Streaming API

• 400 Keywords

• 5,000 User Ids

• 25 Location Boxes

• Can ask Twitter for increased access

Friday, August 12, 2011

Page 10: Consuming the Twitter Streaming API with Ruby and MongoDB

A Tweet, in JSON

Friday, August 12, 2011

Page 11: Consuming the Twitter Streaming API with Ruby and MongoDB

Intro to MongoDB

• NoSQL - what does that mean?

• Great fit for JSON-oriented applications

• If you don’t know your schema in advance

• Query language

• Map Reduce

Friday, August 12, 2011

Page 12: Consuming the Twitter Streaming API with Ruby and MongoDB

Storing data in MongoDB

• Native format of MongoDB is BSON, similar to JSON

• Connect to a database (similar to MySQL)

• Connect to a collection (created if it doesn’t exist)

• Insert JSON (in our case, a tweet)

Friday, August 12, 2011

Page 14: Consuming the Twitter Streaming API with Ruby and MongoDB

Considerations for MongoDB

• Tweets - very verbose JSON

• Date format in Tweets not same as MongoDB

• May want to pre-process Tweets

• Can use both MongoDB and MySQL in same app if you want

Friday, August 12, 2011

Page 15: Consuming the Twitter Streaming API with Ruby and MongoDB

Here’s the code

• Okay, the whole thing is really done in about three lines.

Friday, August 12, 2011

Page 16: Consuming the Twitter Streaming API with Ruby and MongoDB

Where do you run this?

• Command line

• Your own server

• Heroku + MongoLab

• Other cloud services

Friday, August 12, 2011

Page 17: Consuming the Twitter Streaming API with Ruby and MongoDB

MongoLab

Friday, August 12, 2011

Page 18: Consuming the Twitter Streaming API with Ruby and MongoDB

Now what?

• Step 1: Collect Tweets

• Step 2: ????

• Step 3: Profit!

Friday, August 12, 2011