Live Tweets using Streaming API

Goals:

  • Use twitter streaming API to fetch live twitter streams

  • The streams will be displayed to the Console.

  • You can also retain the tweets by adding a log out to a log file using log4j.properties

  • You can specify the search string for twitter.

Step 1 : Sign up for a twitter account at twitter.com. If you already have access keys and tokens move to step 4.

Step 2. Create an app in apps.twitter.co All you need to enter are name (globally unique), description, a url. You can change the url later.

Step 3: Now you need to obtain 2 sets of details - keys and tokens. Both are available on the same page. Click on "Generate My Access Token" to generate tokens.

Make sure you got these following details

  • Consumer Key (API Key)

  • Consumer Secret (API Secret)

  • Access Token

  • Access Token Secret

Use the above values in the twitter4k.properties file as describe in the steps below.

Step 4: Create a java maven project in your IDE of choice. Use the following POM details.

<repositories>

<repository>

<id>twitter4j.org</id>

<name>twitter4j.org Repository</name>

<url>http://twitter4j.org/maven2</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>true</enabled>

</snapshots>

</repository>

</repositories>

<dependencies>

<dependency>

<groupId>org.twitter4j</groupId>

<artifactId>twitter4j-core</artifactId>

<version>[4.0,)</version>

</dependency>

<dependency>

<groupId>org.twitter4j</groupId>

<artifactId>twitter4j-stream</artifactId>

<version>[4.0,)</version>

</dependency>

</dependencies>

Step 5: Add a new java class "Stream.java".

import twitter4j.*;

public final class Stream {

private static final Logger logger = Logger.getLogger(Stream.class);


/**

* Main entry of this application.

*

* @param args arguments doesn't take effect with this example

* @throws TwitterException when Twitter service or network is unavailable

*/


public static void main(String[] args) throws TwitterException {

TwitterStream twitterStream = new TwitterStreamFactory().getInstance();

StatusListener listener = new StatusListener() {

public void onStatus(Status status) {

System.out.println("@" + status.getUser().getScreenName()

+ " - " + status.getText());

logger.info(TwitterObjectFactory.getRawJSON(status));

}

public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {

//System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());

}

public void onTrackLimitationNotice(int numberOfLimitedStatuses) {

System.out.println("Got track limitation notice:"

+ numberOfLimitedStatuses);

}

public void onScrubGeo(long userId, long upToStatusId) {

//System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);

}

public void onStallWarning(StallWarning warning) {

System.out.println("Got stall warning:" + warning);

}

public void onException(Exception ex) {

ex.printStackTrace();

}

};

twitterStream.addListener(listener);

if(args.length == 1){

twitterStream.filter(args[0]);

}else{

twitterStream.sample();

}

}

}

Step 6: Create a new directory "resources" inside your project and right click > Build Path > Use As Source Folder.

Step 7: Inside resources directory, create a new file twitter4j.properties. Fill in the place holders with details from the step 3.

debug=true

jsonStoreEnabled=true

oauth.consumerKey=<enter your details>

oauth.consumerSecret=<enter your details>

oauth.accessToken=<enter your details>

oauth.accessTokenSecret=<enter your details>

Step 8: Now you can export this project as jar and run it. Make sure your working directory (... that contains the project jar) contains the maven dependency jar.

$ java -cp "*" Stream "trump, hillary"

Step 9: If you want to retain the tweets, add a log4j property using log4j.properties to your project. Following is an example:

#log4j.properties

log4j.rootLogger=ERROR,console

log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.appender.console.target=System.err

log4j.appender.console.layout=org.apache.log4j.PatternLayout

log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n

log4j.logger.Stream=INFO,file

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.File=tweets.log

log4j.appender.file.MaxFileSize=10MB

log4j.appender.file.MaxBackupIndex=10

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %m%n