-
-
Save neetaro/ab6995d83b3ac886e279323e15ae3207 to your computer and use it in GitHub Desktop.
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 test; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.User; | |
public class Minshukun { | |
public static void main(String[] args) throws TwitterException { | |
Twitter twitter = new TwitterFactory().getInstance(); | |
// 立憲民主党のユーザー情報を取得 | |
User user = twitter.showUser("CDP2017"); | |
System.out.println(user); | |
// 立憲民主党のフォロワーIDを取得() | |
long[] ids = twitter.getFollowersIDs(user.getId(), -1).getIDs(); | |
System.out.println(ids.length + "件のフォロワーを取得しました。"); | |
List<List<Long>> pack = new ArrayList<>(); | |
for (int j = 0; j < 50; j++) { | |
List<Long> tmp = new ArrayList<>(); | |
for (int i = 0; i < 100 && j * 100 + i < ids.length; i++) { | |
tmp.add(ids[j * 100 + i]); | |
} | |
pack.add(tmp); | |
} | |
System.out.println(pack.size() + "パックに小分けしました。"); | |
List<User> followers = new ArrayList<>(); | |
for (List<Long> query : pack) { | |
if (query.size() > 0) { | |
List<User> fpart = twitter.lookupUsers(query.stream().mapToLong(s -> s).toArray()); | |
followers.addAll(fpart); | |
} | |
} | |
System.out.print(followers.size() + "件のフォロワー情報を取得しました。"); | |
// 立憲民主党のフォロワーが何ツイートしているのかでグループ分け | |
Map<Integer, List<Integer>> tweetCountMap = followers.stream() | |
.mapToInt(s -> s.getStatusesCount()) | |
.sorted() | |
.boxed() | |
.collect(Collectors.groupingBy(s -> s)); | |
System.out.println("立憲民主党のフォロワー" + followers.size() + "人のツイート数の分布は"); | |
for (int tweetCount : tweetCountMap.keySet()) { | |
System.out.println("ツイート数 " + tweetCount + " : " + tweetCountMap.get(tweetCount).size() + "人"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment