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);
Related
Query query = new Query("Apple");
query.lang("en");
query.setCount(100);
query.setSince("2018-12-03");
query.setUntil("2018-12-04");
QueryResult result = twitter.search(query);
SentiWordNetDemoCode sentiwordnet = new SentiWordNetDemoCode();
for (Status tweet : result.getTweets()){
System.out.println(tweet.getCreatedAt());
}
When testing this, all the tweets are from 7:59 SRET. Is there any way to get a tweet from a time other than this?
According to the API reference on twitter, there are some restrictions to the query. There is no (more) since parameter, only a since_id which takes a status id. Then you can only search in the last 7 days. So the until field should not be older than that.
https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
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.
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());
}
}
}