Twitter List twitter4j - java

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

Related

how to get the full tweet of the user timeline with twitter4j?

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

How to post in a group on facebook using restfb?

I am trying to post a message onto a facebook group (I am the admin for the page). Here is the java code that I am using:
public void makeTestPost() {
fbClient = new DefaultFacebookClient(groupPageAccessToken);
counter = 0;
fbClient.publish(groupID + "/posts", FacebookType.class, Parameter.with("message", Integer.toString(counter) + ": Hello, fb World!"));
counter++;
}
with:
private final string groupPageAccessToken = "XXXXXXXXXXXXXXXXXX";
private final String groupID = "XXXXXXX";
I got these values using the facebook graph explorer api online Graph API Explorer
But when I login to facebook I don't see any message/post on the group. Please tell me how to make it work?
You need an user access token with the permissions publish_actions and user_managed_groups.
Then you can publish a new message like this:
GraphResponse response = fbClient.publish(groupID + "/feed", GraphResponse.class, Parameter.with("message", Integer.toString(counter) + ": Hello, fb World!"));

Cannot get user details from post in restfb using java

Recently I've been struggling with getting user information from post_id with java. I'm new in restfb, but after reasearching, below code should work. All available permissions are granted. Even in Graph API Explorer when writing post_id I cannot retrieve post's author details.
This is how I do it:
FacebookClient facebookClient = new DefaultFacebookClient(token);
String command = "orangepolska/feed";
Connection<Post> pagePosts = facebookClient.fetchConnection(command, Post.class);
ArrayList<String> postList = new ArrayList<String>();
String row;
for( List<Post> posts : pagePosts){
for (Post post : posts) {
if (post.getCreatedTime().after(startDate) && post.getCreatedTime().before(endDate)){
String message = post.getMessage();
CategorizedFacebookType postedBy = post.getFrom();
Post.Comments comments = post.getComments();
row = " owner: "+postedBy.getName()+" owner_id: "+postedBy.getId()+" post: "+message+" + " likes: "+post.getLikesCount() + "\n";
System.out.println(row);
postList.add(row);
}
}
}
return postList;
The problem occurs with various of functions like: getName(), getID(), getLikesCount() etc - these return null.
How can i fix it?
Thanks in advance.
You need to fetch the feed with the fields parameter so Facebook knows which fields you need to be filled. RestFB can only provide access to information that are given by Facebook ;)
Have a look here: http://restfb.com/#selecting-specific-fields
Norbert is correct but the link he posted did not work for me - you need to include "Parameter.with("fields", "from")" to get the user information.
Connection<Post> pagePosts = facebookClient.fetchConnection(command, Post.class, Parameter.with("fields", "from"));

Searching for a tweet using Twitter4j

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 :).

How do twitter4j Paging pages work?

I'm working on writing a simple program to fetch tweets for an information retrieval class (later I will be learning to index and search through them). For now, I just want to gather data. I'm using the twitter4j API and am having trouble understanding the Paging class and how it works. Below is a snippet of code:
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Paging paging = new Paging(2,40);
try{
List<Status> statuses = twitter.getUserTimeline("google", paging);
System.out.println(paging);
for(Status status : statuses)
{
System.out.println(status.getText());
}
System.out.println("\n\n\n");
paging.setPage(2);
statuses = twitter.getUserTimeline("google",paging);
for(Status status : statuses)
{
System.out.println(status.getText());
}
}
catch(TwitterException e){
e.printStackTrace();
}
My hope was that the second call to twitter.getUserTimeline would return the next 40 tweets from Google as this example seems to suggest, but upon inspection, both return the same 40 tweets. Can someone then explain what the Paging class actually does?
Your first call should request the first page Paging paging = new Paging(1, 40);, not the second one.

Categories