- Used Twitter’s streaming API to perform a basic sentiment analysis on the tweet stream. ntwitter Node package was used to get tweet stream. API keys can obtained from Twitter Developers website. It is advisable to update your $HOME/.bashrc file as follows to store keys:
export TWITTER_CONSUMER_KEY = your_key_from_twitter
export TWITTER_CONSUMER_SECRET = your_secret_from_twitter
export TWITTER_ACCESS_TOKEN = your_access_token_from_twitter
export TWITTER_ACCESS_SECRET = your_access_secret_from_twitter
The environment variables can be accessed using Node, you can run following command to test:
node -e "console.log(process.env.TWITTER_CONSUMER_KEY)"
node -e "console.log(process.env.TWITTER_CONSUMER_SECRET)"
Here is what program does on server-side and client-side
Server
- Retrives data from Twitter's streaming API:
- Uses API keys to fetch tweets from twitter.
- Stay tuned for more data(tweets) to become available for application.
- Extracts meaning from the data stream:
- Subsribe from twitter to get only tweets that contain either ‘love’ or ‘hate’ words. Parse the data received in the JSON format.
- Test that only tweets of interest are displayed on the server console.
- Push/emit the data to the web browser client:
- Establishes the socket.io connection with the web client requesting the connection.
- Emits information on user name and tweet text from every filtered tweet to the client as a JSON object.
- Analyzes the tweet streams:
- Updates the summary statistics on
- the total number of tweets containing love or hate
- the total number of tweets containing love
- the total number of tweets containing hate.
- Calculates the percentages for love and hate counts.
- Keep the total as actual count.
- Sends the above calculated three metrics to the client.
- Updates the summary statistics on
Client
- Establishes the socket.io connection with the server.
- Receive the tweets from the server.
- Receives updates from the server on the summary statistics about the love-hate sentiments on Twitter.
- Display the tweets and summary statistics on the webpage.
- Display the Love and Hate stats as percentages.
Navigate to directory containing above code. Start the server using following command:
node app.js
In web browser, enter following URL to execute localhost:3000/twitter_sentiment