Skip to content

Instantly share code, notes, and snippets.

@yukihirai0505
Created January 23, 2018 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukihirai0505/7f15b67020f9e66b9cce2e4004a83e8e to your computer and use it in GitHub Desktop.
Save yukihirai0505/7f15b67020f9e66b9cce2e4004a83e8e to your computer and use it in GitHub Desktop.
Twitterで特定のユーザーのフォロワー名を全部取得する
  1. フォロワーIDを取得する => 5,000*15で15分で75,000フォロワー取得可能
  2. IDからユーザーを検索 => 100*900で15分で90,000分のユーザー情報取得可能
class OutputTwitterFollower {
def outputTwitterFollower = {
val targetAccount = "yabaiwebyasan"
def getFollower(ids: Seq[Long] = Seq.empty, nextCursor: Long = -1): Future[Seq[Long]] = {
twitterClient.followerIdsForUser(targetAccount, nextCursor, 5000).flatMap { result =>
val _ids = ids ++ result.data.ids
val cursor = result.data.next_cursor
if (cursor > 0) {
print(s"next_cursor: $cursor")
getFollower(_ids, cursor)
} else {
Future successful _ids
}
}
}
import java.io.{FileOutputStream => FileStream, OutputStreamWriter => StreamWriter}
val fileName = s"$targetAccount.txt"
val encode = "UTF-8"
val append = true
val fileOutPutStream = new FileStream(fileName, append)
val writer = new StreamWriter(fileOutPutStream, encode)
val ids = Await.result(getFollower(), Duration.Inf)
println(s"users by ids request num: ${ids.length}")
ids.grouped(100).toSeq.foreach { _ids =>
val users = Await.result(twitterClient.usersByIds(_ids), Duration.Inf).data
users.foreach(u => writer.write(s"${u.screen_name}\n"))
}
writer.close()
Future successful "ok"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment