Here's my method for finding a hashtag:
void getNewTweets()
{
//try the search
try
{
Query query = new Query(searchString);
//get the last 50 tweets
query.count(2);
QueryResult result = twitter.search(query);
tweets = result.getTweets();
System.out.println(tweets);
}
//if there is an error then catch it and print it out
catch (TwitterException te)
{
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}
I'm using the Twitter4j library. How would I count the number of tweets found?
You can also use the QueryResult.getCount() function.
Query query = new Query(searchString);
QueryResult result = twitter.search(query);
int count = results.getCount();
System.out.println("tweet count: " + count);
Store the tweets in an ArrayList and fetch the size of the ArrayList to display the number of tweets.
ArrayList tweets = (ArrayList) result.getTweets();
System.out.println(tweets.size());
Related
Said that Twitter API will give tweets in 7 days, but when i tried to implement my code using java, i just get the result of tweets less than enough. It just covered tweets in 1 until 2 minutes. The number of data or tweets is far from my need. Is that any error on my code?
here my code:
public class TweetPublic {
public static void main(String[] args) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey("key")
.setOAuthConsumerSecret("key")
.setOAuthAccessToken("key")
.setOAuthAccessTokenSecret("key");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
FileOutputStream fos;
DataOutputStream dos;
try {
File file = new File("C:/Users/Charlie Siagian/Desktop/data.doc");
fos = new FileOutputStream(file);
dos = new DataOutputStream(fos);
try {
Query query = new Query("(#hadoop) OR (#bigdata) OR (#data) OR (#datamining) "
+ "OR (#mapreduce) OR (#flume) OR (#semanticanalytic) OR (#dataset) OR (#rdbms)"
+ "OR (#database) OR (#mysql)");
QueryResult result;
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("#" + tweet.getUser().getScreenName() + " - " + tweet.getText() + "\n=======\n"
+ tweet.getCreatedAt());
dos.writeBytes("#" + tweet.getUser().getScreenName() + " - " + tweet.getText() + "\n"
+ tweet.getCreatedAt() + "\n=======\n");
}
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
You have a popular query, so you are only getting the last 10 tweets. You can walk back in chunks of 100. Use "&max_id=ID&count=100"
So add an outer loop and remember the minimum id you have seen on previous iterations. For the first iteration, don't set max_id, and then keep updating max_id to the new minimum on subsequent iterations.
https://api.twitter.com/1.1/search/tweets.json?q=%23hadoop+OR+%23bigdata+OR+%23data+OR+%23datamining+OR+%23mapreduce+OR+%23flume+OR+%23semanticanalytic+OR+%23dataset+OR+%23rdbms+OR+%23database+OR+%23mysql&max_id=758939297495982100&count=100
Looks like that is available here http://twitter4j.org/javadoc/twitter4j/Query.html#setMaxId-long-
More general docs https://dev.twitter.com/rest/reference/get/search/tweets
Anyone know if it's possible to get ONLY the tweet that contain entities (Hashtag and Photos) with twitter4j ?
Here is my code, it works fine but I get all the tweet including the ones that don't have entities.
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(TwitterConstantes.APIKEY)
.setOAuthConsumerSecret(TwitterConstantes.APIKEYSECRET)
.setOAuthAccessToken(TwitterConstantes.TOKEN)
.setOAuthAccessTokenSecret(TwitterConstantes.TOKENSECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
double res = 5;
//Query query = new Query(this.text);
Query query = new Query().geoCode(new GeoLocation(latitude,longitude), res, Query.KILOMETERS.toString());
query.setResultType(Query.RECENT); // get the recent tweets
query.count(100);
QueryResult result = twitter.search(query);
do {
for (Status tweet : result.getTweets()) {
//if (tweet.isRetweet()){continue;}
try {
Status tweetById = twitter.showStatus(tweet.getId());
String url= "https://twitter.com/" + tweetById.getUser().getScreenName()
+ "/status/" + tweetById.getId();
List<String> hashtags = new ArrayList<String>();
HashtagEntity[] hashtagsEntities = tweetById.getHashtagEntities();
for (HashtagEntity hashtag : hashtagsEntities){
System.out.println(hashtag.getText());
}
ExtendedMediaEntity[] medias = tweetById.getExtendedMediaEntities();
for (ExtendedMediaEntity m : medias){
System.out.println(m.getMediaURL());
}
} catch (TwitterException e) {
System.err.print("Failed to search tweets: " + e.getMessage());
return;
}
}
query = result.nextQuery();
if(query!=null){
result = twitter.search(query);
}
}while(query!=null);
Hello guys I would ask you why I my code doesn't get me all the tweet I asked for in the query, and it's just stop next the first page result. I'm asking because the same code worked very well just six months ago.
Query query = new Query("Carolina OR flood lang:en since:2015-10-04 until:2015-10-09");
query.setCount(100);
QueryResult result;
createNewFile(contFile);
do {
result = twitterInstance.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
if (cont > MAX_TWEET_PER_FILE) {
cont = 1;
contFile++;
writer.close();
createNewFile(contFile);
}
writeToFile(cont,tweet);
cont++;
}
if(result.getRateLimitStatus().getRemaining()<1){
try {
Thread.sleep(result.getRateLimitStatus().getSecondsUntilReset() * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
} while (query!=null);
writer.flush();
writer.close();
System.exit(0);
So after the first iteration of do, query is always null, and what I gain is only the tweets about few hours of friday. I thought that I'm not wil be able to obtain tweets older than a week, but this is only a day (3 days ago...)
Are there any news or updates from Twitter guys I've missed?
Try using:
do{
...
}while((query = result.nextQuery()) != null);
Your query will get the results from the next page, if it exists.
I want all the list of tweets which are favorited by a twitter user account.
I have done some sample code that will give me all the posts that the user posted but i want all the tweets that the user favorited.
public List getAllTweetsOfUser(Twitter twitter, String user) {
if (user != null && !user.trim().isEmpty()) {
List statuses = new ArrayList();
int pageno = 1;
while (true) {
try {
int size = statuses.size();
Paging page = new Paging(pageno++, 100);
statuses.addAll(twitter.getUserTimeline(user, page));
if (statuses.size() == size) {
break;
}
} catch (TwitterException e) {
}
}
return statuses;
} else {
return null;
}
}
Can any one help me in this..
You need to start the paging with 1, and then increment the page. However, note that you will be rate limited, if you exceed 15 requests per 15 minutes (or 15* 20 = 300 statuses per 15 minutes).
Paging paging = new Paging(1);
List<Status> list;
do{
list = twitter.getFavorites(userID, paging);
for (Status s : list) {
//do stuff with s
System.out.println(s.getText());
}
paging.setPage(paging.getPage() + 1);
}while(list.size() > 0);
One of the Twitter4J samples does exactly this.
public final class GetFavorites {
/**
* Usage: java twitter4j.examples.favorite.GetFavorites
*
* #param args message
*/
public static void main(String[] args) {
try {
Twitter twitter = new TwitterFactory().getInstance();
List<Status> statuses = twitter.getFavorites();
for (Status status : statuses) {
System.out.println("#" + status.getUser().getScreenName() + " - " + status.getText());
}
System.out.println("done.");
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get favorites: " + te.getMessage());
System.exit(-1);
}
}
}
I have tried like below..
ResponseList<Status> status = twitter.getFavorites(twitterScreenName);
It given me the favorite tweets of the user which i have passed as a parameter. But the problem here is i am able to get only 20 favorites, though the user has so many tweets.
ResponseList<Status> status = twitter.getFavorites(twitterScreenName, paging);
I tried with the paging but i am not sure how to use this paging. So i am getting the top 20 favorites using my first code. If anybody tried this then please share the info like how to get all favorites of a given user.
I'm working on the REST API of Twitter using Twitter4J libraries, particularly on the https://api.twitter.com/1.1/search/tweets.json endpoint. I am quite aware of Twitter's own Streaming API, but I don't want to use that (at least for now). I have a method that queries the /search/tweets endpoint by a do-while loop, but I want the method's return to be in streaming fashion, so that I can print the results in the console simultaneously, instead of loading everything all at once. Here's the method:
public List<Status> readTweets(String inputQuery) {
List<Status> tweets = new ArrayList<Status>();
int counter = 0;
try {
RateLimitStatus rateLimit = twitter.getRateLimitStatus().get("/search/tweets");
int limit = rateLimit.getLimit();
Query query = new Query(inputQuery);
QueryResult result;
do {
result = twitter.search(query);
tweets.addAll(result.getTweets());
counter++;
} while ((query = result.nextQuery()) != null && counter < (limit - 1));
} catch (TwitterException e) {
e.printStackTrace();
System.out.println("Failed to search tweets: " + e.getMessage());
tweets = null;
}
return tweets;
}
What can you suggest?
P.S. I don't want to put the console printing functionality inside this method.
Thanks.