How pagination works in twitter gem? - ruby-on-rails

I have been integrated a twitter feed into my Rails 4 application using this gem.
Able to fetched twitter home timeline feeds using following twitter gem method
Method:
twitter.home_timeline
and this will return last 20 tweets of home_timeline
To fetch first page tweets, i have added following options to the method which will return first 200 tweets in page 1.
Method:
twitter.home_timeline(:page => 1, :count => 200)
Here, everytime i have to provide page number manually like :page => 2, :page => 3,..and so on to fetch next page tweets.
So, is their any method or way to get total page counts for twitter home time tweets using twitter gem?

As mentioned in the official documentation:
Note: This method can only return up to 800 Tweets, including
retweets.
Meaning that you will never be able to get tweets greater than page 4 (while using count as 200)

To answer the question in headline, gem simply makes a call and pass all provided parameters, including page and count, to twitter API - see source
def home_timeline(options = {})
perform_get_with_objects('/1.1/statuses/home_timeline.json', options, Twitter::Tweet)
end
So whatever logic you get is logic from the twitter API. For API, you can read official doc here. Paging is additionally explained here. As indicated in other answer, there is a limit for statuses/home_timeline call:
Up to 800 Tweets are obtainable on the home timeline. It is more
volatile for users that follow many users or follow users who tweet
frequently.
Meanwhile, you can test the API https://dev.twitter.com/rest/tools/console - was helpful link for me

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.

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.

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.

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