Facebook graph API: get the "subcomments" in a tree of a discussion - facebook-graph-api

I am trying to download a Facebook discussion using the graph API. The problem is: the discussion is located in a page, and in a tree-style manner, meaning that there are two types of comments: "main" comments, to the first message, and "subcomments" to the main comments themselves.
It seems that the graph result only shows the "main" comments and doesn't show the subcomments. Here's an example of a comment it returns:
{
"id": "53526364352_1574091",
"can_remove": false,
"created_time": "2014-02-05T10:46:37+0000",
"from": {
"name": "Main commenter",
"id": "5345353"
},
"like_count": 163,
"message": "I am a main comment",
"user_likes": false
},
There is no link or whatever to the subcomments of this main comment (and there are many).
Is there a way to get the subcomments?

If 10101140614002197_8831228 is an ID of a root comment, then you can check for subcomments/replies by requesting COMMENT_ID/comments.
For example:
the root comment: http://graph.facebook.com/10101140614002197_8831228
the subcomment: http://graph.facebook.com/10101140614002197_8831228/comments
this root comment has no subcomments so the data list is empty: https://graph.facebook.com/10101140614002197_8831286/comments

You can use field expansion (curly parenthesis in url) to get nested data
http://graph.facebook.com/{object-id}/comments?fields=id,message,comments{id,message,comments{id,message,comments}}
More info here in the section labeled Nested requests (a.k.a. field expansion).

If you want to traverse and flatten the tree, you can do this:
def get_all_comments(post_or_comment_id):
next_ids = [post_or_comment_id]
results = []
while next_ids:
next_id = next_ids.pop()
comments = get_comments_from_facebook(next_id) # Facebook API call
results += comments
next_ids.extend(c["id"] for c in comments)
return results
Make sure to add parent to the API call so you can replicate the tree.

Related

How to find plurals with Google Cloud Natural Language API

The Google Cloud Natural Language API can be used to analyse text and return a syntactic parse tree with each word labeled with parts-of-speech tags.
Is there a way to deturmine if a noun is plural or not?
If Google Cloud NL is able to work out the lemma then perhaps the information is there but not returned through the API?
Update
With the NL API's GA launch, the annotateText endpoint now returns a number key for each token indicating whether word is singular, plural, or dual. For the sentence "There are some cats here," the API returns the following token data for 'cats' (notice that number is PLURAL):
{
"text": {
"content": "cats",
"beginOffset": -1
},
"partOfSpeech": {
"tag": "NOUN",
"aspect": "ASPECT_UNKNOWN",
"case": "CASE_UNKNOWN",
"form": "FORM_UNKNOWN",
"gender": "GENDER_UNKNOWN",
"mood": "MOOD_UNKNOWN",
"number": "PLURAL",
"person": "PERSON_UNKNOWN",
"proper": "PROPER_UNKNOWN",
"reciprocity": "RECIPROCITY_UNKNOWN",
"tense": "TENSE_UNKNOWN",
"voice": "VOICE_UNKNOWN"
},
"dependencyEdge": {
"headTokenIndex": 1,
"label": "DOBJ"
},
"lemma": "cat"
}
See the full documentation here.
Thanks for trying out the NL API.
Right now there isn't a clean way to detect plurals other than to note that the base word is different than the lemma and guess whether it's plural (in English, perhaps it ends in an -s).
However, we plan to release a much better way of detecting morphological information like plurality, so stay tuned.

How can I use scan/scroll with pagination and sort in ElasticSearch?

