Facebook API get shared friends likes with same app installed - facebook-graph-api

I need to get in some way the shared likes between friends using the same app, some FQL that finally return to me a list of most common likes, is it possible?
Thanks.

I've had a look at this, and to get the likes for a current user based off the indexable user_id field, you need a user_access token for the current user.
https://developers.facebook.com/docs/reference/fql/like/
What this basically means is,only the current user of your app at a particular time can query information based on their likes. Something like the following query might work for you. Be aware that you need the read_stream permission
select object_id, user_id,object_type FROM like WHERE object_id in(SELECT object_id FROM like WHERE user_id =me()) and user_id in(select uid FROM user WHERE uid in(SELECT uid2 FROM friend where uid1 = me()) and is_app_user='true')
you can try this out at
https://developers.facebook.com/tools/explorer/433871385166/?fql=select%20object_id%2C%20user_id%2Cobject_type%20FROM%20like%20WHERE%20object_id%20in(SELECT%20object_id%20FROM%20like%20WHERE%20user_id%20%3Dme())%20and%20user_id%20in(select%20uid%20FROM%20user%20WHERE%20uid%20in(SELECT%20uid2%20FROM%20friend%20where%20uid1%20%3D%20me())%20and%20is_app_user%3D'true')
You would then most likely need to use some server side code to analyse the returned data to count what is most popular. Possibly by creating an array, looping through the data, check if there is a key in the array based off the object_id, if there isn't add that key to the array with a value of 1 (where 1 is the count) otherwise increase the value by 1.
pseudo code example
$data; // This would be what was returned from your FQL query
$compare = array();
foreach($data as $value){
if(array_key_exists($value['object_id'],$compare)){
$compare['object_id'] = $value['object_id'] + 1;
}else{
$compare['object_id'] = 1;
}
}
// Do some sorting function to compare the counts.
//you would then probably need to batch queries to get the name/title of the object that the user has liked
I hope this gives you a good starting point

Related

How to avoid N+1 when counting in django

look at the following scenario:
I have an User model and an Address model that belongs to user.
In the user index, I need to show along with user's info how many addresses does the user have, but it's generating N+1 queries as everytime I call count it executes an additional query for that user id.
How can I do that? I read about select_related but I'm trying to make it in the reverse order...
In SQL it could be translated to:
SELECT user.*,
(SELECT count(*) FROM address WHERE address.user_id = user.id) AS address_count
FROM user
Is there a way to get the above SQL with django QuerySet?
You can annotate the number of addresses, you haven't shown your models but you can use the following on your queryset
.annotate(address_count=Count('address'))
User.objects.all().annotate(address_count=Count('address')) # Im guessing you want this
This would provide an address_count property on for each result
Docs for count

FQL query using Facebook Graph API

I have this query:
SELECT uid, name, current_location FROM user WHERE uid IN ( SELECT uid2 FROM friend WHERE uid1 = me() ) AND current_location.id IN (Select current_location.id from user where uid = me() )
It gives me results as required i-e friends only with a particular location.
Can i get the same results using Graph API instead? assuming I have the location id.
I tried this in graph API:
me/friends?location.id='111501225536407'&fields=location
Is something of ^ sort even possible using graph API?
Not that I can see. The Graph API is best for getting all of or the most recent items of an object. If you wanted to filter those friends by location, you would need to do that on the client side.
If you want Facebook to pre-filter your results on their servers, you need to use FQL.
you can execute fql queries (including multiqueries) through the graph api like this:
https://graph.facebook.com/fql?q={"albums":"select aid, name,cover_pid FROM album where owner=me()","album_covers":"select src from photo where pid in (select cover_pid from %23albums)"}&access_token=xxxxxx
It's a little tricky with the url encoding. Be sure that "#" sign used in multiqueries is encoded as %23

FQL does not give all the Friends likes

I am trying to retrieve all my friends' likes using FQL. But I am not getting all the likes only those pages which have "is_community_page" value true are being returned. Here's my FQL query
SELECT uid, page_id FROM page_fan WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
There is a max limit of 5000 results per FQL query that Facebook imposes before the results are trimmed for Privacy preferences of users. You can read this blog post about the same.
Facebook FQL retrieve a link to have full likes list and a samples list (quite unuseful), the point is not the limit of result, the point is fql has a different approach to list this data.
I stepped into it as well, trying to find a solution atm

Facebook FQL error when IN parameter exceeds 20

$fql = "SELECT link_id, owner, created_time, title, summary, url, image_urls FROM link WHERE owner IN (select uid2 from friend where uid1 = me() LIMIT 20) AND created_time >= $_7ago";
The above query works great. However, when I increase the LIMIT from 20 to, say 21 or anything higher, I get an error and the query returns Null. I am looking to query Links shared by ALL friends. Thanks.
That query takes forever to execute. Just testing it a few times in the Graph API explorer, even getting the links from 20 friends takes > 28 seconds. As I'm writing this, I've got a query running for 100 friends. We'll see if it completes before I do.
It looks like if you want to get all the links shared by all friends, you'll need to break this query up into smaller chunks and repeat it multiple times.
I might structure it like this to get your closest friend's links first, and then dig deeper for people who have less in common with you:
SELECT link_id, owner, created_time, title, summary, url, image_urls FROM link
WHERE owner IN (SELECT uid FROM user WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me())
ORDER BY mutual_friend_count LIMIT $offset, 20)
AND created_time >= $_7ago
You'd increment $offset in multiples of 20.
This is best done as an asynchronous query where you'll get the initial results quickly and others will fill in as they become available. It's going to take a LONG time to get all these results.
(100 Friends query still hasn't returned results after 5 mins.)
BTW, don't forget to mark your questions as resolved.

Search profiles by city in Facebook API

I need to find a profile in a specific town or nation, but I just
found a way to get profiles by checking locale column in my Facebook
query. Is it possible to retrieve a user id and link with town and
other location information, but is it not possible to retrieve
profiles by town?
Using the Facebook API, it is not possible to query by locale since it's not indexed (see: http://developers.facebook.com/docs/reference/fql/user/ and look for column names with a * star). According to the documentation the only columns you can search on with the API is uid, username, and name.
If you only want to query over the friends associated with the provided access token, you can do this FQL query:
query = {}
# first get friends which have provided a current location
query['friends'] = "SELECT uid, name, current_location FROM user \
WHERE uid IN (SELECT uid1 FROM friend WHERE uid2=me()) \
AND current_location"
# then get the latlongs corresponding to their current location
query['latlong'] = "SELECT page_id, latitude, longitude FROM place \
WHERE page_id IN (SELECT current_location.id FROM #friends)"
You can use this information to assign a latlong to each friend. The filtering must be done client-side ... I don't think there is another way.
NOTE: Of course this needs the friends_location permission.
Using the Graph API, you can search profiles using text place names like so:
https://graph.facebook.com/search?q=new%20york&type=user&access_token=???
(Replace ??? with your current access_token.)
Not clear to me exactly what profile text is matched against, so you may end up finding people that have a name similar to your search query or that are just talking about a place. Best to check the users specified location as a second filter. Hope that helps.