Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import twitter4j.Status;
- import twitter4j.StatusAdapter;
- import twitter4j.StatusDeletionNotice;
- import twitter4j.StatusListener;
- import twitter4j.TwitterException;
- import twitter4j.TwitterStream;
- import twitter4j.TwitterStreamFactory;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import com.csvreader.CsvReader;
- import java.io.File;
- import java.io.FileWriter;
- import com.csvreader.CsvWriter;
- import java.util.Date;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- public static void main(String[] args)
- {
- int runtime=0;
- Calendar calOne=Calendar.getInstance();
- DateFormat dateFormatTime = new SimpleDateFormat("MM.dd.yyyy HH:mm:ss");
- DateFormat dateFormatDay= new SimpleDateFormtat("MM.dd.yyyy");
- System.out.println("I have started running at " + dateFormatTime.format(calOne);
- do
- {
- Calendar calTwo=Calendar.getInstance();
- String fileName = dateFormatDay.format(calTwo) + ".csv";
- boolean fileExists = new File(fileName).exists();
- CsvWriter Output= new CsvWriter(new File Writer(fileName , true) , ',');
- if (!fileExists)
- {
- Output.write("Entry #");
- Output.write("Time");
- Output.write("Text");
- Output.write("Tweet ID");
- Output.write("Retweet?");
- Output.write("Times Retweeted");
- Output.endRecord();
- }
- TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
- int count = 0;
- StatusListener listener = new StatusListener()
- {
- @Override
- public void onStatus(Status status)
- {
- count ++;
- Output.write(count);
- Output.write(dateFormatTime.format(status.getCreatedAt()));
- Output.write(status.getText());
- Output.write(status.getId());
- if(status.isRetweet() = = true) {Output.write("Y");}
- else {Output.write("N");}
- Output.write(status.getRetweetCount());
- Output.endRecord();
- }
- @Override
- public void onException (Exception ex)
- { ex.printStackTrace();}
- @Override
- public void onTrackLimitations (int num)
- { System.out.println("Track Limitation Notice :" + num); }
- };
- tstream.addListener(listener);
- //tstream.firehose()
- //They say this sample is only for people that are approved, I think we should look into working with it
- tstream.sample();
- if(count == 20000)
- {
- tstream.shutdown;
- Calendar calThree = Calendar.getInstance();
- long one= calTwo.getTimeInMillis();
- long two= calThree.getTimeInMillis();
- int minInBetween= (two - one)/(1000*60);
- System.out.println("Collected 20,000 Tweets in " + minInBetween + "minutes");
- if(mininBetween>90)
- {
- System.out.println("The Streamer has failed to collect the correct number of tweets between " + dateFormatTime.format(calTwo) + " and " + dateFormatTime.format(calThree) + ". The Streamer has been shut down."
- runtime++;
- }
- }
- }(while runtime < 1);
Add Comment
Please, Sign In to add comment