Anyone knows why setCount() is not working in twitter4j? - java

Hello i am trying to Just retrieve 1 tweet using Twitter4j but the setCount() method is doing what ever it wants ( maybe its just me doing something wrong ) i leave my code below.
I have tried with multiple options "1", "2","0" and regardless the number it retrieves any amount of tweets from 3 to 10.
ConfigurationBuilder cf = new ConfigurationBuilder();
cf.setDebugEnabled(true)
.setOAuthConsumerKey("xxx")
.setOAuthConsumerSecret("xxxx")
.setOAuthAccessToken("xxxx")
.setOAuthAccessTokenSecret("xxxx");
TwitterFactory tf = new TwitterFactory(cf.build());
twitter4j.Twitter twitter = tf.getInstance();
try {
Query query = new Query("sverige");
QueryResult result;
do {
query.setCount(2);
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("#" + tweet.getUser().getScreenName() + tweet.getFavoriteCount() + tweet.getUser().getName());
}
} 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);
}

You are getting all the tweets from that user, at 2 results per page. So if they had 22 tweets, you would get 11 pages of 2 tweets per page.
query.setCount(2); // set the number of tweets per page
// get the next page until there are no more pages
} while ((query = result.nextQuery()) != null);

The loop will continue to query for tweets while more tweets are available.
This is because
(query = result.nextQuery()) != null is true until all of the tweets (matching the query) are read.
setCount only limits the amount of tweets each search operation returns.
WHen debugging a situation like this (if you don't want to look into the source code / documentation) you can test how many times the outer loop occurs.

Related

How to get a list of retweeters of a retweet?

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.

How to get user geolocation in Twitter4j?

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.

Twitter4j how can i get any twitter handle tweets from it's 1'st tweets

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

Using twitter4j to search through more than 100 queries [duplicate]

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.

Extract all the tweets by giving date as the parameter

I want to extract all the tweets of the week using twitter4j lib in java. I tried doing it like below
try {
Query query = new Query("since:2013-04-01&until:2013-04-08");
QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("#" + tweet.getUser().getScreenName() + " - " + tweet.getText());
}
} while ((query = result.nextQuery()) != null);
System.exit(0);
But, I ended up with an error message
403:The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits (https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following).
message - Missing or invalid url parameter
code - 195
So, how do i get the tweets by giving dates as parameters? Thanks
I think the below snippet should answer your question (Date in YYYY-MM-DD format)
Query query = new Query("#sad");
query.lang("en");
query.setSince("2006-01-01");
query.setUntil("2013-12-28");
QueryResult result = twitter.search(query);

Categories

Resources