I'm using the koala gem as show in Railscasts episode #361. I'm attempting to get all of the comments of a given Post but Facebook only seems to be giving me back the last 50 comments on the post. Is this a limitation of Facebook's Graph API or am I doing something wrong?
fb = Koala::Facebook::API.new oauth_token
post = fb.get_object(id_of_the_post)
comments = fb.get_object(post['id'])['comments']['data']
puts comments.size # prints 50
Graph API paginates the result when is a larger number of posts than the limit that is set (in your case 50).
In order to access the next page of results, call "next_page" method:
comments = fb.get_object(post['id'])
while comments['comments']['data'].present?
# Make operations with your results
comments = comments.next_page
end
Also, by looking in source one can see that "get_object" method receives 3 parameters:
def get_object(id, args = {}, options = {})
This way, you can raise your posts per page to as many posts as you want:
comments = fb.get_object(post['id'], {:limit => 1000})
Related
I have aprox 520 classrooms archived in my account, if I try to select them with
var courseList = Classroom.Courses.list({"courseStates":["ARCHIVED"]}).courses;
I get only 300 of them. Is this normal?
How can I select them all? Actually I'm writing a script to delete the oldest, but if I can't retrieve them, I can't delete them.
I understand that you got so many courses that the Courses.list() response is splitted in separate pages. In that case you can very easily navigate them by using tokens. First of all, make sure that you specify the pageSize in your request. That would set the desired amount of responses per page. Please keep in mind that the server may return fewer than the specified number of results, as it declared on the docs. In case that your response got divided into pages, the response would include the nextPageToken field. Then, to obtain the rest of courses, you have to repeat your request including that nextPageToken into the pageToken property. Please don't hesitate to ask me any doubt about this approach.
Thanks a lot Jaques, I found the solution:
var parametri = {"courseStates": "ARCHIVED"};
var page = Classroom.Courses.list(parametri);
var listaClassi = page.courses;
if (page.nextPageToken !== '') {
parametri.pageToken = page.nextPageToken;
page = Classroom.Courses.list(parametri);
listaClassi = listaClassi.concat(page.courses);
}
Anyway, I didn't need to change the pageSize, nor I found any tutorial about it.
I was playing around with the Knowledge Graph API recently, but can't really get it to return anything useful.
My scenario is this: I query the API for maxim gorki theater, and it rightly returns the entry for the theater in Berlin. Now, on the web, I can click Kommende Veranstaltungen [Upcoming events] and it gives me the next plays for that theater. This is still part of the knowledge graph, the displayed search term is maxim gorki theater kommende veranstaltungen. Yet if I try to pass that query to the API to get a JSON of the next plays I am simply returned an empty result.
My query is this: https://kgsearch.googleapis.com/v1/entities:search?query=QUERY&key=API_KEY&limit=1&indent=True
Am I doing this wrong or is this a general limitation of the API? If so, what is the point of this API?
I'm using the code from https://developers.google.com/knowledge-graph/, It works well
service_url = 'https://kgsearch.googleapis.com/v1/entities:search'
params = {
'query': query,
'limit': 10,
'indent': True,
'key': api_key,
}
url = service_url + '?' + urllib.urlencode(params)
response = json.loads(urllib2.urlopen(url).read())
Sorry for a strange question.
I'm an admin of a very useful Facebook group. There is a lot of valuable info, which I'd like to have offline. Is there any (cli) method to download it?
You could use online services like Sociographand Grytics to get data and even export them(I tried sociograph).
If you want to download the data yourself, then you need to build a program that gets the data for you through the graph api and from there you can do whatever you want with the data you get.
Here is a simple I hacked in python to get the data from a facebook group.
Using this SDK
#!/usr/bin/env python3
import requests
import facebook
from collections import Counter
graph = facebook.GraphAPI(access_token='fb_access_token', version='2.7', timeout=2.00)
posts = []
post = graph.get_object(id='{group-id}/feed') #graph api endpoint...group-id/feed
group_data = (post['data'])
all_posts = []
"""
Get all posts in the group.
"""
def get_posts(data=[]):
for obj in data:
if 'message' in obj:
print(obj['message'])
all_posts.append(obj['message'])
"""
return the total number of times each word appears in the posts
"""
def get_word_count(all_posts):
all_posts = ''.join(all_posts)
all_posts = all_posts.split()
for word in all_posts:
print(Counter(word))
print(Counter(all_posts).most_common(5)) #5 most common words
"""
return number of posts made in the group
"""
def posts_count(data):
return len(data)
get_posts(group_data)
get_word_count(all_posts)
Basically using the graph-api you can get all the info you need about the group such as likes on each post, who liked what, number of videos, photos etc and make your deductions from there.
I googled but couldn't find a bash script for this.
I'm currently trying to pull data via the Yelp API, and can only seem to access half of the hash.
This code:
client = Yelp::Client.new
include Yelp::V2::Search::Request
request = Location.new(
:city => 'Melbourne',
:limit => 1) response = client.search(request)
puts response
Will output the full hash of
{"region"=>{"span"=>{"latitude_delta"=>0.0, "longitude_delta"=>0.0},
"center"=>{"latitude"=>28.0772451, "longitude"=>-80.6045478}},
"total"=>2324, "businesses"=>[{"is_claimed"=>false, "rating"=>4.5,
"mobile_url"=>"http://m.yelp.com/biz/el-ambia-cubano-melbourne",
"rating_img_url"=>"http://s3-media2.fl.yelpassets.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png",
"review_count"=>168, "name"=>"El Ambia Cubano",
"snippet_image_url"=>"http://s3-media1.fl.yelpassets.com/photo/NgfGcZGdYlhTO18p8Shqrw/ms.jpg",
"rating_img_url_small"=>"http://s3-media2.fl.yelpassets.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png",
"url"=>"http://www.yelp.com/biz/el-ambia-cubano-melbourne",
"phone"=>"3213278389", "snippet_text"=>"4.5 stars to me - rounded up
because the kids liked it too.\n\nWent here for lunch based mostly on
yelp reviews. Rest of my crew voted against Indian or Thai....",
"image_url"=>"http://s3-media3.fl.yelpassets.com/bphoto/pnZSlPiBDl1bS9w7saOAZA/ms.jpg",
"categories"=>[["Cuban", "cuban"]],
"display_phone"=>"+1-321-327-8389",
"rating_img_url_large"=>"http://s3-media4.fl.yelpassets.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png",
"id"=>"el-ambia-cubano-melbourne", "is_closed"=>false,
"location"=>{"city"=>"Melbourne", "display_address"=>["950 E Melbourne
Ave", "Melbourne, FL 32901"], "geo_accuracy"=>8.0,
"postal_code"=>"32901", "country_code"=>"US", "address"=>["950 E
Melbourne Ave"], "coordinate"=>{"latitude"=>28.0771809,
"longitude"=>-80.6044922}, "state_code"=>"FL"}}]}
I can access the region info by using
puts response["region"]
But I can't seem to access the rest of the hash? I'm specifically trying to extract the business name. What am I missing and need to do to access the entire hash?
businesses seems to be an array. Something like
response["businesses"][0]["name"]
Will retrive the name of the first business
"The SoundCloud API will be dropping support for offset-based pagination on March 2, 2015, in favor of linked partitioning.
To page through a JSON response, pass the linked_partitioning=1 parameter along with your request and it will return a collection, along with a next_href property if there are additional results. To fetch the next page of results, simply follow that URI. If the response does not contain a next_href property, you have reached the end of the results."
I'm trying to do this in ruby with the soundcloud api. I know max results are 200, but if I search through 500 I will literally see "next_href" in the response. Is it possible to continue the search? How do I do it if its possible?
When I try calling it, I end up getting a syntax error ":undefined method `title'" or just not the requested amount of results.
When you request a collection of resources (eg: search results, stream tracks), if you don't get the full collection back again, in the response is a field called next_href. This is a url pointing to the next page of the same collection. Each subsequent page will give you a next_href until you reach the end and none will exist. As an example:
/users/2/tracks.json?limit=10
{
"collection": [ { id: 123, title: ... }, { id: 456 ... }, ... ],
"next_href": "https://api.soundcloud.com/users/2/tracks.json?limit=10&offset=10"
}
I don't quite know what you mean here:
When I try calling it, I end up getting a syntax error ":undefined method `title'" or just not the requested amount of results.
That sounds like a bug in your ruby code.