- Streaming APIs
- Signing Up for Twitter
- Using Twitter's API with Node.js
- Extracting Meaning from the Data
- Pushing Data to the Browser
- Creating a Real-Time Lovehateometer
- Summary
- Q&A
- Workshop
- Exercises
Extracting Meaning from the Data
So far, you created a way to retrieve data in real-time from Twitter, and you saw a terminal window move very fast with a lot of data. This is good, but in terms of being able to understand the data, you are not able to answer the question set. To work toward this, you need to be able to parse the tweets received and extract information. Twitter provides data in JSON, a subset of JavaScript, and this is great news for using it with Node.js. For each response, you can simply use dot notation to retrieve the data that you are interested in. So, if you wanted to view the screen name of the user along with the tweet, this can be easily achieved:
twit.stream('statuses/filter', { track: ['love', 'hate'] }, function(stream) { stream.on('data', function (data) { console.log(data.user.screen_name + ': ' + data.text); }); });
Full documentation on the structure of the data received from Twitter is available on the documentation for the status element. This can be viewed online at https://dev.twitter.com/docs/api/1/get/statuses/show/%3Aid. Under the section “Example Request,” you can see the data structure for a status response. Using dot notation on the data object returned from Twitter, you are able to access any of these data points. For example, if you want the URL for the user, you can use data.user.url. Here is the full data available for the user who posted the tweet:
"user": { "profile_sidebar_border_color": "eeeeee", "profile_background_tile": true, "profile_sidebar_fill_color": "efefef", "name": "Eoin McMillan ", "profile_image_url": "http://a1.twimg.com/profile_images/1380912173/ Screen_shot_2011-06-03_at_7.35.36_PM_normal.png", "created_at": "Mon May 16 20:07:59 +0000 2011", "location": "Twitter", "profile_link_color": "009999", "follow_request_sent": null, "is_translator": false, "id_str": "299862462", "favourites_count": 0, "default_profile": false, "url": "http://www.eoin.me", "contributors_enabled": false, "id": 299862462, "utc_offset": null, "profile_image_url_https": "https://si0.twimg.com/profile_images/1380912173/ Screen_shot_2011-06-03_at_7.35.36_PM_normal.png", "profile_use_background_image": true, "listed_count": 0, "followers_count": 9, "lang": "en", "profile_text_color": "333333", "protected": false, "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme14/bg.gif", "description": "Eoin's photography account. See @mceoin for tweets.", "geo_enabled": false, "verified": false, "profile_background_color": "131516", "time_zone": null, "notifications": null, "statuses_count": 255, "friends_count": 0, "default_profile_image": false, "profile_background_image_url": "http://a1.twimg.com/images/themes/theme14/bg.gif", "screen_name": "imeoin", "following": null, "show_all_inline_media": false }
There is much more information available with each response including geographic coordinates, whether the tweet was retweeted, and more.