facebook graph api comment list sort , like 'orderby=desc'? - facebook

I use graph api to get the picture's comments, but I want to first sort the results by creating time and then return to the latest data. Similar to the sql statement 'order by create_time desc', I do not know if have such a parameter.
Currently used to offset and limit access to the latest data, but also know the total number of comments,
pagesize = 25;
offset = comments.count - pagesize;
limit = 25;
url = "https://graph.facebook.com/" + object_id + "/comments?access_token=" + access_token + "&limit=" + limit + "&offset=" + limit;
next page:
offset -= 25
but comments.ount of numerical sometimes is not accurate
and the result of the request URL to return to sometimes don't match
Whether to have very good solution
Or I used the wrong way (‘limit’ and ‘offset’ Parameter)!!!
Thank you for your answer.
"Graphics API" the existence of the cache?
i post a message and 46 comments.requests url, set the parameters:
offset=0&limit=1
Then it should return to the last comment (latest one), the actual return to the middle of a comment, and I tested a few times, set the
offset and limit. According to the returned results, the middle one is
the latest comment
If I set the limit value is greater than the 'comment.count', the returned data is all, the official website and facebook consistent
Because the cache reason?
Thanks again~

#dbau - You are still better off using FQL. In my experience, unless you are making a very simple call, you have very little control over what you get via a Graph API call.
Why don't you want to use FQL? FQL is an endpoint of the Graph API. There is still some data that can only be returned via FQL.
This will get you the result you're looking for. The query needs to be URL encoded. I left it in plain text for clarity.
https://graph.facebook.com/fql?access_token=[TOKEN]&q=
SELECT id, fromid, text, time, likes, user_likes FROM comment
WHERE object_id = [OBJECT_ID] ORDER BY time DESC LIMIT 0,[N]
You may find you don't get [N] comments returned each time, because Facebook filters out items that are not visible to the access_token owner after the query is run. You could either up the LIMIT and filter out any excess results returned or if you are using a user access_token, you could add AND can_like = TRUE to the WHERE clause to be guaranteed that, if they exist, [N] posts visible to the current user are returned.

Graph API returns latest objects first.
Facebook provides 2 keywords to filter the fetched data.
Limit : Returns "limit" number of latest records
Offset : Returns "limit" number of records from the offset position
So to retrieve latest "x" comments posted for an object
https://graph.facebook.com/[OBJECTID]?limit=[X]&offset=0
To retrieve next "X" comments (page wise)
https://graph.facebook.com/[OBJECTID]?limit=[X]&offset=[X*PAGENo]
Hope the answer is clear enough for you.

Related

getting full number of comments of facebook status in API v2.6

How to get number of all comments (number of status comment + number of comments of comments) without looping over every comment?
This parameters show only number of direct comments of status, without nested comments
?fields=comments.summary(true).limit(0)
How to do it similarly to FQL?
FQL requests have no problem with it
SELECT id,likes,post_fbid,time,fromid,text,text_tags,parent_id FROM
comment WHERE post_id = %post_id%
it returns all comments (nested or not) as is. easy to count and easy to check of something changed
Found an answer: you should use filter = stream
likes this
?fields=comments.summary(true).filter(stream).limit(0)
It's in the official documentation, but was not very obvious to me, that it can be used in object endpoint and not only in object/comments.
https://developers.facebook.com/docs/graph-api/reference/v2.6/object/comments#readmodifiers

How to implement cursors for pagination in an api

