⚠️ 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
Published 7 years ago
Updated a year ago

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.

egghead
egghead
~ 35 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today