Last active
December 10, 2015 09:18
-
-
Save noexpect/4412901 to your computer and use it in GitHub Desktop.
twitter4jで自分のフォローしてる人のお気に入り登録ランキングをファイルに出力
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
import java.io.*; | |
import twitter4j.*; | |
import java.util.*; | |
public class GetFollowingFav { | |
public static String outputPath = "C:\\Users\\hogehoge\\Desktop\\result.txt"; | |
public static void main(String[] args) throws IOException, TwitterException { | |
Twitter tw = new TwitterFactory().getInstance(); | |
ArrayList<Long> idList = new ArrayList<Long>(); | |
ArrayList<String> favList = new ArrayList<String>(); | |
String content=""; | |
idList = getFollowings(tw); | |
for (int i=0; i<idList.size(); i++){ | |
favList.addAll(getFavs(tw, idList.get(i))); | |
if(i%30==0){ | |
try{ | |
Thread.sleep(30*60*1000); | |
}catch(InterruptedException e){} | |
} | |
} | |
content+=rankingUser(favList); | |
writeList(content); | |
} | |
static ArrayList<Long> getFollowings(Twitter tw) throws TwitterException{ | |
long cursor = -1L; | |
IDs ids = tw.getFriendsIDs(cursor); | |
ArrayList<Long> idList = new ArrayList<Long>(); | |
for (long id : ids.getIDs()) { | |
idList.add(id); | |
} | |
return idList; | |
} | |
static ArrayList<String> getFavs(Twitter tw, Long id) throws TwitterException{ | |
int Id = (int) (id+0); | |
ResponseList<Status> favs = tw.getFavorites(Id); | |
ArrayList<String> favList = new ArrayList<String>(); | |
for (Status status : favs) { | |
String userName = status.getUser().getScreenName(); | |
//String s = Id+","+userName+","+status.getText().replaceAll("\\n",""); | |
//String s = Id+","+userName; | |
String s = userName; | |
favList.add(s); | |
} | |
return favList; | |
} | |
static void writeList(String content) { | |
try{ | |
FileOutputStream f = new FileOutputStream(outputPath); | |
OutputStreamWriter out = new OutputStreamWriter(f, "Shift_JIS"); | |
out.write(content); | |
out.close(); | |
}catch(IOException e){ | |
System.out.println(e); | |
} | |
} | |
static String rankingUser(ArrayList<String> users){ | |
String result =""; | |
ArrayList<String> userNames = new ArrayList<String>(); | |
ArrayList<Integer> counter = new ArrayList<Integer>(); | |
for(String user : users) { | |
boolean flag = false; | |
int i=0; | |
for(String userName : userNames){ | |
if(user.equals(userName)){ | |
counter.set(i, counter.get(i)+1); | |
flag=true; | |
break; | |
} | |
i++; | |
} | |
if(!flag){ | |
userNames.add(user); | |
counter.add(1); | |
} | |
} | |
TreeSet<String> rankingResult = new TreeSet<String>(); | |
for(int i=0; i<userNames.size(); i++){ | |
String str =counter.get(i)+":"+userNames.get(i); | |
rankingResult.add(str); | |
} | |
ArrayList<String> revers = new ArrayList<String>(); | |
for(String item : rankingResult){ | |
revers.add(item); | |
} | |
for(int i=revers.size()-1; i>0; i--){ | |
result+=revers.get(i)+"\n"; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
まえのバージョンはフォローしてる人のお気に入り出力
いまのバージョンはフォローしてる人のお気に入り登録してるユーザランキングをファイル出力
になりました