Collecting Tweets Using a Geolocation Bounding Box Using Java (Twitter4j)

This code collects Tweets in an area around Galway, Ireland. The one thing to notice here (and something that baffled me for a while) is that while it returns (latitude, longitude), the bounding box is defined as {{longitude1,latitude1},{longitude2,latitude2}} as answered here

This is using Twitter4j 3.0.3 Core and Stream.

package me.davidcrowley.deri.geotweets;

import java.util.Date;

import twitter4j.FilterQuery;
import twitter4j.GeoLocation;
import twitter4j.Place;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.User;
import twitter4j.conf.ConfigurationBuilder;

public class GeoTweets {
	public static void main(String[] args) {
		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true);
		cb.setOAuthConsumerKey("xxxxxx");
		cb.setOAuthConsumerSecret("xxxxx");
		cb.setOAuthAccessToken("xxxxx");
		cb.setOAuthAccessTokenSecret("xxxxx");

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

		StatusListener listener = new StatusListener() {

			@Override
			public void onException(Exception arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onDeletionNotice(StatusDeletionNotice arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrubGeo(long arg0, long arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStatus(Status status) {
				User user = status.getUser();

				// gets Username
				String username = status.getUser().getScreenName();
				System.out.println(username);
				String profileLocation = user.getLocation();
				System.out.println(profileLocation);
				long tweetId = status.getId(); 
				System.out.println(tweetId);
				String content = status.getText();
				System.out.println(content);
				GeoLocation geolocation = status.getGeoLocation();
				double tweetLatitude = geolocation.getLatitude();
				double tweetLongitude = geolocation.getLongitude();

				String application = status.getSource();
				System.out.println(application);

				Place placenameJSON = status.getPlace();
				System.out.println(placenameJSON);
				String placename = placenameJSON.getFullName();
				System.out.println(placename);
				Date time = status.getCreatedAt();
				System.out.println(time);

				System.out.println(tweetLatitude + "," + tweetLongitude +"\n");
			}

			@Override
			public void onTrackLimitationNotice(int arg0) {
				// TODO Auto-generated method stub
				System.out.println("onTrackLimitationNotice" +"\n");

			}

			@Override
			public void onStallWarning(StallWarning arg0) {
				// TODO Auto-generated method stub
				System.out.println("onStallWarning" +"\n");

			}

		};
		FilterQuery fq = new FilterQuery();
		double lat = 53.270141;
		double longitude = -9.055488;
		double lat1 = lat - .25;
		double longitude1 = longitude - .25;
		double lat2 = lat + .25;
		double longitude2 = longitude + .25;
		twitterStream.addListener(listener);
		double[][] bb= {{longitude1, lat1}, {longitude2, lat2}};

		fq.locations(bb);

		twitterStream.filter(fq);  

	}
}

Leave a Reply