This is similar to to this question which doesn't have any answers. I've read all about how to use cursors with the twitter, facebook, and disqus api's and also this article about how disqus generally built their cursors, but I still cannot seem to grok the concept of how they work and how to implement a similar solution in my own projects. Can someone explain specifically the different techniques and concepts behind them?
Lets first understand why offset pagination fails for large data sets with an example.
Clients provide two parameters limit for number of results and offset and for page offset.
For example, with offset = 40, limit = 20, we can tell the database to return the next 20 items, skipping the first 40.
Drawbacks:
Using LIMIT OFFSET doesn’t scale well for large
datasets. As the offset increases the farther you go within the
dataset, the database still has to read up to offset + count rows
from disk, before discarding the offset and only returning count
rows.
If items are being written to the dataset at a high frequency, the
page window becomes unreliable, potentially skipping or returning
duplicate results.
How Cursors solve this ?
Cursor-based pagination works by returning a pointer to a specific item in the dataset. On subsequent requests, the server returns results after the given pointer.
We will use parameters next_cursor along with limit as the parameters provided by client in this case.
Let’s assume we want to paginate from the most recent user to the oldest user.When client request for the first time , suppose we select the first page through query:
SELECT * FROM users
WHERE team_id = %team_id
ORDER BY id DESC
LIMIT %limit
Where limit is equal to limit plus one, to fetch one more result than the count specified by the client. The extra result isn’t returned in the result set, but we use the ID of the value as the next_cursor.
The response from the server would be:
{
"users": [...],
"next_cursor": "1234", # the user id of the extra result
}
The client would then provide next_cursor as cursor in the second request.
SELECT * FROM users
WHERE team_id = %team_id
AND id <= %cursor
ORDER BY id DESC
LIMIT %limit
With this, we’ve addressed the drawbacks of offset based pagination:
Instead of the window being calculated from scratch on each request based on the total number of items, we’re always fetching the next count rows after a specific reference point. If items are being written to the dataset at a high frequency, the overall position of the cursor in the set might change, but the pagination window adjusts accordingly.
This will scale well for large datasets. We’re using a WHERE clause to fetch rows with id values less than the last id from the previous page. This lets us leverage the index on the column and the database doesn’t have to read any rows that we’ve already seen.
For detailed explanation you can visit this wonderful engineering article from slack!
Here is an article about pagination: paginating-real-time-data-cursor-based-pagination
Cursors – we need to have at least one column with unique sequential values to implement cursor based pagination. This can be similar to Twitter’s max_id parameter or Facebook’s after parameter.
In general you should pass the current item or page number in the request as a param. Other usual param is the batch size of the page. Then on the server side backend you select and return the proper dataset, with an SQL query for example.
enter image description hereHere's what I am Done with. The cursor is working as a pointer and it points to that index. and limit will pick that many rows from that pointer. Let's say we have given id 10 and limit 5 then it will go to id 10 and pick 5 elements from there.
Some Graph API connections uses cursors by default. You can use 'limit' and 'before'/'after' parameters in your call. If you are still not clear, you can post your code here and I can explain with it.

Photo tags query response of max 400 elements

i'm using new Facebook SDK 3.0 but it's not relevant since Graph API Explorer has the same behavior.
The problem is that when i try to fetch some user photo tags, like "me/photos" or the equivalent FQL query, i get a response of max 400 elements, using either graph or fql.
I already tried using LIMIT field (for example the FQL query is "SELECT src, src_big FROM photo WHERE pid IN (SELECT pid FROM photo_tag WHERE subject=me() LIMIT 1000) LIMIT 1000")
but nothing change.
I also tried with SINCE and UNTIL, but i understood that FB returns a table of 400 rows and then shrink it according to your query.
Is maybe another way to bypass this limit or for some unknown reason FB wanted it and it's not a bug?
Thank you

FQL query for finding public event

I would like to execute an FQL query for retrieving all the public events in a specific area (using longitude,latitude and maximum distance or simply location name). Do you know if it's possible?
Apparently, somebody is able to do it, somehow: http://elmcity.info/fb_events?location=tokyo
It is possible indeed to receive events using location based "search"
To do so you'll need the longitude and latitude coordinates of the location you want to search and a access_token with user_events permission (i think you could also use the public search)
Here's an FQL example how can you get all the events nearby of a location. (this searches from your and your friends events):
$lat = "40";
$long = "30";
// using offset gives us a "square" on the map from where to search the events
$offset = 0.4;
$events = 'SELECT pic_big, name, venue, location, start_time, eid FROM event WHERE eid IN (SELECT eid FROM event_member WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND start_time > '. $created_time .' OR uid = me()) AND start_time > '. $created_time .' AND venue.longitude < \''. ($long+$offset) .'\' AND venue.latitude < \''. ($lat+$offset) .'\' AND venue.longitude > \''. ($long-$offset) .'\' AND venue.latitude > \''. ($lat-$offset) .'\' ORDER BY start_time ASC '. $limit;
The trick itself lies in the venue.longitude and venue.latitude useage. To get events from a city, just get the city coordinates and adjust the offset to your needs.
If you don't know how to use FQL please look into Facebook PHP SDK
I'm wrestling with this same problem right now. It does not look possible. There are several problems I see:
the location fields are not searchable in the page or place tables,
the way event locations are populated is inconsistent: See this page's events for an example. (I personally populated most of these. These are FB changes, not user error.),
FQL does not have an AS statement to query based on the result of a calculation, and
Facebook limits the amount of items returned by a query to some fairly low value.
What the referenced site seems to do is query event.description for the presence of the input string. Query "IKEA", and you'll get lots of results. Definitely not a geo search. (BTW, the site's source code is on GitHub).
Edit:
Okay, I was wrong. Facebook does expose a search method, but only in the Graph API. Using the batch request functionality, you could execute a series of requests that:
Find all place entries within a distance (in meters) of a known lat/long. The graph api accepts q=* for the search string to return all places.
Query event to find all events at the places returned above.
I'm going to play with this. I'll update this post again as I flesh out this code better.
try this code worked for me returns all details of the upcoming public event which contains a specific key word that can be your location
SELECT eid,name,description,venue,creator,location,ticket_uri,start_time, eid,host,not_replied_count,pic_square,pic_big,unsure_count,attending_count,declined_count,all_members_count FROM event WHERE start_time > now() AND CONTAINS("your location name") AND privacy='open'
I believe that in http://elmcity.info/fb_events?location=tokyo they are not really searching for events in Tokyo, they simply executing a query like this
https://graph.facebook.com/search?type=event&q=tokyo
(access token is needed, see https://developers.facebook.com/tools/explorer?method=GET&path=search%3Ftype%3Devent%26q%3Dhaifa)
Note that this url also works
http://elmcity.info/fb_events?location=sport
I'm also looking for a way to retrieve public events using fql and so far I didn't find any.

How to get (better) demographics for fans of a Facebook page?

I'm trying to get demographics for fans of a page on Facebook - mostly country and city, but age and gender as secondary.
The primary way to do it is using FQL and doing a query in the insights table. Like so:
FB.api({
method: 'fql.query',
query: "SELECT metric, value FROM insights WHERE object_id='288162265211' AND metric='page_fans_city' AND end_time=end_time_date('2011-04-16') AND period=period('lifetime')"
}, callback);
The problem with this, however, is that the table returns a maximum of 19 records only, both for the country and the city stats. The response for a page I'm testing is as such:
[
{
"metric": "page_fans_city",
"value": {
"dallas": "12345",
"atlanta": "12340",
(...)
"miami": "12300"
}
}
]
So I'd like to know if there's any alternative to that -- to get demographics of the current fans of a page (no snapshot necessary).
Things I've tried:
Using LIMIT and OFFSET on the query do nothing (other than, sometimes, give me an empty list).
One alternative that has been discussed in the past is to use the "/members" method from the Graph API (more here) to get a list of all users, and then parse through that list. That simply doesn't work - a method exists, and it may have worked in the past, but it's not valid anymore (disabled?).
Request:
https://graph.facebook.com/platform/members?access_token=...
Response:
{"error":
{
"type":"OAuthException",
"message":"(#604) Your statement is not indexable. The WHERE clause must contain an indexable column. Such columns are marked with * in the tables linked from http:\/\/developers.facebook.com\/docs\/reference\/fql "
}}
Other solution was to do a query to the page_fan table and filtering by page_id. This doesn't work, either; it may have worked in the past, but now it says that the page_id column is not indexable therefore it cannot be used (same error as above, which leads me to believe /members uses the same internal API that has been disabled). Page_fan query is only useful to check if individual users are fans of a page.
There's also the like table, but that's only useful for Facebook items (like posts, photos, links, etc), and not Facebook Pages.
Going to the insights website about the Page, you can see the data in some nice graphs and tables, and download an Excel/CSV spreadsheet with the historic demographics data... however, it also limits the data to 19 entries (sometimes 20 with a few holes in there as cities trade top positions though).
Any other hint on how to get that data? I'd either like the insights query with more results, or at least a way to get all the page fans so I could do the location query myself later (even if the page I want to get it from has almost 5 million fans... gulp).
The data pipeline for this metric is currently limited to 20 items. This is a popular feature request and something Facebook hopes to improve soon.