Get age of friend on Facebook with Ruby via rFacebook - ruby-on-rails

I am creating a Facebook app and need to access people's ages - only the friends of the user, not the general public.
I am using rFacebook version 0.6.2, setup along the line of this.
I need to get the ages/ birthdays of all of my friends.

As per http://rfacebook.rubyforge.org/ rFacebook isnt being maintained, it suggested Facebooker, but even Facebooker hasn't been updated in months: https://github.com/mmangino/facebooker
I suggest Koala (and not just because I'm an Aussie). Have a read at: https://github.com/arsduo/koala There's also details on setting Koala up on Rails: https://github.com/arsduo/koala/wiki/Koala-on-Rails
I just built a FB app using Koala and a Custom Tab Page last week and it was very quick.
You also need to read: http://developers.facebook.com/docs/authentication/ (pay attention to the mention of scopes and permission levels). As per: http://developers.facebook.com/docs/authentication/permissions/ you must request 'friends_birthday' when you request your scope.
I'm not sure there is an easy way to get all your friends' birthdays in a batch. You might have to traverse each friend and get their info.
As a test, go to: http://developers.facebook.com/docs/reference/api/ and click on the friends link. Then copy the ID of your first friend. In the URL replace '/me/friends' with the ID you copied. Eg: https://graph.facebook.com/me/friends?access_token=ABC123 becomes https://graph.facebook.com/12345678?access_token=ABC123 You will then see the data of that friend, one field of which is birthday.
#i have already asked for user permissions, and have my access token
#https://github.com/arsduo/koala/wiki/OAuth
graph = Koala::Facebook::GraphAPI.new(oauth_access_token)
friends = graph.get_connections("me", "friends")
friends.each do |f|
friend = graph.get_object(f['id'])
puts "#{f['name']} has a birthday on #{friend["birthday"]}"
end
Although, you might be able to use FQL to do a batch.
#FQL taken from http://stackoverflow.com/questions/5063431/easiest-way-to-get-birthday-info-of-all-friends-thorugh-graph-api
fql = "select uid,name,birthday_date from user where uid in (select uid2 from friend where uid1=me())"
#https://github.com/arsduo/koala/wiki/REST-API
#rest = Koala::Facebook::GraphAndRestAPI.new(oauth_access_token)
birthdays = #rest.fql_query(fql)
Good luck!

FQL is probably the way to go here as is mentioned above.
A couple notes:
Not all of your friends will have a birthday accessible (either because they restricted that information, or because they didn't post it). If you only want data for your friends with accessible birthdays, you can add a "and birthday_date" to your where clause.
That query will not return all data, but only the first 100 or so. If you want to get all of them, you will need to request them one page at a time. You can do this by adding a "limit 0,50" clause, to request 50 rows, starting at the 0th one.

Related

Get all liked pages sorted by general likes

I'm trying to get Pages liked by the user ordered descending by amount of likes each Page has...
It's difficult to get this using Graph API cause I'd have to fetch request like this:
let request = FBSDKGraphRequest(graphPath: "me/likes" parameters: nil)
and recursively call this inside because this request will paginate response. After I get everything I'll have to sort it locally and that's how I'd get it 😬
IMHO, it's a lil bit overkill so I've looked into a method of achieving same thing but using FQL and this is the query:
SELECT name, fan_count FROM page WHERE page_id IN (SELECT page_id FROM page_fan WHERE uid = me()) ORDER BY fan_count DESC
At first I was happy with this but after some test my friend told me that he can't see Messi on his list. So I wonder what's the reason that not all Pages are show in this FQL query result?
You don’t have to make separate requests for this.
The Graph API has a feature called “field expansion”, that allows you to specify that you want data from multiple “levels” in one go. https://developers.facebook.com/docs/graph-api/using-graph-api/v2.4#fieldexpansion
So requesting
/me/likes?fields=id,name,likes
will give you the id, name and number of likes for each of the user’s liked pages.
(You will still have to follow the pagination links, gather all results and do the sorting on your end afterwards, since the API doesn’t currently allow for sorting.)
FQL is deprecated and only works with older Apps using v2.0 of the Graph API. As of now, the only way to do this is to recursively get all Pages and do the sorting on your own.

Facebook API Get Number Of Friends For Userid

I am trying to get the number of friends for a userid, I can see that this userid is a friend but how do I get there total number of friends?
I have tried using the /{user-id}/friends endpoint but I get a privilege error. Using /{user-id-a}/friends/{user-id-b} I just get my friend data.
You need to use /{user_id}/friends. Therefore you need to gather the user_friends permission during the login dialog of your app.
See
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/friends/
The response for the call will contain a field summary.total_count with the total number of friends this user_id has. Keep in mind that only the friends which are also using the same app will be returned in the friends data array. If you just want the total count, the info returned should be sufficient.

How to retrieve all public images (posts) from a facebook search by hashtag in rails?

I have to create an application, which retrieves images by a given facebook hashtag, and stores data in database (with the post, hashtags, and the image)
Is this possible? I have no idea where to start. Do I need omniauth for this, or if it's public, it works without it.
The processing should run in a rake task, not in a browser.
You probably want to use the Facebook Graph API – there's a gem for that called Koala. You can filter the feed with a FQL query. Here's some untested Ruby code, may need some tweaking:
require 'koala'
oauth_access_token = '...'
tag = 'yolo'
query = <<-FQL
SELECT pid
FROM photo
WHERE pid IN (
SELECT pid
FROM photo_tag
WHERE text='#{tag}'
)
FQL
api = Koala::Facebook::API.new(oauth_access_token)
api.batch do |batch_api|
batch_api.fql_query(query).each do |photo|
picture = api.get_picture(photo['pid'])
# do something with picture
end
end
See the Koala docs for more information on how to get the oauth_access_token and the FQL Reference for more information on queries.

How can I get all tweets relating to a specific user in Twitter?

I am new to the Twitter API and I'm having an issue with the user_timeline API.
I am using the following REST query:
https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=twitterapi&count=50
which is provides the user's timeline data, however it only gives the user's tweets; I want all tweets by and about the user (i.e. the user's tweets, and mentions of the user by the user's followers).
Thanks in advance!
You can access this by searching for the user's # handle. This will return tweets which mention #user and also tweets by #user.
Twitter API - Search
--
I've no experience about formatting for JSON calls but the following should be enough:
https://api.twitter.com/1.1/search/tweets.json?q=%40ataulm
The %40 is for the # symbol, and ataulm is the user name you wish to query. See the page linked for default values to the other parameters - this will, for example, only return 15 tweets per "page" (not sure what a page refers to), but can be set to a maximum of 100 per page, using the count parameter.
"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser.'&count=500'
BUt it is giving only 200 records.

How would I retrieve the latest 'x' number of posts from multiple Facebook users with the Koala gem?

I'm looking to be able to return a JSON list of posts from a set of Facebook users with the Koala gem either through FQL or the Graph API. Currently I'm using the batch function of Koala to make a call for the first 'x' number of items in from each user feed and then flattening the array and sorting by time. The issue with this is that I would like to be able to get the first 'x' number of items where it doesn't get 10 from each user but simply the 10 most recent posts from the users I queried so I can retrieve the next 10 from the feed akin to what you would expect from your own home feed.
This is what I currently have:
#oauth = Koala::Facebook::OAuth.new
#access_token = #oauth.get_app_access_token
#graph = Koala::Facebook::API.new(#access_token)
artists = []
followings.each do |following|
artists << following.artist
end
feed = #graph.batch do |batch_api|
artists.each do |artist|
feed | batch_api.get_connections(artist.facebook, "feed", {"limit" => "10"})
end
end
feed.flatten!
feed.sort! { |x, y| y["created_time"] <=> x["created_time"] }
EDIT
I believe I've found one possible solution with FQL as follows
batch_api.fql_query("SELECT post_id, source_id, likes, actor_id, message, type FROM stream WHERE source_id IN(artist1, artist2...)")
But I am now returned the error
[#<Koala::Facebook::APIError: OAuthException: (#606) Queries for multiple source_ids require a non-zero viewer that has granted read_stream permission>]
The issue with this is that my application does not require someone to login with Facebook and so I cannot request a read_stream permission. Is there anyway the Facebook app itself can request this?
It should work, but there is a FB bug described here:
I think that there is nothing to do on your part to solve it.

Resources