Created
January 14, 2016 19:42
-
-
Save fancellu/6e244fd846252d40ad27 to your computer and use it in GitHub Desktop.
Will export your twitter followers and following lists
This file contains hidden or 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
package twitter | |
import scala.collection.mutable.ArrayBuffer | |
import twitter4j._ | |
import twitter4j.conf._ | |
import scala.collection.JavaConverters._ | |
object TwitterExport extends App { | |
// http://twitter4j.org/en/code-examples.html | |
val cb = new ConfigurationBuilder() | |
// goto https://apps.twitter.com/ create your app, then create your oauth tokens | |
cb.setDebugEnabled(true) | |
.setOAuthConsumerKey("**") | |
.setOAuthConsumerSecret("**") | |
.setOAuthAccessToken("**") | |
.setOAuthAccessTokenSecret("**"); | |
val tf = new TwitterFactory(cb.build()) | |
val twitter = tf.getInstance() | |
def getFollowersIDs()={ | |
var lastCursor= -1L | |
val buff=new ArrayBuffer[Long] | |
while (lastCursor!=0){ | |
val users=twitter.getFollowersIDs(lastCursor) | |
buff++=users.getIDs | |
lastCursor=users.getNextCursor | |
} | |
buff | |
} | |
def getFriendIDs()={ | |
var lastCursor= -1L | |
val buff=new ArrayBuffer[Long] | |
while (lastCursor!=0){ | |
val users=twitter.getFriendsIDs(lastCursor) | |
buff++=users.getIDs | |
lastCursor=users.getNextCursor | |
} | |
buff | |
} | |
def lookupUsers(followerids:Seq[Long])={ | |
val grouped=followerids.grouped(100).toList | |
val buff=new ArrayBuffer[User] | |
for {group<-grouped | |
users=twitter.lookupUsers(group:_*) | |
user<-users.asScala | |
} yield user | |
} | |
def printOutUsers(users:List[User])= | |
users.zipWithIndex.foreach{case (u,idx)=>println(s"$idx,@${u.getScreenName},${u.getName}")} | |
val followerids=getFollowersIDs() | |
println("Followers===") | |
val followers=lookupUsers(followerids) | |
printOutUsers(followers) | |
val friendids=getFriendIDs() | |
println("Friends===") | |
val friends=lookupUsers(friendids) | |
printOutUsers(friends) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment