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.
Related
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.
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.
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
I am using Twitter4j to get tweets from the user's I'm following. Getting 1000 per time, but I'm a bit stuck on how I would include user ID and username in the out put.
Here is the code I'm using in order to get the tweets:
try {
ResponseList<Status> a = twitter.getHomeTimeline(new Paging(1,1000));
for (Status b: a){
System.out.println(b.getText());
}
}
Does anybody know what I'd have to add in order to output the ID, Username and then the Tweet?
Thanks
Z19
You can get the id and user name using following methods.
User user = b.getUser() --> Return the user associated with the status.
then using user.getId() and user.getName() you can get the id and user name.
try {
ResponseList<Status> a = twitter.getHomeTimeline(new Paging(1,1000));
for (Status b: a){
long userId = b.getUser().getId();// user Id
String userName = b.getUser().getName(); // user name
String tweetText = b.getText(); // tweet
System.out.println(userId+" "+userName+" "+tweetText);
}
}
For more info you can refer following links:
Twitter 4j Status
Twitter 4j User
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);