Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. //This code has to be opened in the Processing IDE, put it in the same folder as the JosKartTwitter.pde
  2. //We are gonnna use the Twitter4J library.
  3. import twitter4j.FilterQuery;
  4. import twitter4j.StallWarning;
  5. import twitter4j.Status;
  6. import twitter4j.Twitter;
  7. import twitter4j.TwitterFactory;
  8. import twitter4j.TwitterStream;
  9. import twitter4j.conf.ConfigurationBuilder;
  10. import twitter4j.StatusDeletionNotice;
  11. import twitter4j.StatusListener;
  12. import twitter4j.TwitterStreamFactory;
  13.  
  14. public class JosKartEngine {
  15. Twitter twitter;
  16. TwitterFactory twitterFactory;
  17. TwitterStream twitterStream;
  18. String[] directions = {"MOVEFORWARD", "MOVELEFT", "MOVERIGHT", "REVERSE"};
  19. String direction = " ";
  20. double distance = 0;
  21. double calculatedDistance;
  22. int secondsPerCM = 1507;
  23.  
  24. public void initConfig() {
  25. ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
  26. configurationBuilder.setDebugEnabled(true)
  27. .setOAuthConsumerKey(" ") //you will need to use your own Twitter Dev credentials.
  28. .setOAuthConsumerSecret(" ")
  29. .setOAuthAccessToken(" ")
  30. .setOAuthAccessTokenSecret(" ");
  31. twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance();
  32. println("CONFIGS ARE DONE");
  33. }
  34.  
  35. //get a number from the tweet
  36. public String getDistanceFrom (String theTweet)
  37. {
  38. if(theTweet == null || theTweet.isEmpty()) {return "0";}
  39.  
  40. StringBuilder stringBuilder = new StringBuilder();
  41. boolean foundDistance = false;
  42. for(char c : theTweet.toCharArray()){
  43. if(Character.isDigit(c)){
  44. stringBuilder.append(c);
  45. foundDistance = true;
  46. } else if (foundDistance) {break;}
  47. }
  48.  
  49. return stringBuilder.toString();
  50. }
  51.  
  52. public void stopMovementAfter(int actualDistance)
  53. {
  54. delay(actualDistance);
  55. serial.write(stopMoving());
  56. println("Distance covered...skirr");//I shouldn't have this here
  57. }
  58.  
  59. public void listenOnTweets()
  60. {
  61. StatusListener statusListener = new StatusListener(){
  62.  
  63. @Override
  64. public void onException(Exception ex) {
  65. System.out.println("ERRRORR: " + ex);
  66. }
  67.  
  68. @Override
  69. public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
  70. System.out.println("LIMIT: " + numberOfLimitedStatuses);
  71. }
  72.  
  73. @Override
  74. public void onStatus(Status status) {
  75. String theStatus = status.getText().toUpperCase();
  76. //the distance
  77. try { distance = Integer.parseInt(getDistanceFrom(theStatus));
  78. } catch (NumberFormatException ex) {
  79. System.out.println("OOPSS...numbers game: " + ex);
  80. }
  81. // the most magic happens on line 84 and 85
  82. calculatedDistance = (distance/28.571) * 1000; //28.. is the estimated speed of the motors on full throttle
  83. Integer timeLapse = Integer.valueOf((int) Math.round(calculatedDistance));//T = D/V * 1000 seconds
  84.  
  85. System.out.println("\n\n-------------NEW TWEET-----------------");
  86. System.out.println("\nTWEET: " + status.getText() + " BY: " + status.getUser().getScreenName());
  87. if (theStatus.contains(directions[0]))
  88. {
  89. println("\n\n--------Extracted Data-----------");
  90. println("Direction: " + directions[0] + "\nDistance: " + distance + "\nTravel Time: " + timeLapse + " milliseconds");
  91. serial.write(moveForward());
  92. stopMovementAfter(timeLapse);
  93. } else if(theStatus.contains(directions[1]))
  94. {
  95. println("Direction: " + directions[0] + "\nDistance: " + distance + "\nTravel Time: " + timeLapse + " milliseconds");
  96. serial.write(moveLeft());
  97. stopMovementAfter(timeLapse);
  98. } else if(theStatus.contains(directions[2]))
  99. {
  100. println("Direction: " + directions[0] + "\nDistance: " + distance + "\nTravel Time: " + timeLapse + " milliseconds");
  101. serial.write(moveRight());
  102. stopMovementAfter(timeLapse);
  103. } else if(theStatus.contains(directions[3]))
  104. {
  105. println("Direction: " + directions[0] + "\nDistance: " + distance + "\nTravel Time: " + timeLapse + " milliseconds");
  106. serial.write(moveBackward());
  107. stopMovementAfter(timeLapse);
  108. } else {
  109. System.out.println("CAN'T READ DIRECTION & Distance (maybe)");
  110. }
  111. System.out.println("\n-------------END TWEET-----------------");
  112. }
  113.  
  114. @Override
  115. public void onStallWarning(StallWarning warning) {
  116. System.out.println("STALLING: " + warning);
  117. }
  118.  
  119. @Override
  120. public void onScrubGeo(long userId, long upToStatusId) {
  121. System.out.println("USER: " + userId + " : " + upToStatusId);
  122. }
  123.  
  124. @Override
  125. public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
  126. System.out.println("DELETING STATUS: " + statusDeletionNotice);
  127. }
  128. };
  129.  
  130.  
  131. //the magic is here..a bit of it
  132. FilterQuery filterQuery = new FilterQuery();
  133. String keyWord = "#redbullbasement"; //you can use any keyword of choice.
  134. filterQuery.track(keyWord);
  135. twitterStream.addListener(statusListener);
  136. twitterStream.filter(filterQuery);
  137.  
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement