Advertisement
Guest User

File1

a guest
Mar 14th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. package co.windall.twitter;
  2.  
  3. import java.math.BigInteger;
  4. import java.sql.Connection;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.time.Instant;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.stream.Collectors;
  14.  
  15. import twitter4j.IDs;
  16. import twitter4j.Paging;
  17. import twitter4j.RateLimitStatus;
  18. import twitter4j.Twitter;
  19. import twitter4j.TwitterException;
  20. import twitter4j.TwitterFactory;
  21. import twitter4j.conf.ConfigurationBuilder;
  22.  
  23. // TODO: Auto-generated Javadoc
  24. /**
  25.  * The Class TwitterBot.
  26.  */
  27. public class TwitterBot {
  28.    
  29.     /** The api write. */
  30.     private static boolean apiWrite = false;
  31.  
  32.     private static Twitter twitter;
  33.     private static Connection connection;
  34.    
  35.     public static void main(String[] args) throws SQLException {
  36.         config();
  37.         indexFollowing();
  38.         Statement stmt = connection.createStatement();
  39.         ResultSet rs = stmt.executeQuery("SELECT * FROM User;");
  40.         while ( rs.next() ) {
  41.            System.out.println(rs.getLong("id"));
  42.         }
  43.         rs.close();
  44.         stmt.close();
  45.     }
  46.    
  47.     /**
  48.      * Runs any config needed for the bot.
  49.      */
  50.     private static void config() {
  51.         //Connect to the database
  52.         Database db = new Database();
  53.         connection = db.getConnection();
  54.        
  55.         //Connect to twitter
  56.         ConfigurationBuilder cb = new ConfigurationBuilder();
  57.         cb.setDebugEnabled(true)
  58.                 .setOAuthConsumerKey("7hJx8nAKbQ8dvc5jI58lyeNHA")
  59.                 .setOAuthConsumerSecret("0qDogd5BvjhnlRgWQv6PVBMveonOhFfaOqK6TGmP4knkWK2mWv")
  60.                 .setOAuthAccessToken("3275525244-V8QvYT4vA93o31pGGVijEfk9t2OTQc1MvxomJsM")
  61.                 .setOAuthAccessTokenSecret("fazE2dCqcpYqJ8X8QZ7BDmC4Npwp6Ull36g0ONdyo9iGv");
  62.         TwitterFactory tf = new TwitterFactory(cb.build());
  63.         twitter = tf.getInstance();
  64.     }
  65.    
  66.     /**
  67.      * Identify users we follow.
  68.      * @throws SQLException
  69.      */
  70.     private static void indexFollowing() throws SQLException {
  71.         List<Long> ids = new ArrayList<Long>();
  72.         ids.add(2442505428L);
  73.         long cursor = -1;
  74.         do {
  75.             try {
  76.                 IDs results = twitter.getFriendsIDs(twitter.getId(), cursor);
  77.                 ids.addAll(Arrays.stream(results.getIDs()).boxed().collect(Collectors.toList()));
  78.                 cursor = results.getNextCursor();
  79.                 RateLimitStatus limitStatus = results.getRateLimitStatus();
  80.                 System.out.println(results);
  81.                 if(limitStatus.getRemaining() <= 0) {
  82.                     Thread.sleep(limitStatus.getSecondsUntilReset()*1000);
  83.                 }
  84.             } catch (TwitterException | InterruptedException e) {
  85.                 System.err.println(e.getClass().getName() + ": " + e.getMessage());
  86.             }
  87.         } while(cursor != 0);
  88.         Statement stmt = connection.createStatement();
  89.         for(Long id : ids) {
  90.             try {
  91.                 ResultSet rs = stmt.executeQuery("SELECT * FROM User WHERE id=" + id.toString() + ";");
  92.                 if(rs.next()){
  93.                     if(rs.getLong("followedThem") == -1) {
  94.                         stmt.executeUpdate("UPDATE User SET followedThem=" + String.valueOf(Instant.now().getEpochSecond()) + " WHERE id=" + id.toString() + ";");
  95.                     }
  96.                 } else {
  97.                     String sql = "INSERT INTO User (id,followedMe,followedThem,unfollowedMe,unfollowedThem,status) " +
  98.                                  "VALUES (%d, %d, %d, %d, %d, %d);";
  99.                     stmt.executeUpdate(String.format(sql, id, -1, Instant.now().getEpochSecond(), -1, -1, 0));
  100.                 }
  101.                 rs.close();
  102.             } catch (SQLException e) {
  103.                 System.err.println(e.getClass().getName() + ": " + e.getMessage());
  104.                 e.printStackTrace();
  105.                 System.err.println(id);
  106.                 System.exit(0);
  107.             }
  108.         }
  109.         stmt.close();
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement