The title is confusing, I know, but I do not know how else to phrase this question.
Using twitter4j, I am able to get tweets and the list of users who have retweeted that tweet, like this
However, if the tweet is actually a retweet then I am not able to get the list of retweeters. Example
This is the code I am using to get the list of retweeters:
if(tweet.getId() > 0 && tweet.getRetweetCount() > 0) {
try {
List<Status> statuses = twitter.getRetweets(tweet.getId());
for (Status status : statuses) {
System.out.println("\n" + "\t" + "Retweeter ID:" + status.getUser().getId() + "\n" + "\t" + "Retweeter Name:" + status.getUser().getScreenName());
}
} catch (TwitterException e) {
//twitter.getRetweeterIds(tweet.getId(), 2, -1);
e.printStackTrace();
}
}
How do I get the retweeters of a retweet?
Twitter4J is a Java client library that interfaces with the Twitter REST API.
To understand the right call to use it's best to understand the underlying REST API.
Looking at the Twitter Rest API we can see an API that returns a list of users who have retweeted a particular tweet, GET statuses/retweeters/ids.
In your code the Twitter4J API you're using, getRetweets(), does not return the IDs of users who retweeted.
Looking at the Twitter4J Twitter4J API docs we find getRetweeterIds(statusId) that returns the list of user IDs that retweed a particular tweet indicated by statusId.
Related
I want get the information of user geolocation in twitter using Twitter4J.
I use tweet.getUser.getLocation() but that gives me the wrong geolocation.
// create ConfigurationBuilder class variables "cb"
// Create Twitter Factory
TwitterFactory twitterFactory = new TwitterFactory(cb.build());
Twitter twitter = twitterFactory.getInstance();
try {
Query query = new Query("범죄");
QueryResult result;
int twittNum = 1;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("===========================================================================================================");
System.out.println("["+twittNum+"] 번째 트윗");
System.out.println("유저이름1:"+tweet.getUser().getName());
System.out.println("트윗장소:"+tweet.getUser().getLocation());
System.out.println("트윗언어:"+tweet.getUser().getLang());
System.out.println("트윗시간?:"+tweet.getUser().getCreatedAt());
System.out.println("===========================================================================================================");
twittNum++;
}
} while ((query = result.nextQuery()) != null);
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}
What do you mean by wrong location?
Twitter4j is just an implementation on Java of the Twitter APi, and from the documentation:
location: Nullable. The user-defined location for this account’s profile. Not necessarily a location nor parseable. This field will occasionally be fuzzily interpreted by the Search service.
That means that tweet.getUser.getLocation() will get you the location from the user and it is not a geolocation (like on some tweets). You can't get the real location from an user, the location field is an input from the user (an string), so it could be anything. If you get stuffs like in your heart, below the bridge or something not so specific like Korea or the world that doesn't mean that is wrong, it just what people wrote.
i am using this code i get an error TwitterException 429
trying to fecth the tweets.
i want to fecth whole tweets of twitter account from starting.
how to solve twitter rate limit issue.
int limitRateCounter=0;
int countOfTweets=0; int numberOfTweets = 3500; long lastID = Long.MAX_VALUE; ArrayList<Status> status = new ArrayList<Status>();while (status.size () < numberOfTweets) { try {.out.print("\nlimit counter = "+limitRateCounter);.out.print("\t tweetsCounter = "+countOfTweets);
List<Status> listOfStatus=
twitter.getUserTimeline(tweeterHandle,pg);
/* making twitter request */
countOfTweets=countOfTweets+listOfStatus.size();
status.addAll(listOfStatus);
limitRateCounter++;
// println("Gathered " + tweets.size() + " tweets");
for (Status t: status)
if(t.getId() < lastID) lastID = t.getId();
}
catch (TwitterException te) {
System.out.println("Couldn't connect: " + te);
//twitter=getTwitterDetails2();
break;
};
pg.setMaxId(lastID-1); /* add pagging max id */
}
after 120 request .getUserTimeline(tweeterHandle,pg); methode not fetching
new tweets after some time get exception.
Twitter4j uses Twitter API to access Twitter data. This API has limits in the number of invocations as is specified here:
API Rate Limits
Rate Limits Charts
You seem to be facing this problem right now. Basically your code needs to wait before making another call until you are again inside the API rate limits. Take into account that the rate limit is per "access token" as the documentation specifies, so you could increase the number of calls your code can make if you provide it with more access tokens, but in the end you will have a (bigger) limit.
create several accounts on twitter (10) then put access tokens in array
This question already has answers here:
How to retrieve more than 100 results using Twitter4j
(4 answers)
Closed 6 years ago.
I am trying to create a program that searches a query from twitter. The problem I am having is that the API returns only a 100 result queries and when I try to retrieve more it keeps giving me the same results again.
User user = twitter.showUser("johnny");
Query query = new Query("football");
query.setCount(100);
query.lang("en");
int i=0;
try {
QueryResult result = twitter.search(query);
for(int z = 0;z<2;z++){
for( Status status : result.getTweets()){
System.out.println("#" + status.getUser().getScreenName() + ":" + status.getText());
i++;
}
}
The program will print me 200 results relating to the query "football", but instead of giving me 200 different results it prints a 100 results twice. My end results should be that I can print as many different results as the rate limit allows. I have seen programs that return more than 100 responses for a specific user, but I haven't seen something that can return more than a 100 responses for a unique query like "football".
To get more than 100 results on a search Query you need to call to the next iteration of the Query.
Query query = new Query("football");
QueryResult result;
int Count=0;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("#" + tweet.getUser().getScreenName() + ":" + tweet.getText());
Count++;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while ((query = result.nextQuery()) != null);
System.out.println(Count);
System.exit(0);
I just tested it and got 275 tweets, keep in mind this from the documentation:
The Search API is not complete index of all Tweets, but instead an index of recent Tweets. At the moment that index includes between 6-9 days of Tweets.
And:
Before getting involved, it’s important to know that the Search API is focused on relevance and not completeness. This means that some Tweets and users may be missing from search results. If you want to match for completeness you should consider using a Streaming API instead.
Is there any way to get tweets containing a keyword in java? I want to download as many as possible, I have seen a java library twitter4j but it gives only small number of tweets.
Read the documentation of twitter api
https://dev.twitter.com/docs/api/1/get/search
Its rate limited though. I dont think there is a way around it.
The rate limiting varies with open search apis and the ones that require authentication.
http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed
(Note - this link is copied from twiter api webpage)
You can set the page size and number using Twitter4J to request more tweets.
public static void main(String[] args) throws TwitterException {
Twitter twitter = new TwitterFactory().getInstance();
for (int page = 1; page <= 10; page++) {
System.out.println("\nPage: " + page);
Query query = new Query("#MyWorstFear"); // trending right now
query.setRpp(100);
query.setPage(page);
QueryResult qr = twitter.search(query);
List<Tweet> qrTweets = qr.getTweets();
if(qrTweets.size() == 0) break;
for(Tweet t : qrTweets) {
System.out.println(t.getId() + " - " + t.getCreatedAt() + ": " + t.getText());
}
}
}
Using the "Network Updates API" example at the following link I am able to post network updates with no problem using client.postNetworkUpdate(updateText).
http://code.google.com/p/linkedin-j/wiki/GettingStarted
So posting works great.. However posting an update does not return an "UpdateKey" which is used to retrieve stats for post itself such as comments, likes, etc. Without the UpdateKey I cannot retrieve stats. So what I would like to do is post, then retrieve the last post using the getNetworkUpdates() function, and in that retrieval will be the UpdateKey that I need to use later to retrieve stats. Here's a sample script in Java on how to get network updates, but I need to do this in Coldfusion instead of Java.
Network network = client.getNetworkUpdates(EnumSet.of(NetworkUpdateType.STATUS_UPDATE));
System.out.println("Total updates fetched:" + network.getUpdates().getTotal());
for (Update update : network.getUpdates().getUpdateList()) {
System.out.println("-------------------------------");
System.out.println(update.getUpdateKey() + ":" + update.getUpdateContent().getPerson().getFirstName() + " " + update.getUpdateContent().getPerson().getLastName() + "->" + update.getUpdateContent().getPerson().getCurrentStatus());
if (update.getUpdateComments() != null) {
System.out.println("Total comments fetched:" + update.getUpdateComments().getTotal());
for (UpdateComment comment : update.getUpdateComments().getUpdateCommentList()) {
System.out.println(comment.getPerson().getFirstName() + " " + comment.getPerson().getLastName() + "->" + comment.getComment());
}
}
}
Anyone have any thoughts on how to accomplish this using Coldfusion?
Thanks
I have not used that api, but I am guessing you could use the first two lines to grab the number of updates. Then use the overloaded client.getNetworkUpdates(start, end) method to retrieve the last update and obtain its key.
Totally untested, but something along these lines:
<cfscript>
...
// not sure about accessing the STATUS_UPDATE enum. One of these should work:
// method 1
STATUS_UPDATE = createObject("java", "com.google.code.linkedinapi.client.enumeration.NetworkUpdateType$STATUS_UPDATE");
// method 2
NetworkUpdateType = createObject("java", "com.google.code.linkedinapi.client.enumeration.NetworkUpdateType");
STATUS_UPDATE = NetworkUpdateType.valueOf("STATUS_UPDATE");
enumSet = createObject("java", "java.util.EnumSet");
network = yourClientObject.getNetworkUpdates(enumSet.of(STATUS_UPDATE));
numOfUpdates = network.getUpdates().getTotal();
// Add error handling in case numOfUpdates = 0
result = yourClientObject.getNetworkUpdates(numOfUpdates, numOfUpdates);
lastUpdate = result.getUpdates().getUpdateList().get(0);
key = lastUpdate.getUpdateKey();
</cfscript>
You can also use socialauth library to retrieve updates and post status on linkedin.
http://code.google.com/p/socialauth