I have a ES DB storing history records from a process I run every day. Because I want to show only 20 records per page in the history (order by date), I was using pagination (size + from_) combined scroll, which worked just fine. But when I wanted to used sort in the query it didn't work. So I found that scroll with sort don't work. Looking for another alternative I tried the ES helper scan which works fine for scrolling and sorting the results, but with this solution pagination doesn't seem to work, which I don't understand why since the API says that scan sends all the parameters to the underlying search function. So my question is if there is any method to combine the three options.
Thanks,
Ruben
When using the elasticsearch.helpers.scan function, you need to pass preserve_order=True to enable sorting.
(Tested using elasticsearch==7.5.1)
yes, you can combine scroll with sort, but, when you can sort string, you will need change the mapping for it works fine, Documentation Here
In order to sort on a string field, that field should contain one term
only: the whole not_analyzed string. But of course we still need the
field to be analyzed in order to be able to query it as full text.
The naive approach to indexing the same string in two ways would be to
include two separate fields in the document: one that is analyzed for
searching, and one that is not_analyzed for sorting.
"tweet": {
"type": "string",
"analyzer": "english",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
The main tweet field is just the same as before: an analyzed full-text field.
The new tweet.raw subfield is not_analyzed.
Now, or at least as soon as we have reindexed our data, we can use the
tweet field for search and the tweet.raw field for sorting:
GET /_search
{
"query": {
"match": {
"tweet": "elasticsearch"
}
},
"sort": "tweet.raw"
}

loopbackjs: how to get specifics parents filtering related model

like a common joined relation i would like to filter my results based on filtered children in a parent-child relation.
i did this
{"include":{"relation":"children","scope":{"where":{"and":[{"name":"xxx"}]}}}}
that gave me ALL parents and inside them, children filtered by that name.
any clues? thanks
ref:
https://docs.strongloop.com/display/public/LB/Querying+related+models#Queryingrelatedmodels-Usingfiltersparameterswithincludedrelations
As per https://stackoverflow.com/a/32933383/344022 there's nothing officially available yet. There is a fork of the loopback-connector by #DiogoDoreto that does attempt to provide it. I haven't tried it, but if you were to use it you would do the following in a filter:
"where": {
"children": {
"where": {
"name": "xxx"
}
}
}

Adding Targets to Target Lists using REST API with SugarCRM

I'm trying to add targets to target lists in Sugar via REST service calls. I'm getting a positive response from Sugar but records are not added. The service method I'm using is *set_relationship*:
{
"session":"3ece4lmn5rtweq9vm5581jht",
"module_name":"ProspectLists",
"module_id":"cb13b96f-8334-733c-1548-52c27a5b8b99",
"link_field_name":"prospects",
"name_value_list":[],
"related_ids":["534f894a-4265-143d-c94b-52be908685b1"],
"delete":0
}
I also tried it the other way around:
{
"session":"3ece4lmn5rtweq9vm5581jht",
"module_name":"Prospects",
"module_id":"cb13b96f-8334-733c-1548-52c27a5b8b99",
"link_field_name":"prospect_lists",
"name_value_list":[],
"related_ids":["534f894a-4265-143d-c94b-52be908685b1"],
"delete":0
}
In both cases I get a promising response:
{"created":1,"failed":0,"deleted":0}
...but when I check the target list I can't find any added targets. I also checked the database but there is no trace either.
My Sugar Version is 6.5.16 CE and I'm using the SuiteCRM 7.0.1 extension but I don't think this makes a difference here.
Any hint is highly appreciated. Thanks!
I finally figured it out. It seems like set_relationship is very picky about the parameter order. The parameter naming doesn't even mean a thing. This worked in the end for me:
{
"session":"3ece4lmn5rtweq9vm5581jht",
"module_name":"Prospects",
"module_id":"cb13b96f-8334-733c-1548-52c27a5b8b99",
"link_field_name":"prospect_lists",
"related_ids":["534f894a-4265-143d-c94b-52be908685b1"],
"delete":0
}
Working Python code (API v4.1):
import sugarcrm
import json
import requests
crm_session = sugarcrm.Session(CRM_HOST, CRM_USER, CRM_PASS)
payload = {
"method": "set_relationship",
"input_type": "JSON",
"response_type": "JSON",
"rest_data": json.dumps({
"session": crm_session.session_id,
"module_name": "Prospects",
# ID of the record you're creating relationship FROM
# In my case it is a record from module "Prospects"
"module_id": "cb13b96f-8334-733c-1548-52c27a5b8b99",
"link_field_name": "events_prospects",
# ID of the record you're creating relationship FOR
# In my case it is a record from module "events"
"related_ids": ["534f894a-4265-143d-c94b-52be908685b1"],
"name_value_list": [],
"delete": 0
})
}
result = requests.post(CRM_HOST, data=payload)
#Till is right, be careful with the order of "rest_data" parameters. In my case placing name_value_list before related_ids has been producing positive results with no actual relationship created.
p.s. I'm using this library: https://pypi.python.org/pypi/sugarcrm/0.1

Is what I see going on here, really going on here?

I admin a couple pages on FB and we recently got hit by a supposedly fake page.
http://www.facebook.com/pages/Duke-St-Rollins/478408292178951
The page is supposedly a duplicate of this user:
http://www.facebook.com/DukeStRollins
However. When I entered this into Graph.facebook.com/478408292178951 I got this returned:
{
"name": "Duke St. Rollins",
"is_published": true,
"talking_about_count": 2,
"category": "Public figure",
"id": "478408292178951",
"link": "http://www.facebook.com/pages/Duke-St-Rollins/478408292178951",
"likes": 2
}
When I entered THIS into graph.facebook.com/Duke-St-Rollins I got this returned:
{
"name": "Duke St. Rollins",
"is_published": true,
"username": "DukeStRollins",
"about": "World famous troll and nemesis of teabaggers.",
"bio": "Press!\n\nhttp://blogs.phoenixnewtimes.com/bastard/2012/07/duke_st_rollins_on_jan_brewer.php \n\nhttp://madmikesamerica.com/2012/07/an-interview-with-duke-st-rollins/\n\nYouTube Channel\nhttp://www.youtube.com/channel/UC_xk6GQzKacHImYl3Vns4VQ\n",
"personal_info": "Follow me on Twitter ",
"talking_about_count": 6450,
"category": "Public figure",
"id": "204170076355643",
"link": "http://www.facebook.com/DukeStRollins",
"likes": 9459,
"cover": {
"cover_id": 261500633955920,
"source": "http://sphotos-a.xx.fbcdn.net/hphotos-ash4/s720x720/376513_261500633955920_779910133_n.jpg",
"offset_y": 92
}
}
If I am understanding how this works correctly, and did this right, does this mean the supposed 'fake' FB page is actually owned by the 'real' Duke?
If I have this wrong (and I hope I do), can someone please explain this to me slowly, like you are talking to a kid as I am TOTALLY new to doing the FB page stuff and until yesterday, never even knew about graph.facebook stuff.
Consider me a noob. Because I am. But I'd really like to know if what I think I am seeing, is what I fear.
No, they aren't the same. The former is a page/public figure. The latter is a user. You can tell them apart by their different IDs (478408292178951 / 204170076355643). They share the same name but can't share the same graph api address because hyphens are ignored (try http://graph.facebook.com/Duke-------------StRollins), which means DukeStRollins and Duke-St-Rollins are effectively identical.
This is in my opinion a glitch in the API: a query by name should be able to distinguish between these two resources, even if a hyphen - is the only difference between their names; that there is not just makes it easier for the spoofer to confuse people.
You've probably already seen this: https://developers.facebook.com/docs/reference/api/
In http://www.facebook.com/pages/Duke-St-Rollins/478408292178951 "Duke-St-Rollins" is just a dummy name created by the Facebook using the title name of the page. You can use anything in place of it to redirect you to the same page. The below links will all redirect you to http://www.facebook.com/pages/Duke-St-Rollins/478408292178951
http://www.facebook.com/pages/page-name/478408292178951
http://www.facebook.com/pages/page-title-name/478408292178951
http://www.facebook.com/pages/dummy-name/478408292178951
http://www.facebook.com/478408292178951 (note that here facebook recognizes 478408292178951 as the username for the page since the page didn't set it's username yet)
In the other page, http://www.facebook.com/Duke-St-Rollins "Duke-St-Rollins" is a username set by the page and hence it uses "dukestrollins" as the facebook graph node to recognize the page. (Note that in the username any dots or hypens will be removed automatically) All the links below redirect you to http://www.facebook.com/DukeStRollins
http://www.facebook.com/duke.st.rollins
http://www.facebook.com/dukestrollins
http://www.facebook.com/Duke-StRollins
http://www.facebook.com/204170076355643
http://www.facebook.com/pages/some-thing-here/204170076355643