How can I obtain the friends list of a friend or follower using Twitter4J?
Using getFriendsId(), I'm only able to retrieve the friend's/follower's list of that current user which is authenticated. What I want is to obtain the list of friends of a follower or friend of the authenticated user.
This will show the name of your friend's followers.
User u1 = null ;
long cursor = -1;
IDs ids;
System.out.println("Listing followers's ids.");
do {
ids = twitter.getFollowersIDs("username", cursor);
for (long id : ids.getIDs()) {
System.out.println(id);
User user = twitter.showUser(id);
System.out.println(user.getName());
}
} while ((cursor = ids.getNextCursor()) != 0);
You only need to do this:
Twitter twitter = mTwitterApp.getTwitterInstance();
long cursor = -1;
List<User> users=twitter.getFriendsList(mTwitterApp.getUserID(), cursor);
Here users is a list users who are your friends(you are following them).
mTwitterApp.getUserID() is your login useris which is a long value.
long lCursor = -1;
IDs friendsIDs = twitter.getFriendsIDs(userID, lCursor);
System.out.println(twitter.showUser(userID).getName());
System.out.println("==========================");
do
{
for (long i : friendsIDs.getIDs())
{
System.out.println("follower ID #" + i);
System.out.println(twitter.showUser(i).getName());
}
}while(friendsIDs.hasNext());
This code works! (without exceeding rate limits). Referred twitter4j documentation and other answers on StackOverflow.
try {
// get friends
long cursor = -1;
PagableResponseList<User> pagableFollowings;
do {
pagableFollowings = twitter.getFriendsList(twitter.getId(), cursor);
for (User user : pagableFollowings) {
listFriends.add(user); // ArrayList<User>
}
} while ((cursor = pagableFollowings.getNextCursor()) != 0);
// get followers
cursor = -1;
PagableResponseList<User> pagableFollowers;
do {
pagableFollowers = twitter.getFollowersList(twitter.getId(), cursor);
for (User user : pagableFollowers) {
listFollowers.add(user); // ArrayList<User>
}
} while ((cursor = pagableFollowers.getNextCursor()) != 0);
} catch (TwitterException e) {
printError(e);
}
You can use
twitter.getFollowersIDs("username", cursor);
http://twitter4j.org/javadoc/twitter4j/api/FriendsFollowersResources.html#getFollowersIDs-java.lang.String-long-
which returns only 5000 user not all users. Also it is limited 15 times in 15 minutes.(https://dev.twitter.com/rest/reference/get/friends/ids)
Also, you can use,
twitter.getFollowersList("username", cursor);
http://twitter4j.org/javadoc/twitter4j/api/FriendsFollowersResources.html#getFollowersList-java.lang.String-long- which is also limited with 20 user. Also it is limited 15 times in 15 minutes for user auth, 30 times in 15 minutes for app auth (https://dev.twitter.com/rest/reference/get/friends/list)
For unlimited access, you can look at https://gnip.com/ or whitelisted user access of twitter.
PagableResponseList<User> friendlist= twitter.getFriendsList(user.getScreenName(), -1);
int sizeoffreindlist= friendlist.size();
for(int i=0;i<sizeoffreindlist;i++)
{
System.out.println(friendlist.get(i));
}
It will provide you a list of 20 friends as the default limit is 20
What about something like get Friends List? https://dev.twitter.com/docs/api/1.1/get/friends/list
According to the docs:
Returns a cursored collection of user objects for every user the
specified user is following (otherwise known as their "friends").
There is an interface for this in twitter4j.api, but I can't figure out how to use it:
PagableResponseList<User> getFriendsList(String screenName, long cursor) throws TwitterException;
Related
I have an application in which I pass conferenceDto object with User Ids to my conferenceService where it needs to be added to a Conference Model. The problem is that the conferenceDto list of user ids is a string (ex. "2,4"). I am trying to find the best way of turning this collection of strings to a list of objects of type User
My conferenceService method:
#Override
public Conference updateConference(#Valid ConferenceDto conferenceDto){
Authentication user1 = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findByUsername(user1.getName());
Optional<Conference> conferenceTemp = findById(conferenceDto.getConference_id());
if (nameExist(conferenceDto.getName()) && !conferenceDto.getName().equals(conferenceTemp.get().getName())) {
throw new ConferenceAlreadyExistException(
"There is a conference with that name: "
+ conferenceDto.getName());
}
Conference conference = new Conference();
conference.setConference_id(conferenceDto.getConference_id());
conference.setCreator(user);
conference.setName(conferenceDto.getName());
conference.setDescription(conferenceDto.getDescription());
conference.setStartConference(conferenceDto.getStartConference());
conference.setEndConference(conferenceDto.getEndConference());
conference.setStudents(Collections.singletonList(userService.findById(conferenceDto.getStudents()))); // doesnt work this way because findById requires type long but here I am using Collection<Strings>
return conferenceRepository.save(conference);
}
I am quite new to Java and Spring so Im not sure if this needs a for loop to fill a new list and then pass it to conference.setStudents or it can be done another way. Any tips is very appreciated!
p.s. Type Conference's students is a Collection<User>
I found a solution. I did create a for loop and it turns the collection of strings into a collection of users like I want to. Although, it does not save to my attendance_table for some reason
Changed conferenceService method to:
#Override
public Conference updateConference(#Valid ConferenceDto conferenceDto){
Authentication user1 = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findByUsername(user1.getName());
Optional<Conference> conferenceTemp = findById(conferenceDto.getConference_id());
if (nameExist(conferenceDto.getName()) && !conferenceDto.getName().equals(conferenceTemp.get().getName())) {
throw new ConferenceAlreadyExistException(
"There is a conference with that name: "
+ conferenceDto.getName());
}
Conference conference = new Conference();
conference.setConference_id(conferenceDto.getConference_id());
conference.setCreator(user);
conference.setName(conferenceDto.getName());
conference.setDescription(conferenceDto.getDescription());
conference.setStartConference(conferenceDto.getStartConference());
conference.setEndConference(conferenceDto.getEndConference());
Collection<User> userCollection = new ArrayList<>();
for (String s: conferenceDto.getStudents()){
System.out.println(s);
userCollection.add(userService.findById((long) Integer.parseInt(s)).get());
}
conference.setStudents(userCollection);
return conferenceRepository.save(conference);
}
private boolean nameExist(String name) {
return conferenceRepository.findByName(name) != null;
}
I have been wondering if there is a way to access all the twitter followers list.
We have tried using call to the REST API via twitter4j:
public List<User> getFriendList() {
List<User> friendList = null;
try {
friendList = mTwitter.getFollowersList(mTwitter.getId(), -1);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (TwitterException e) {
e.printStackTrace();
}
return friendList;
}
But it returns only a list of 20 followers.
I tried using the same call in loop, but it cause a rate limit exception - says we are not allowed to make too many requests in a small interval of time.
Do we have a way around this?
You should definitely use getFollowersIDs. As the documentation says, this returns an array (list) of IDs objects. Note that it causes the list to be broken into pages of around 5000 IDs at a time. To begin paging provide a value of -1 as the cursor. The response from the API will include a previous_cursor and next_cursor to allow paging back and forth.
The tricky part is to handle the cursor. If you can do this, then you will not have the problem of getting only 20 followers.
The first call to getFollowersIDs will need to be given a cursor of -1. For subsequent calls, you need to update the cursor value, by getting the next cursor, as done in the while part of the loop.
long cursor =-1L;
IDs ids;
do {
ids = twitter.getFollowersIDs(cursor);
for(long userID : ids.getIDs()){
friendList.add(userID);
}
} while((cursor = ids.getNextCursor())!=0 );
Here is a very good reference:
https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/friendsandfollowers/GetFriendsIDs.java
Now, if the user has more than around 75000 followers, you will have to do some waiting (see Vishal's answer).
The first 15 calls will yield you around 75000 IDs. Then you will have to sleep for 15 minutes. Then make another 15 calls, and so on till you get all the followers. This can be done using a simple Thread.sleep(time_in_milliseconds) outside the for loop.
Just Change like this and try, this is working for me
try {
Log.i("act twitter...........", "ModifiedCustomTabBarActivity.class");
// final JSONArray twitterFriendsIDsJsonArray = new JSONArray();
IDs ids = mTwitter.mTwitter.getFriendsIDs(-1);// ids
// for (long id : ids.getIDs()) {
do {
for (long id : ids.getIDs()) {
String ID = "followers ID #" + id;
String[] firstname = ID.split("#");
String first_Name = firstname[0];
String Id = firstname[1];
Log.i("split...........", first_Name + Id);
String Name = mTwitter.mTwitter.showUser(id).getName();
String screenname = mTwitter.mTwitter.showUser(id).getScreenName();
// Log.i("id.......", "followers ID #" + id);
// Log.i("Name..", mTwitter.mTwitter.showUser(id).getName());
// Log.i("Screen_Name...", mTwitter.mTwitter.showUser(id).getScreenName());
// Log.i("image...", mTwitter.mTwitter.showUser(id).getProfileImageURL());
}
} while (ids.hasNext());
} catch (Exception e) {
e.printStackTrace();
}
Try This...
ConfigurationBuilder confbuilder = new ConfigurationBuilder();
confbuilder.setOAuthAccessToken(accessToken)
.setOAuthAccessTokenSecret(secretToken)
.setOAuthConsumerKey(TwitterOAuthActivity.CONSUMER_KEY)
.setOAuthConsumerSecret(TwitterOAuthActivity.CONSUMER_SECRET);
Twitter twitter = new TwitterFactory(confbuilder.build()).getInstance();
PagableResponseList<User> followersList;
ArrayList<String> list = new ArrayList<String>();
try
{
followersList = twitter.getFollowersList(screenName, cursor);
for (int i = 0; i < followersList.size(); i++)
{
User user = followersList.get(i);
String name = user.getName();
list.add(name);
System.out.println("Name" + i + ":" + name);
}
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , list));
listView.setVisibility(View.VISIBLE);
friend_list.setVisibility(View.INVISIBLE);
post_feeds.setVisibility(View.INVISIBLE);
twit.setVisibility(View.INVISIBLE);
}
This is a tricky one.
You should specify whether you're using application or per user tokens and the number of users you're fetching followers_ids for.
You get just 15 calls per 15 minutes in case of an application token. You can fetch a maximum of 5000 followers_ids per call. That gives you a maximum of 75K followers_ids per 15 minutes.
If any of the users you're fetching followers_ids for has over 75K followers, you'll get the rate_limit error immediately. If you're fetching for more than 1 user, you'll need to build strong rate_limit handling in your code with sleeps and be very patient.
The same applies for friends_ids.
I've not had to deal with fetching more than 75K followers/friends for a given user but come to think of it, I don't know if it's even possible anymore.
I want to convert my Resultset to List of object . This is my query:
String querystring1= "SELECT userID, privilege"
+ "FROM dbo.User order by userID, privilege";
userID privilege
------------------
1001 read
1001 write
1001 execute
1001 delete
1006 execute
1006 read
1006 write
1007 read
1007 write
I have a class User defined like :
public class User {
private int userID;
private List<String> userPrivelege;
}
I want to have as an output to a list of Users, and this is my implemented code:
String previousId = null;
List<String> PrivList= new ArrayList<String>();
List<User> allUserList= new ArrayList<User>();
while(result_set.next()) {
String userID = result_set.getString("userID");
String privilege = result_set.getString("privilege");
if (previousId == null) { // first time
User user = new User();
PrivList.add(privilege);
previousId=userID;
} else if (previousId.equals(userID) {
PrivList.add(privilege);
} else {
user.setUserPrivilege(PrivList);
allUserList.add(user);
PrivList.clear();
previousId=null;
}
}
The problem is, other than the first user object created, all the next one are always missing the first value which means user 1006 will have 2 privileges other than 3.
Any idea?
All User objects refer the same object List of privileges as you don't create a new instance for each User. Instead you clear only the List.
Consequently, all Users are set with 2 privileges as the last User that you handle has 2 privileges.
So replace :
PrivList.clear();
by :
PrivList = new ArrayList<>();
That's because You Miss the First privilege of the New User here,
else {
user.setUserPrivilege(PrivList);
allUserList.add(user);
PrivList.clear();
previousId=null;
}
Do the privilege initialization for the next user here.
PrivList = new ArrayList<>();
PrivList.add(privilege);
As far as I can see, you are never assigning the user ID to the User object?
PrivList.clear(); clears the list that you have just assigned to a user. Instead create a new list, as davidxxx also said.
As anchreg said, after you have created a user and added it to the list, you need to initialize the next user in the same way as you did the first time.
After your loop terminates, if previousId is not null (that is, any user previlege was processed at all), you need to assign values to the last user and add it to the list in the same way as in the last else case in the loop.
All of this said a more elegant solution could be coded using streams.
You need to create a new instance of PrivList for each user.
Additionnaly, you need to add a privilege for the next user, otherwise you lose that information on the loop.
Edits shown by the <-- comments.
while(result_set.next()) {
String userID = result_set.getString("userID");
String privilege = result_set.getString("privilege");
if (previousId == null) { // first time
User user = new User();
PrivList.add(privilege);
previousId=userID;
} else if (previousId.equals(userID) {
PrivList.add(privilege);
} else {
// nex user
user.setUserPrivilege(PrivList);
allUserList.add(user);
PrivList = new ArrayList<>(); // <--
PrivList.add(privilege); // <--
previousId=null;
}
}
In m y opinion, I think that is not a good model. You should have a second table with privileges related with an user. But to solve your question:
List<String> privList= new ArrayList<String>();
Map<Integer, List<String>> hmUserPrivileges = HashMap<Integer, String>();
while(result_set.next()) {
int userID = result_set.getInt("userID");
String privilege = result_set.getString("privilege");
if (!hmUserPrivileges.contains(userID)) {
privList= new ArrayList<String>();
}
privList.add(privilege);
hmUserPrivileges.put(userID, privList);
}
List<User> allUserList = new ArrayList<User>();
Iterator<Entry<Integer, List<String>>> iterator = hmUserPrivileges.entrySet().iterator();
while(iterator.hasNext()) {
Entry<Integer, List<String>> entry = iterator.next();
User user = new User()
user.setUserID(entry.getKey());
user.setUserPrivelege(entry.getValue());
allUserList.add(user);
}
I am trying to get all the followersIDs from an a twitter account with about 150.000 followers. I later want to map their location, but first I need all those IDs.
at the moment I am using this code:
long lCursorIDs = -1;
long[] fArray = new long[100];
do
{
fArray = twitter.getFollowersIDs(name, lCursorIDs).getIDs();
} while (twitter.getFollowersIDs(name, lCursorIDs).hasNext ());
try
{
PrintWriter pr = new PrintWriter(filenameOutput);
for (int i=0; i<fArray.length ; i++)
{
pr.println(fArray[i]);
}
pr.close();
System.out.println("Follower IDs collected and saved to file: " + filenameOutput );
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("No such file exists.");
}
This works for User with less followers. but with that many it always returns an error message - rate limit exceeded.
I was thinking about getting only a certain number of followersIDs per hour, but I am not sure how to do that and not start every hour from the beginning with the first follower. also, I am not sure how many followers I can get with one request. maybe it is 100, as with the "lookupUser" method but I am not sure.. any ideas/suggestions?
EDIT: ok, I just tried to get the followerIDs of an account with 2700 followers and it stored them correctly in the text file. It also only "cost" one request. than I changed the account name to an account with 15500 followers and it crashes again with an rate limit exceeded message. I don´t get why since it´s only roughly 6 times as many followers but all the remaining requests get spend.. any ideas on what I´m doing wrong?
the answer:
int numberOfFollowers;
numberOfFollowers = user.getFollowersCount();
//CREATE ARRAYS FOR FOLLOWER IDS
long cursor = -1;
long[] fArray = new long[numberOfFollowers];
long[] local = new long[5000];
IDs ids = twitter.getFollowersIDs(name, cursor);
int j = 0;
int x = 5000;
int durchgang = 1;
int d_anzahl = 1 + numberOfFollowers / 5000;
//STROE FOLLOWER IDS IN ARRAYS
do
{
ids = twitter.getFollowersIDs(name, cursor);
local = twitter.getFollowersIDs(name, cursor).getIDs();
System.out.println("Durchgang: " + durchgang + " / " + d_anzahl );
System.arraycopy(local, 0, fArray, j * x , local.length);
j++;
durchgang++;
cursor = ids.getNextCursor();
} while (ids.hasNext());
this gets an array with all follower IDs of any twitter User. It calculates the number of loops needed to get all follower IDs and copys each array of 5000 IDs into new array which has all IDs at the end.
I'm trying to get a list of recent statuses from each user on a persons list of followers. I've got the following to get the users...
IDs list = twitter.getFriendsIDs(0);
for(long ID : list.getIDs()){
twitter4j.User TW_user = twitter.showUser(ID);
}
All I can get from this is getStatus() which is their most recent status. getHomeTimeline() is also insufficient as I need a list of recent tweets from each user. Is there anyway I can achieve this using Twitter4J?
I was just trying to find this answer myself. I had decent success using the getUserTimeline method. Looks like you're trying to look up a list of friend IDs, so this method below should take the long[] and spit out all the user statuses. lookupUsers also accepts a String[] of screen names if you want to look users up that way instead.
public static void lookupUsers(long[] usersList) {
try {
Twitter twitter = new TwitterFactory().getInstance();
ResponseList<User> users = twitter.lookupUsers(usersList);
Paging paging = new Paging(1, 100);
List<Status> statuses;
for (User user : users) {
statuses = twitter.getUserTimeline(user.getScreenName(), paging);
System.out.println("\nUser: #" + user.getScreenName());
for (Status s : statuses) {
System.out.println(s.getText());
}
}
} catch (TwitterException e) {
e.printStackTrace();
}
}
Alex's answer is close, but will only get you 100 tweets per user. The following will get you all (or at least the API's max limit):
IDs list = twitter.getFriendsIDs(0);
for(long ID : list.getIDs()) {
Status[] tweets = getAllTweets(twitter, ID);
System.out.println(ID + ": " + tweets.length);
}
Status[] getAllTweets(Twitter twitter, long userId)
{
int pageno = 1;
List statuses = new ArrayList();
while (true)
{
try
{
int size = statuses.size();
Paging page = new Paging(pageno++, 100);
statuses.addAll(twitter.getUserTimeline(userId, page));
if (statuses.size() == size)
break;
}
catch (TwitterException e)
{
e.printStackTrace();
}
}
return (Status[]) statuses.toArray(new Status[0]);
}