Skip to content

Instantly share code, notes, and snippets.

@slgithub
Forked from ashrithr/TwitterStreamExample.java
Last active September 3, 2015 12:18
Show Gist options
  • Save slgithub/3d6809f14ac06a9e49c0 to your computer and use it in GitHub Desktop.
Save slgithub/3d6809f14ac06a9e49c0 to your computer and use it in GitHub Desktop.
Twitter4j and GeoCode Parsing
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
/**
* Example illustrating 'twitter4j' streaming api to get data from twitter firehose, and get
* some useful information about the tweet. There is also an reverse geocode lookup function
* that wrote and then realized that twitter can give me that info as well :)
* Add the OAuth params in the main method.
*
* @author ashrith
*/
public class TwitterStreamExample {
/**
* Connect to the Google's GeoCode API and tries to retrieve the
* GeoCode information in json format
* @param address
* latitude and longitude information for reverse geocode
* @return String
* returns json string retrieved from GeoCode API
* @throws IOException
*/
public static String readUserLocationFeed(String address) throws IOException {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"http://maps.google.com/maps/api/geocode/json?latlng=" + address
+ "&sensor=false");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
System.err.println("Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
/**
* Parses the JSON object from GeoCode API
* @param lat
* latitude of the geocode
* @param lon
* longitude of the geocode
* @return String
* userLocation string
* @throws IOException
*/
public static String getUserLocation(String lat, String lon) throws IOException {
String userLocation = null;
String readUserFeed = readUserLocationFeed(lat.trim() + "," + lon.trim());
try {
JSONObject strJson = (JSONObject) JSONValue.parse(readUserFeed);
JSONArray jsonArray = (JSONArray) strJson.get("results");
JSONObject jsonAddressComp = (JSONObject)jsonArray.get(1);
userLocation = jsonAddressComp.get("formatted_address").toString();
} catch (Exception e) {
e.printStackTrace();
}
return userLocation;
}
public static void main(String[] args) throws InterruptedException {
//set the OAuth params from 'https://dev.twitter.com/apps/new'
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("");
cb.setOAuthConsumerSecret("");
cb.setOAuthAccessToken("");
cb.setOAuthAccessTokenSecret("");
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener(){
@Override
public void onStatus(Status status) {
/**
* Initializes every time it receives a tweet
*/
User user = status.getUser();
// gets Username
String username = status.getUser().getScreenName();
// user location
String profileLocation = user.getLocation();
// place
Place place = status.getPlace();
String placeCountry = place.getCountry();
String placeStreetAddress = place.getStreetAddress();
String placeName = place.getName();
String placeId = place.getId();
String placeFullName = place.getFullName();
String placeUrl = place.getURL();
String placeCountryCode = place.getCountryCode();
// tweet id
long tweetId = status.getId();
// tweet content
String content = status.getText();
// get tweet hash tags
HashtagEntity hashTags[] = status.getHashtagEntities();
// geo-location
GeoLocation geoLocation = status.getGeoLocation();
double latitude = geoLocation.getLatitude();
double longitude = geoLocation.getLongitude();
String userLocation = null;
try {
userLocation = getUserLocation(String.valueOf(latitude), String.valueOf(longitude));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("UserName: " + username);
System.out.println("UserLocation: " + profileLocation);
System.out.println("TweetId: " + tweetId);
System.out.println("Tweet: " + content);
HashSet<String> hashTagContainer = new HashSet<String>();
for(HashtagEntity hashTag: hashTags) {
hashTagContainer.add(hashTag.getText());
}
System.out.println("HashTags: " + hashTagContainer);
System.out.println("Lat: " + latitude);
System.out.println("Long: " + longitude);
System.out.println("User Full Location: " + userLocation);
System.out.println("Place_Country: " + placeCountry);
System.out.println("Place_Name: " + placeName);
System.out.println("Place_FullName: " + placeFullName);
System.out.println("Place_ID: " + placeId);
System.out.println("Place_StreetAddress: " + placeStreetAddress);
System.out.println("Place_URL: " + placeUrl);
System.out.println("Place_CountryId: " + placeCountryCode);
System.out.println();
System.out.println();
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
@Override
public void onTrackLimitationNotice(int i) {}
@Override
public void onScrubGeo(long l, long l2) {}
@Override
public void onStallWarning(StallWarning stallWarning) {}
@Override
public void onException(Exception e) {}
};
FilterQuery fq = new FilterQuery();
//Keywords to filter the stream on
String keywords[] = {"big data", "nfl", "cfb"};
fq.track(keywords);
twitterStream.addListener(listener);
twitterStream.filter(fq);
Thread.sleep(50000);
twitterStream.cleanUp();
twitterStream.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment