I have a problem using Twitter4j. I need to recover a tweet using its id but when I search, the result it's always [ ]. Here is the code:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("XXXXXXXXX")
.setOAuthConsumerSecret("XXXXXXXX")
.setOAuthAccessToken("XXXXXXXX")
.setOAuthAccessTokenSecret("XXXXXXXXX");
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
Query searchTweet = new Query("id:twitter4j " + Long.parseLong("427552735219965952"));
QueryResult resultSearchTweet = twitter.search(searchTweet);
System.out.println("Value: " + resultSearchTweet);
System.out.println("Tweets: " + resultSearchTweet.getTweets());
System.out.println("Tweets size: " + resultSearchTweet.getTweets().size());
And here is the exit:
Value: QueryResultJSONImpl{sinceId=0, maxId=603858195988131841, refreshUrl='?since_id=603858195988131841&q=id%3Atwitter4j%20427552735219965952&include_entities=1', count=15, completedIn=0.028, query='id:twitter4j 427552735219965952', tweets=[]}
Tweets: []
Tweets size: 0
Two observations:
1) I need to use search function because it has more rate limit than
other functions.
2) The tweet exists.
What am I doing wrong?
Thanks in advance and sorry for my english :).
I just found a solution. I search the tweets that a certain user using an URL of the tweet then I access to the JSON and get what I want
Thank you very much :).
Related
I want to get tweets from certain user timelines using java library twitter4j, currently I have source code which can get ~ 3200 tweets from user time line but I can't get full tweet. I have searched in various sources on the internet but I can't find a solution to my problem. anyone can help me or can anyone provide an alternative to get a full tweet from the user timeline with java programming?
my source code :
public static void main(String[] args) throws SQLException {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("aaa")
.setOAuthConsumerSecret("aaa")
.setOAuthAccessToken("aaa")
.setOAuthAccessTokenSecret("aaa");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
int pageno = 1;
String user = "indtravel";
List statuses = new ArrayList();
while (true) {
try {
int size = statuses.size();
Paging page = new Paging(pageno++, 100);
statuses.addAll(twitter.getUserTimeline(user, page));
System.out.println("***********************************************");
System.out.println("Gathered " + twitter.getUserTimeline(user, page).size() + " tweets");
//get status dan user
for (Status status: twitter.getUserTimeline(user, page)) {
//System.out.println("*********Place Tweets :**************\npalce country :"+status.getPlace().getCountry()+"\nplace full name :"+status.getPlace().getFullName()+"\nplace name :"+status.getPlace().getName()+"\nplace id :"+status.getPlace().getId()+"\nplace tipe :"+status.getPlace().getPlaceType()+"\nplace addres :"+status.getPlace().getStreetAddress());
System.out.println("["+(no++)+".] "+"Status id : "+status.getId());
System.out.println("id user : "+status.getUser().getId());
System.out.println("Length status : "+status.getText().length());
System.out.println("#" + status.getUser().getScreenName() +" . "+status.getCreatedAt()+ " : "+status.getUser().getName()+"--------"+status.getText());
System.out.println("url :"+status.getUser().getURL());
System.out.println("Lang :"+status.getLang());
}
if (statuses.size() == size)
break;
}catch(TwitterException e) {
e.printStackTrace();
}
}
System.out.println("Total: "+statuses.size());
}
Update :
After the answer given by #AndyPiper
the my problem is every tweet that I get will be truncated or not complete. a tweets that I get will be truncated if the length of tweet more than 140 characters. I found the reference tweet_mode=extended, but I do not know how to use it. if you know something please tell me.
Your Configuration should be like this:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("aaa")
.setOAuthConsumerSecret("aaa")
.setOAuthAccessToken("aaa")
.setOAuthAccessTokenSecret("aaa")
.setTweetModeExtended(true);
It is explained well here
The Twitter API limits the timeline history to 3200 Tweets. To get more than that you would need to use the (commercial) premium or enterprise APIs to search for Tweets by a specific user.
if you are streaming tweets
fist: you have to add .setTweetModeExtended(true); into your configurationbuilder
second(here is the code)
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener(){
public void onStatus(Status status) {
System.out.println("-------------------------------");
if(status.isRetweet()){
System.out.println(status.getRetweetedStatus().getText());
}
else{
System.out.println(status.getText());
}`
its totally works for me.
take care yourself :)
If you are implementing twitter api using twitter4j.properties file and getting truncated timeline text value,simply add the below property in it at the end.
tweetModeExtended=TRUE
This is my FQL. What it does is it receives the post based on post_id, and also returns if post was made by user or page. User or page is returned based on id of the one who posted the post, which is returned by first FQLQuery.
StringBuilder fqlQuery = new StringBuilder();
fqlQuery.append("SELECT "
+ FacebookGraphApi.FQL_STREAM_SELECT
+ " FROM stream WHERE post_id=\"");
fqlQuery.append(rmtId);
fqlQuery.append("\"");
HashMap<String, String> querys = new HashMap<String, String>();
querys.put("messages", fqlQuery.toString());
querys.put(
"users",
"SELECT "
+ FacebookGraphApi.FQL_USER_SELECT
+ " FROM user WHERE uid IN (SELECT actor_id FROM #messages)");
querys.put(
"pages",
"SELECT "
+ FacebookGraphApi.FQL_PAGE_SELECT
+ " FROM page WHERE page_id IN (SELECT actor_id FROM #messages)");
Now what i would like to do is the same using RESTFB Batch Request API
BatchRequest firstRequest = new BatchRequestBuilder(rmtId)).build();
BatchRequest secondRequest = new BatchRequestBuilder(poster_id).build();
List<BatchResponse> batchResponses =
facebookClient.executeBatch(firstRequest,secondRequest);
These are 2 batches that executes, first one returns the post, and it contains "from", that contains poster_id of the one who posted, but i dont know how to put that poster_id from first batch to second batch so it would return poster information.
Any help would be appreciated, thanks.
Just to post the answer,
first request should contain a name method on which u depend on the other request, and search on second request that depends on first ids like this.
?ids={result=name-of-method:$.data.id}
I'm using twitter4j version 2.2.5. setPage() doesn't seem to work if I use it with setSince() and setUntil(). See the following code:
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
AccessToken accessToken = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
twitter.setOAuthAccessToken(accessToken);
int page = 1;
while(true) {
Query query = new Query("from:someUser");
query.setSince("2012-01-01");
query.setUntil("2012-07-05");
query.setRpp(100);
query.setPage(page++);
QueryResult qr = twitter.search(query);
List<twitter4j.Tweet> qrTweets = qr.getTweets();
if(qr.getTweets().size() == 0) break;
for(twitter4j.Tweet t : qrTweets) {
System.out.println(t.getId() + " " + t.getText());
}
}
The code inside the loop is only executed once if I use setSince() and setUntil() methods. But without them, setPage() seems to work and I get more tweet results.
Any ideas why this is happening?
Your code appears to be working for me. It only returns tweets from the past nine days, but that's expected (approximately) according to your comment.
You mentioned in the question that the loop only ran once with rpp=100, and you said in a comment that all 80 of the user's tweets were returned when rpp=40. I think that would indicate that the code is working as expected. If the user only has 80 tweets, the loop only should run once when rpp=100, and it should execute twice and display all their tweets when rpp=40. Try displaying the page number as soon as you enter the while loop so you can see how many times the loop runs.
Hello i want to display a user timeline in a list with twitter4j, because im new to java i just don't know how to do that. I searched the web but couldn't find anything usefull. So my question is: does someone know how to display a user timeline in a list in android?
twitter4j-core-2.2.6
Twitter tw = new TwitterFactory().getInstance();
// some userlist
UserList userList = tw.getUserLists("screen name", -1).get(0);
System.out.println(userList.getName());
int listId = userList.getId();
ResponseList<Status> userListStatuses = tw.getUserListStatuses(listId, new Paging(1));
for (Status status : userListStatuses) {
System.out.println(status.getText());
}
As per the twitter4j Code Examples
Twitter twitter = new TwitterFactory().getInstance();
List<Status> statuses = twitter.getFriendsTimeline();
System.out.println("Showing friends timeline.");
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":" +
status.getText());
}
Please check official document and code example for twitter4j
I am using twitter4j for searching and displaying tweets. Here is the code snippet
Query query = new Query(queryString);
QueryResult result = twitter.search(query);
for (Tweet tweet : result.getTweets()) {
System.out.pritnln(tweet.getFromUser() + ":" + tweet.getText());
}
Here tweet.getText() returns plain text without anchor tag (plain text not html) even if it has hashtag and url to some website. How can I get twitter text with anchor tag as you see in twitter search result on twitter?
You need to use the EntitySupport methods to get hashtags and URLs from each tweet. The URLEntity and HashtagEntity classes both have methods that tell you where the entity starts and ends in the text of the tweet, so you can use that information to insert link markup when you display the tweet.
Example code that extracts the URLs from recent tweets:
Twitter twitter = new TwitterFactory().getInstance();
Query query = new Query("lizardbill");
QueryResult result = twitter.search(query);
for (Tweet tweet : result.getTweets()) {
System.out.println(tweet.getFromUser() + ":" + tweet.getText());
for (URLEntity urle : tweet.getURLEntities()) {
System.out.println(urle.getDisplayURL());
}
}
Use twitter-text-java. It matches the same syntax that Twitter uses.