⚠️ This lesson is retired and might contain outdated information.

Use the Twitter Stream API with Twit.js

Hannah Davis
InstructorHannah Davis
Share this video with your friends

Social Share Links

Send Tweet

We’ll get a real-time stream of tweets, and learn how to filter a stream by keyword, location, and user.

[00:00] The Twitter stream API lets you access a real-time stream of tweets. There are two ways to do this. The first is a sampling of random public tweets from the last seven days. To access this we can say barStream=bought.stream statuses/sample. Then we type stream.ontweet and then make a function to do something with that tweet.

[00:34] We'll just print out the test, and a new line. This is just a random sampling of tweets. You could do control-C to cancel. For more speciific information, we can do bought.stream statuses/filter and then pass it some parameters.

[01:02] There are several options here. First is to track and we can track via word. Let's try bots. That's not a super-popular one. If we tried something like win, we can see there are many more tweets for this one. You can also search for multiple words, and that will get you back tweets with either win or lose in them.

[01:37] If you want both words in a tweet, just separate them by a space. You can think of spaces as the logical and, and commas as the logical or. One additional thing to know -- if you search for a word with punctuation, the punctuation is treated as attached to the word.

[02:02] Searching win! Will only return tweets with this exact expression. Another cool thing we can do is filter by location, with the parameter locations. This takes two coordinates which represent a bounding box, with the first set of coordinates being the southwest corner. This location is New York City.

[02:33] Finally, we can filter by the follow parameter, which lets us input the IDs of specific users. I know in advance that my own personal ID is here. If I send out a test tweet the stream receives it.