Get all liked pages sorted by general likes - ios

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.

Related

Twitter gem: how to avoid searching deeply with max_id?

I want my app to search tweets with a specific #tag on Twitter every few minutes, like this:
results = client.search("#mypopulartag")
However, I don't want to do a full search each time. In building the app, I've encountered the Twitter::TooManyRequests error, because it returns a lot of results (presumably the Twitter gem makes as many requests to Twitter as needed for one client.search() call).
I don't need it to search super deep each time. Can I pass in the max_id parameter to the client.search method, so I don't waste API calls?
Yes, if you keep track of the id of the most recent tweet that you've processed, you can get all tweets since then with something like this (using gem version 5.13):
client.search(
"#mypopulartag",
result_type: 'recent',
since_id: since_id # your last processed id
).take(15)
Just keep in mind that if there are, say, 60 results, you'll need to execute more client.search calls to get all the tweets. For those calls, you'd want to also specify a max_id equal to the last tweet id that was processed in the current search.

AngularJS scoped rails query

I just started learning AngularJS integrated with a Rails backend. I'm confused about where scoped query logic belongs.
I have simple blog with a list of posts and two links which should sort the posts by "newest" and "most voted".
With angular, where is this scope logic? I see there is a 'filter' option on ng-repeat, but it seems inefficient to query Post.all in my index action if there are thousands of posts and then use filter to show the top 10 most voted?
I'd appreciate a simple explanation of what should take place from the point of a user clicking the "most voted" link" to interacting with the back-end API to properly scope the query.
I would add a param order_by in the request to PostsController. Using this param you can decide how to order posts fetched from the database and the send then back to angular.
It's worth noting two things:
Don't use Post.all because it's inefficient to fetch all the records to the application and even worse to send all of them to the client. Use pagination: fetch a certain number of posts. You can add pagination easily using gems like kaminari or will_paginate
Remember not to put params[:order_by] value directly as a Post.order() argument, because it might cause SQL injection. It's mentioned in this Railscast

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.

Instagram gem: how to get next page of results?

How do I get the next page of results using the Instagram gem?
The documentation for the API itself says there is a pagination hash passed in with each result (see here: http://instagram.com/developer/endpoints/). How do I access that with the gem?
Got it. I don't think the pagination hash that Instagram passes back is accessible, but you can pass in a max_id option when querying, to get the next set of older pictures.
#results = Instagram.user_recent_media(some_user_id, {access_token: token, count: 10, max_id: 197679035065553721})
By passing in max_id (the id of a photo), it will return all results older than that. So grab the id of the oldest photo from the first query, and pass it in to get the next page.
Note: when you get the results, the ids of pictures are in the form: 197679035065553721_someuserid. You have to parse out the first bit before the underscore, and pass that in as max_id.
Instagram.tag_recent_media(params[:q]).pagination
run that code and you will see the pagination attributes.
I have looked into this for a project of my own and eventually found that you can get the client to give you the pagination info by setting no_response_wrapper to true when creating the client:
client = Instagram.client(:access_token => accesstoken,
:no_response_wrapper => true)
This makes it so you can use .pagination on the response as Yigit C. Bacakoglu suggested.
You may be out of luck. If you look through the client modules, you can see that the methods are returning the data field of the response, so the pagination field is unavailable. There's also an issue that touches on the lack of pagination info in responses.

Get age of friend on Facebook with Ruby via rFacebook

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.

Resources