Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package twitter_app_p1; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import twitter4j.FilterQuery; import twitter4j.StallWarning; import twitter4j.Status; import twitter4j.StatusDeletionNotice; import twitter4j.StatusListener; import twitter4j.TwitterStream; import twitter4j.TwitterStreamFactory; import twitter4j.conf.ConfigurationBuilder; import twitter4j.json.DataObjectFactory; /** * * @author rachel */ public class Twitter_app_p1 { static String file_name = "tweet_data"; //1gb per 1000 static long file_size_limit = 10 * 1024 * 1024; //10mB // static long file_size_limit = 1024; //1kB static String file_name_current = "tweet_data.txt"; static long file_size_current = 0; static int file_count_current = 0; static TwitterStream twitterStream; static StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); //Put data into Status member variables // JSONObject obj = new JSONObject(); // String screenName = status.getUser().getScreenName(); // GeoLocation geoLocation = status.getGeoLocation(); // Date createdAt = status.getCreatedAt(); // String text = status.getText(); // URLEntity[] urlEntities = status.getURLEntities(); // String urls = ""; // for(URLEntity urlEntities_iter : urlEntities) { // urls = urls + urlEntities_iter.getExpandedURL(); // } // String title = ""; // //json-simple is cool! ... put information into JSONObject called tweet // JSONObject tweet = new JSONObject(); // tweet.put("screen_name", screenName); // tweet.put("geo", geoLocation); // tweet.put("created_at", createdAt); // tweet.put("text", text); // tweet.put("url", urls); //part for connecting and getting the title /*try { Document url_website = Jsoup.connect(urls.toString().replaceAll("\\\\","")).get(); title = url_website.title(); } catch (IOException ex) { // Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } tweet.put("title", title);*/ // Status To JSON String Object statusJson = DataObjectFactory.getRawJSON(status); try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file_name_current, true)))) { out.println(statusJson); } catch (IOException ex) { // ex.printStackTrace(); } // keep track of file size, and replace all the escaped characters with normal string stuff file_size_current += statusJson.toString().length(); if (file_size_current >= file_size_limit) { file_count_current += 1; file_name_current = file_name + Integer.toString(file_count_current) + ".txt"; file_size_current = 1; } //keep track of file size, and replace all the escaped characters with normal string stuff // file_size_current += tweet.toString().replaceAll("\\\\", "").length(); // if (file_size_current >= file_size_limit) { // file_count_current += 1; // file_name_current = file_name + Integer.toString(file_count_current) + ".txt"; // file_size_current = 1; // } //set up to write to file // try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file_name_current, true)))) { // out.println(tweet.toString().replaceAll("\\\\","")); // }catch (IOException ex) { // // ex.printStackTrace(); // } //stop program because we have obtained 10MB * 500 = 5GB of data! if (file_count_current >= 500) { System.exit(0); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { //System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { //System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { //System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { //System.out.println("Got stall warning:" + warning); } @Override public void onException(Exception ex) { // ex.printStackTrace(); } }; public static void initialize_stuff() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("T9Gl1DWeRjeBazUInktTNtEtD") .setOAuthConsumerSecret("3NXrL1qGpF3WPVyJQ7ytInlhNjIsMgT6s7bJOIDhD1uotzZ958") .setOAuthAccessToken("2428771880-4e3LHhOzmiYx4lChlAEYtJ6sB2oetARYbXcqfyU") .setOAuthAccessTokenSecret("7HLOf3dafjJEGoioWOMHfgiNrhBDb66YJZgSzrDkTNA9x") .setJSONStoreEnabled(true); TwitterStreamFactory twitterStreamFact = new TwitterStreamFactory(cb.build()); twitterStream = twitterStreamFact.getInstance(); } public static void GetTweets() { twitterStream.addListener(listener); FilterQuery tweet_filter = new FilterQuery(); double[][] location = { { -124, 24 }, { -66, 49 } }; //continental US tweet_filter.locations(location); twitterStream.filter(tweet_filter); } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here initialize_stuff(); GetTweets(); } }