Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package co.windall.twitter;
- import java.math.BigInteger;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.time.Instant;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- import twitter4j.IDs;
- import twitter4j.Paging;
- import twitter4j.RateLimitStatus;
- import twitter4j.Twitter;
- import twitter4j.TwitterException;
- import twitter4j.TwitterFactory;
- import twitter4j.conf.ConfigurationBuilder;
- // TODO: Auto-generated Javadoc
- /**
- * The Class TwitterBot.
- */
- public class TwitterBot {
- /** The api write. */
- private static boolean apiWrite = false;
- private static Twitter twitter;
- private static Connection connection;
- public static void main(String[] args) throws SQLException {
- config();
- indexFollowing();
- Statement stmt = connection.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM User;");
- while ( rs.next() ) {
- System.out.println(rs.getLong("id"));
- }
- rs.close();
- stmt.close();
- }
- /**
- * Runs any config needed for the bot.
- */
- private static void config() {
- //Connect to the database
- Database db = new Database();
- connection = db.getConnection();
- //Connect to twitter
- ConfigurationBuilder cb = new ConfigurationBuilder();
- cb.setDebugEnabled(true)
- .setOAuthConsumerKey("7hJx8nAKbQ8dvc5jI58lyeNHA")
- .setOAuthConsumerSecret("0qDogd5BvjhnlRgWQv6PVBMveonOhFfaOqK6TGmP4knkWK2mWv")
- .setOAuthAccessToken("3275525244-V8QvYT4vA93o31pGGVijEfk9t2OTQc1MvxomJsM")
- .setOAuthAccessTokenSecret("fazE2dCqcpYqJ8X8QZ7BDmC4Npwp6Ull36g0ONdyo9iGv");
- TwitterFactory tf = new TwitterFactory(cb.build());
- twitter = tf.getInstance();
- }
- /**
- * Identify users we follow.
- * @throws SQLException
- */
- private static void indexFollowing() throws SQLException {
- List<Long> ids = new ArrayList<Long>();
- ids.add(2442505428L);
- long cursor = -1;
- do {
- try {
- IDs results = twitter.getFriendsIDs(twitter.getId(), cursor);
- ids.addAll(Arrays.stream(results.getIDs()).boxed().collect(Collectors.toList()));
- cursor = results.getNextCursor();
- RateLimitStatus limitStatus = results.getRateLimitStatus();
- System.out.println(results);
- if(limitStatus.getRemaining() <= 0) {
- Thread.sleep(limitStatus.getSecondsUntilReset()*1000);
- }
- } catch (TwitterException | InterruptedException e) {
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
- }
- } while(cursor != 0);
- Statement stmt = connection.createStatement();
- for(Long id : ids) {
- try {
- ResultSet rs = stmt.executeQuery("SELECT * FROM User WHERE id=" + id.toString() + ";");
- if(rs.next()){
- if(rs.getLong("followedThem") == -1) {
- stmt.executeUpdate("UPDATE User SET followedThem=" + String.valueOf(Instant.now().getEpochSecond()) + " WHERE id=" + id.toString() + ";");
- }
- } else {
- String sql = "INSERT INTO User (id,followedMe,followedThem,unfollowedMe,unfollowedThem,status) " +
- "VALUES (%d, %d, %d, %d, %d, %d);";
- stmt.executeUpdate(String.format(sql, id, -1, Instant.now().getEpochSecond(), -1, -1, 0));
- }
- rs.close();
- } catch (SQLException e) {
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
- e.printStackTrace();
- System.err.println(id);
- System.exit(0);
- }
- }
- stmt.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement