Filtering graph results - facebook-graph-api

Using the following query:
/me/posts?fields=story,type,created_time,privacy,message,picture,comments.summary(1).limit(100){created_time,from.id,like_count,message},likes.summary(1).limit(100){id}
i am getting posts from a user and some related data; this works fine.
However, i would like to get only status updates (type = status) with a non-empty Message field.
As a quick fix i have solved this programatically, but i find this crude. Is there a better way?

Related

Too many queries when preparing test data on salesforce

I occured some 'Too many SOQL queries: 101' when deploying my apex code to production. After some investigation, I found that was caused by the queries in the Triggers of User object and Account object. When I prepare some test data and insert them into database, the triggers work and query for many times. A sample of my test code is like this(This is just a sample, actually there are more objects I need to prepare). Besides that, all the queries in my Trigger are not redundant, so I can't change my Trigger to reduce queries.
#isTest(SeeAllData=true)
static void testMethod() {
User user = testFixture.prepareUser();
insert user; //57 queries
Account acc = testFixture.prepareAccount();
insert acc; //50 queries
Test.startTest();
//my test code
Test.stopTest();
}
So my question is how can I avoid this error? I already searched a lot and I don't have any query in loop. Already tried to use the 'startTest' to reset the query counter, but it still not solve my problem. I wonder if there is a method I can disable the triggers when prepare data or any other way to solve my problem?
I tried my best to express my problem, if it is still not understandable, please tell me.
Any ideas are appreciated.

Musicbrainz querying artist and release

I am trying to get an artist and their albums. So reading this page https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2 i created the following query to get Michael Jackson's albums
http://musicbrainz.org/ws/2/artist/?query=artist:michael%20jackson?inc=releases+recordings
My understanding is to add ?inc=releases+recordings at the end of the URL which should return Michael Jackson's albums however this doesnt seem to return the correct results or i cant seem to narrow down the results? I then thought to use the {MBID} but again thats not returned in the artists query (which is why im trying to use inc in my query)
http://musicbrainz.org/ws/2/artist/?query=artist:michael%20jackson
Can anyone suggest where im going wrong with this?
You're not searching for the correct Entity. What you want is to get the discography, not artist's infos. Additionally, query fields syntax is not correct (you must use Lucene Search Syntax).
Here is what you're looking for:
http://musicbrainz.org/ws/2/release-group/?query=artist:"michael jackson" AND primarytype:"album"
We're targeting the release-group entity to get the albums, searching for a specific artist and filtering the results to limit them to albums. (accepted values are: album, single, ep, other)
There are more options to fit your needs, for example you can filter the type of albums using the secondarytype parameter. Here is the query to retrieve only live albums:
http://musicbrainz.org/ws/2/release-group/?query=artist:"michael jackson" AND primarytype:"album" AND secondarytype="live"
Here is the doc:
https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2/Search
Note that to be able to use MB's API you need to understand how it is structured, especially, the relations between release_group, release and medium.

jsonpath: mix two paths

I am querying facebook for fullsize profilepictures of users that commented on posted pictures or who like posted pictures by the /me user, using the graph api.
I am trying to either:
do it in one query, where i am having trouble getting data from deep enough.
or minimize the amount of subqueries
In one query: this does not work (it does not give details of the users that commented):
me/photos/uploaded?fields=from,images,comments.picture,likes.picture&limit=500
and specifying the dimensions throws an error:
me/photos/uploaded?fields=from,images,comments.picture,likes.picture.type(large)&limit=500
There seems to be a limit as to how deep you can specify, though I havent found anything explicit in the documentation. (which is so terse that is hardly a big surprise)
so, resolving to batched queries with a dependency
myphotos => me/photos/uploaded?fields=created_time,name,id,from,comments.summary(1).limit(100),images,likes.summary(1).limit(100)&limit=500
userinfo_photocomments => ?ids={result=myphotos:$.data.*.comments.data.*.from.id}&fields=id,name,updated_time,picture.redirect(0).type(large).height(2000).width(2000)
userinfo_photolikes => ?ids={result=myphotos:$.data.*.likes.data.*.id}&fields=id,name,updated_time,picture.redirect(0).type(large).height(2000).width(2000)
though this works, the fact that i cant find how to do it in one or max 2 queries annoys me, especially as the results of the last 2 subqueries will most likely overlap.
I would like to find a way to mix the jsonpath anchors, such that i can combine the last two queries.
The problem seems to be that the userid's of the commentors and the likers are on different depths in the trees.
I would be gratefull for any insights.
I think you should be able to use the result from the first query twice, resulting in only two queries.
I tested the ?ids= endpoint if it filters out duplicate user_id like the following:
GET /?ids={user_id1},{user_id2},{user_id1}
which results in only getting two distinct user_ids back.
So, this yields in the following batch query:
curl \
-F 'access_token={access_token}' \
-F 'batch=[{ "method":"GET","name":"myphotos","relative_url":"me/photos/uploaded?fields=created_time,name,id,from,comments.summary(1).limit(100),images,likes.summary(1).limit(100)&limit=500",},{"method":"GET","relative_url":"?ids={result=myphotos:$.data.*.comments.data.*.from.id},{result=myphotos:$.data.*.likes.data.*.id}&fields=id,name,updated_time,picture.redirect(0).type(large).height(2000).width(2000)"}]' \
https://graph.facebook.com/
Just replace {access_token} with an actual access token to test this.
Basically, I just concatenated the user_id lists for comments and likes as
GET ?ids={result=myphotos:$.data.*.comments.data.*.from.id},{result=myphotos:$.data.*.likes.data.*.id}&fields=...

Find model returns undefined when trying to get the attribute of a model by first finding the model by another attribute?

I would like to do something like:
App.Model.find({unique_attribute_a: 'foo'}).objectAt(0).get('attribute_b')`
basically first finding a model by its unique attribute that is NOT its ID, then getting another attribute of that model. (objectAt(0) is used because find by attribute returns a RecordArray.)
The problem is App.Model.find({unique_attribute_a: 'foo'}).objectAt(0) is always undefined. I don't know why.
Please see the problem in the jsbin.
It looks like you want to use a filter rather than a find (or in this case a findQuery). Example here: http://jsbin.com/iwiruw/438
App.Model.find({ unique_attribute_a: 'foo' }) converts the query to an ajax query string:
/model?unique_attribute_a=foo
Ember data expects your server to return a filtered response. Ember Data then loads this response into an ImmutableArray and makes no assumption about what you were trying to find, it just knows the server returned something that matched your query and groups that result into a non-changable array (you can still modify the record, just not the array).
App.Model.filtler on the other hand just filters the local store based on your filter function. It does have one "magical" side affect where it will do App.Model.find behind the scenes if there are no models in the store although I am not sure if this is intended.
Typically I avoid filters as it can have some performance issues with large data sets and ember data. A filter must materialize every record which can be slow if you have thousands of records
Someone on irc gave me this answer. Then I modified it to make it work completely. Basically I should have used filtered.
App.Office.filter( function(e){return e.get('unique_attribute_a') == 'foo'}).objectAt(0)
Then I can get the attribute like:
App.Office.filter( function(e){return e.get('unique_attribute_a') == 'foo'}).objectAt(0).get('attribute_b')
See the code in jsbin.
Does anyone know WHY filter works but find doesn't? They both return RecordArrays.

Information of re-shared status

I am working with Facebook graph api for few days. I am trying to extract user's status and the information of reshared if any. I can easily find status of a user using fields=id,name,statuses query. But I could not find any information about re-sharing. I found a field of status sharedposts. But could not understand what it actually does. Can anyone enlighten me about how can I collect information about resharing (who reshared,when reshared,resharing location). I used user_status access token.
The sharedposts field applies to a status id. For example, the status id 10151794781777494 is from a status update by the TheKrazyCouponLady which has been shared 4 times. This query:
/10151794781777494?fields=sharedposts
Will return all the information about the users that have shared it. If you want to limit the returned fields to the name and id of the sharer, and the time and location it was shared, you could do this:
/10151794781777494?fields=sharedposts.fields(from,created_time,place)
Although I expect there won't be any location data most of the time.
To find the status id in the first place, you could just query the statuses field for a particular user. Again, using TheKrazyCouponLady (uid 255919387493) as an example:
/255919387493?fields=statuses
To get just the ids:
/255919387493?fields=statuses.fields(id)
As an alternative to that, you may want to consider querying the user's posts instead. The advantage to using posts, is that you can get back the share count for each post in that query.
/255919387493?fields=posts.fields(id,shares)
If the share count on a post is zero, then there is obviously no need to run another query to retrieve the users that have shared that post.
The downside of using posts is that the post id is slightly different from a status id. You'll see ids that look like this:
255919387493_10151794781777494
The first half of that string is the user id of the post owner. The second half is the actual status id. If you want to query the sharedposts field for the post, you first have to extract the second half (the status id) and use that for the query.
Having said that, it occurs to me that you could actually retrieve all the information you need in one go if you chain the statuses query and the sharedposts query together. For example, something like this:
/255919387493?fields=statuses.fields(id,message,sharedposts.fields(from,created_time,place))
That will return the status id and message text for each status from that user, and the user details, create time and location for each person that shared each of those statuses.
Even with paging, though, that is likely to be a fairly slow query, so I'm not sure if that's such a good idea. It's worth considering though.
According new version of API 2.1 and documentation from here
https://developers.facebook.com/docs/graph-api/reference/v2.1/post
there is a new edge called "sharedposts"
As described here https://developers.facebook.com/docs/graph-api/reference/v2.1/object/sharedposts
This reference describes the /sharedposts edge that is common to
multiple Graph API nodes. The structure and operations are the same
for each node.
This edge represents any posts where the original object was shared on
Facebook.
If the post type is photo sharedposts will return empty as the object is different to the postID
/317380948302131_847979698575584 => Object : 847979378575616
/317380948302131_847979698575584/sharedposts?fields=from,via
ObjectID will work as expected
/847979378575616//sharedposts?fields=from,via
The only problem if the object is a shared_post it will show all shares from the original post object too and no via node is present .
Just struggle around some time why the APi only sometimes return sharedposts