Created
November 23, 2020 00:20
-
-
Save maxandersen/eabd3466aa95d6e83c5b4590d11bc700 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//DEPS io.quarkus:quarkus-picocli:1.8.1.Final | |
//Q:CONFIG quarkus.banner.enabled=false | |
//Q:CONFIG quarkus.log.level=WARN | |
//DEPS org.twitter4j:twitter4j-core:4.0.7 | |
//JAVA 11+ | |
import picocli.CommandLine; | |
import javax.enterprise.context.Dependent; | |
import javax.inject.Inject; | |
import io.quarkus.runtime.annotations.QuarkusMain; | |
import io.quarkus.runtime.QuarkusApplication; | |
import io.quarkus.runtime.Quarkus; | |
import twitter4j.IDs; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.User; | |
import twitter4j.api.UsersResources; | |
import twitter4j.conf.ConfigurationBuilder; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import static java.lang.System.out; | |
@CommandLine.Command(description = "quick'n'dirty utility to iterate followers for a twitter user and get who was a specific follower.") | |
public class twitterth implements Runnable { | |
@CommandLine.Option(names="--user", required = true) | |
String user; | |
@CommandLine.Option(names="--markers", split = ",", defaultValue = "1,10,100,1000,10000,100000,10000000") | |
List<Long> markers; | |
@CommandLine.Option(names="--consumerKey", required = true, defaultValue = "${CONSUMERKEY}") | |
String consumerKey; | |
@CommandLine.Option(names="--consumerSecret", required = true, defaultValue = "${CONSUMERSECRET}") | |
String consumerSecret; | |
@CommandLine.Option(names="--accessToken", required = true, defaultValue = "${ACCESSTOKEN}") | |
String accessToken; | |
@CommandLine.Option(names="--accessTokenSecret", required = true, defaultValue = "${ACCESSTOKENSECRET}") | |
String accessTokenSecret; | |
@Override | |
public void run() { | |
ConfigurationBuilder cb = new ConfigurationBuilder(); | |
cb.setDebugEnabled(true) | |
.setOAuthConsumerKey(consumerKey) | |
.setOAuthConsumerSecret(consumerSecret) | |
.setOAuthAccessToken(accessToken) | |
.setOAuthAccessTokenSecret(accessTokenSecret); | |
TwitterFactory tf = new TwitterFactory(cb.build()); | |
Twitter twitter = tf.getInstance(); | |
try { | |
User twuser = twitter.showUser(user); | |
long followCount = twitter.users().showUser(twuser.getId()).getFollowersCount(); | |
out.println(String.format("%s has %s followers, looking for followers (%s)", user, followCount, markers.toString())); | |
ArrayList<Long> friendList = new ArrayList<>(); | |
long cursor = -1L; | |
long followersFound = 0; | |
IDs ids = null; | |
do { | |
ids = twitter.getFollowersIDs(user, cursor); | |
for(long userID : ids.getIDs()) { | |
friendList.add(userID); | |
followersFound++; | |
Long marker = (followCount - followersFound); | |
if(markers.contains(marker)) { | |
out.println(String.format("#%s was @%s", | |
marker, | |
twitter.showUser(userID).getScreenName())); | |
markers.remove(marker); | |
} | |
} | |
} while((cursor = ids.getNextCursor())!=0 && !markers.isEmpty()); | |
} catch (TwitterException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment