How to extract on the public page user posts on restfb? - java

I want to extract just user Posts using RestFB but it seems there is no way to do it.
/{page-id}/feed - gives all posts Page owner + users
/{page-id}/posts - gives all the owner posts
But i want just user posts, any way ?
Connection<Post> postsPages = facebookClient.fetchConnection(page + "/feed",Post.class,
Parameter.with("fields", "id,name,message,story,link,description,created_time,likes.limit(10).summary(true),"
+ "comments.limit(10).summary(true),shares"),
// add comments (comments.limit(10000).summary(true){id,message}
Parameter.with("until", "now"),
Parameter.with("since", START_DATE),
Parameter.with("limit", 100));
List<Post> posts = new ArrayList<Post>();

Related

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"));

Restfb - Using the Batch Request API

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}

RestFB: can't get page posts when page's URI has page's name

I'm here again to ask for your help.
I'm building a simple application that gets all the comments from a page that I liked. It works very well, but during some tests I got HTTP Status 500 as exception.
The problem occours when the URI of the pase has the name of the page. Let me explain with two examples:
WORKING URI: https://www.facebook.com/pages/ULTRAS-NAPOLI/101174668938?fref=ts
NOT WORKING URI: https://www.facebook.com/FASTWEB?fref=ts
As you can see the difference between the two URIs is that 1. has page's ID, and 2. has page's name!
Here's my code:
public LinkedList<String> writeOnePageComments(String id){
LinkedList<String> s = new LinkedList<String>();
Connection<Post> page = fb.fetchConnection(id.trim()+"/posts", Post.class,Parameter.with("limit",999));
List<Post> pagePosts = page.getData();
List<Comment> commentList;
Comments comments;
for(int i=0;i<pagePosts.size();i++){
Post p=pagePosts.get(i);
comments=p.getComments();
if(comments!=null){
commentList = comments.getData();
if(!commentList.isEmpty())
for(int k=0;k<commentList.size();k++){
String tmp = commentList.get(k).getMessage();
tmp = f.subEmoticons(tmp);
tmp = f.removeRepeatedVocals(tmp);
s.add(tmp);
}
}
}
return s;
Any help? I'm going CRAZY! :)
SOLVED:
Connection<Post> page = fb.fetchConnection(id.trim()+"/posts", Post.class,Parameter.with("limit",999));
Should be changed to:
Connection<Post> page = fb.fetchConnection(id.trim()+"/feed", Post.class,Parameter.with("limit",999));
According to Facebook Graph API Documentation, "feed" means: "The feed of posts (including status updates) and links published by this page, or by others on this page. " , while "/posts" shows only the posts that were published by this page.

Get an array of user photos using FQL

I'm to get a list of the users photos (one's they've been tagged in) using FQL.
Basically I've go an array object like so: _imageAddressArray.
I can retrieve the users' photos using graphApi so I know it works, problem with graphAPi is that it's too slow (+15 seconds min for 100 photos).
So far I've got:
//New Stuff
FQL fql = new FQL(facebook);
String FQLResult = null;
try
{
_userGallery = graphApi.getPhotosMy(_noOfPhotos);
FQLResult = fql.fqlQuery("SELECT object_id, src_small FROM photo");
}
catch (EasyFacebookError e)
{
e.printStackTrace();
}
System.out.println("FQL Result" + FQLResult);
This returns the error: 601, any ideas anyone?
Of course ideally FQLResult will be a string[] (string array)
You're getting an error because you don't have a WHERE clause in your FQL statement that references one of the indexed columns -- shown with a "*" here
To get the photos using FQL that your user has been tagged in, try this:
SELECT object_id, src_small FROM photo WHERE object_id IN
(SELECT object_id FROM photo_tag WHERE subject = me())

Twitter List twitter4j

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

Categories