Guest User

Untitled

a guest
Jun 25th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import twitter4j.Status;
  2. import twitter4j.StatusAdapter;
  3. import twitter4j.StatusDeletionNotice;
  4. import twitter4j.StatusListener;
  5. import twitter4j.TwitterException;
  6. import twitter4j.TwitterStream;
  7. import twitter4j.TwitterStreamFactory;
  8. import java.io.FileNotFoundException;
  9. import java.io.IOException;
  10. import com.csvreader.CsvReader;
  11. import java.io.File;
  12. import java.io.FileWriter;
  13. import com.csvreader.CsvWriter;
  14. import java.util.Date;
  15. import java.text.DateFormat;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Calendar;
  18.  
  19. public static void main(String[] args)
  20. {
  21. int runtime=0;
  22. Calendar calOne=Calendar.getInstance();
  23. DateFormat dateFormatTime = new SimpleDateFormat("MM.dd.yyyy HH:mm:ss");
  24. DateFormat dateFormatDay= new SimpleDateFormtat("MM.dd.yyyy");
  25. System.out.println("I have started running at " + dateFormatTime.format(calOne);
  26. do
  27. {
  28. Calendar calTwo=Calendar.getInstance();
  29. String fileName = dateFormatDay.format(calTwo) + ".csv";
  30. boolean fileExists = new File(fileName).exists();
  31. CsvWriter Output= new CsvWriter(new File Writer(fileName , true) , ',');
  32. if (!fileExists)
  33. {
  34. Output.write("Entry #");
  35. Output.write("Time");
  36. Output.write("Text");
  37. Output.write("Tweet ID");
  38. Output.write("Retweet?");
  39. Output.write("Times Retweeted");
  40. Output.endRecord();
  41. }
  42. TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
  43. int count = 0;
  44. StatusListener listener = new StatusListener()
  45. {
  46. @Override
  47. public void onStatus(Status status)
  48. {
  49. count ++;
  50. Output.write(count);
  51. Output.write(dateFormatTime.format(status.getCreatedAt()));
  52. Output.write(status.getText());
  53. Output.write(status.getId());
  54. if(status.isRetweet() = = true) {Output.write("Y");}
  55. else {Output.write("N");}
  56. Output.write(status.getRetweetCount());
  57. Output.endRecord();
  58. }
  59. @Override
  60. public void onException (Exception ex)
  61. { ex.printStackTrace();}
  62. @Override
  63. public void onTrackLimitations (int num)
  64. { System.out.println("Track Limitation Notice :" + num); }
  65. };
  66. tstream.addListener(listener);
  67. //tstream.firehose()
  68. //They say this sample is only for people that are approved, I think we should look into working with it
  69. tstream.sample();
  70. if(count == 20000)
  71. {
  72. tstream.shutdown;
  73. Calendar calThree = Calendar.getInstance();
  74. long one= calTwo.getTimeInMillis();
  75. long two= calThree.getTimeInMillis();
  76. int minInBetween= (two - one)/(1000*60);
  77. System.out.println("Collected 20,000 Tweets in " + minInBetween + "minutes");
  78. if(mininBetween>90)
  79. {
  80. 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."
  81. runtime++;
  82. }
  83. }
  84. }(while runtime < 1);
Add Comment
Please, Sign In to add comment