Parsing a big json then multiple small jsons - facebook-graph-api

My english is not the best, but I will try to explain what I want. So, we have a big json which has a lot of data, now because the server cant show us all the data at the same time, there will be multiple links to small json inside this big json, like when there are 20 comment on a page, and after that there is a "Show more comments" button. My problem is that I dont know how to do this, how to request the smaller json, the parse it. Another question is how to parse a json which links at the end to another json, something like when requestiong a json from graph api and at the end it has
[paging] => stdClass Object
(
[previous] => https://graph.facebook.com/$group_id$/feed?access_token=$valid_token$&__paging_token=$paging_token$&__previous=1
[next] => https://graph.facebook.com/$group_id$/feed?access_token=$valid_token$&limit=25&until=1375310522&__paging_token=$paging_token$
)
When the link from the [next] is opened, it shows more post from that group, then again at its end there is a next link.
As for the first question, I have a bit longer example from the facebook graph api comments
[comments] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 53575265890127
[from] => stdClass Object
(
[name] => Pop Dan
[id] => 10000897827962
)
[message] => Random message
[can_remove] => 1
[created_time] => 2013-08-18T20:01:44+0000
[like_count] => 0
[user_likes] =>
)
... more coments...
[paging] => stdClass Object
(
[cursors] => stdClass Object
(
[after] => NTM1ODIxODE5ODA2NTQ0
[before] => NTM1NzUyNjU2NDgwMTI3
)
next] => https://graph.facebook.com/$group_id$_$post_id$/comments?access_token=$accestoken$&limit=25&after=NTM1ODIxODE5ODA2NTQ0
)
)
There I want to continue parsing the json from the [next] and then print it in the same html page, without any breaks or anything between comments. Thanks ;)

Some of the details depend on the language you are using for querying graph api.
Json returned from graph api can consist of json arrays and json objects. Json objects can further have named value entities
If you are doing it in android then first of all you have to get the inner json object from the response first. you can do it in this way
GraphObject go = response.getGraphObject();
JSONObject jso = go.getInnerJSONObject();
Now to get an object from the json you can use
jso.getJSONObject("object_name");
and to get json array you can use
jso.getJSONArray("array_name")
other languages will have similar interfaces
For general understanding of json refer to this link
Regarding your second question you should understand that next is just another query to the graph node but with different parameters. How the parameters can be set depends on the api but in Android you can pur parameters as follows
Bundle params=r.getParameters();
params.putString("offset", ""+25);
r.setParameters(params);
r is the object of type Request

Related

WP_Query meta_query REGEXP

I am pretty much below beginner level for REGEX/REGEXP and have hit a blocking point in a project I am working in, where I am trying to get the ids for posts that match the search criteria , but I want to restrict the search between 2 sub-strings. I am trying to figure out is how to write the REGEXP in the meta_query:
$args = array(
'post_type'=> 'custom',
'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $page,
'meta_query' => array(
array(
'key' => 'key',
'value' => "title*".$search."*_title",
'compare' => 'REGEXP',
)
),
);
And an example of the field in the DB :
a:302:{s:5:"title";s:10:"Test title";s:6:"_title";s:19:"
Unfortunately none of the combinations I tried based on documentation of SQL REGEXP won't return any values and I am trying to understand how I can pull this off and would appreciate any input.
Also would rather stick to WP_Query for now even though an SQL LIKE "%title%{$search}%_title%" works perfectly , so an alternative solution would be how to set the compare to 'LIKE' and parse it '%' since that is not possible out of the box as the % get escaped I believe.

How can I generate a PDF file from Amazon Textract output?

I am using Amazon Textract to extract data from a scanned document. Now I want to convert the output to a PDF file. Below is a sample output of Textract:
[1] => Array
(
[BlockType] => LINE
[Confidence] => 99.4744720459
[Text] => Hello
[Geometry] => Array
(
[BoundingBox] => Array
(
[Width] => 0.243866533041
[Height] => 0.0134594505653
[Left] => 0.176409825683
[Top] => 0.0463116429746
)
[Polygon] => Array
(
[0] => Array
(
[X] => 0.176409825683
[Y] => 0.0463116429746
)
[1] => Array
(
[X] => 0.420276373625
[Y] => 0.0463116429746
)
[2] => Array
(
[X] => 0.420276373625
[Y] => 0.0597710944712
)
[3] => Array
(
[X] => 0.176409825683
[Y] => 0.0597710944712
)
)
)
[Id] => 75e8917d-701e-4e26-bade-f00bde9d87db
[Relationships] => Array
(
[0] => Array
(
[Type] => CHILD
[Ids] => Array
(
[0] => 46f44500-4960-4405-99f3-fa43101bc2ca
)
)
)
)
As you can see, the output contains text, height, width and its XY coordinates. How can I place the text with same co-ordinates into a PDF file?
Assuming you can convert the above to JSON, you can use jsPDF or PDFkit to create the PDF. The functionality maps pretty well based upon the limited data you posted, but I have not seen the full structure of Textract as it is still in Beta and I didn't get an invite to the program. Both these projects can use Node to create a server-side solution, but they also work in the Browser.
At the time of this writing, Google Cloud has an OCR component in their Vision - Document Text Detection feature. Unlike Textract, it approaches the task as just reporting what visual elements the document has and creating a comprehensive (and large) data structure that describes what it "sees." Textract, according to Amazon, uses machine learning to organize the data in a more human understandable form that seeks to differentiate the form from the data that constitutes the filled-out part of the form. If you are trying to create a relatively complete PDF, the Google product is well suited. Textract might be too, but I don't know yet.
This repository contains code examples (in Java) showing how you can generate a searchable PDF using AWS Textract. If you are not using Java, you may also deploy it as an AWS Lambda function and then invoke it via the AWS SDK or as a REST API call using AWS API Gateway.
There is a corresponding blog post also available here.

Missing creative spec facebook

I'm trying to create an ad using the php sdk.
I can create the campaign, targeting, adset, and the creative (which returns a creative_id that I can validate using the graph explorer).
But when I finally run the code to create the ad itself, I get an exception that looks like this:
"error_user_title" => "Missing creative spec"
"error_user_msg" => "No creative spec found for given adgroup."
I just can't find anything referring to this error.
Below is the relevant portion of my code:
$link_data = new AdCreativeLinkData();
$link_data->setData(array(
AdCreativeLinkDataFields::LINK => $route,
AdCreativeLinkDataFields::MESSAGE => $petition_statement,
AdCreativeLinkDataFields::NAME => $banner_title,
AdCreativeLinkDataFields::IMAGE_HASH => $image_hash,
));
$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => $pageid,
AdCreativeObjectStorySpecFields::INSTAGRAM_ACTOR_ID=>$instagram_id,
AdCreativeObjectStorySpecFields::LINK_DATA=>$link_data
));
$creative = new AdCreative(null,$account_id);
$creative->setData(array(
AdCreativeFields::TITLE => $banner_title,
AdCreativeFields::BODY => $banner_subtitle,
AdCreativeFields::IMAGE_HASH => $image_hash,
AdCreativeFields::OBJECT_TYPE => 'SHARE',
AdCreativeFields::OBJECT_STORY_SPEC=>$object_story_spec
));
$creative->create();
echo 'Creative ID: '.$creative->id . "\n";
$ad = new Ad(null, $account_id);
$ad->setData(array(
AdFields::NAME => $short_name,
AdFields::ADSET_ID => $adset->id,
AdFields::CREATIVE => $creative,
AdFields::TRACKING_SPECS => array(array(
'action.type' => 'offsite_conversion',
'fb_pixel' => $pixel_code,
))
));
$ad->create(array(Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED));
Appreciate any help.
I've often said that the only skill you need to be a successful developer is the ability to agonize over a problem for days, read through source code, google it, refactor, rewrite and then realize you forgot something fucking obvious.
AdFields::CREATIVE => $creative,
should read
AdFields::CREATIVE => $creative->id,
But the ability to persist isn't the skill you need. The real skill is to somehow resist the overwhelming urge to chuck your computer out the window and do something productive with your life instead.
After hours of testing it seems Trevor's answer is incorrect. This is the right syntax:
AdFields::CREATIVE => array('creative_id'=>$creative->id)

retrieve cookie value in cakephp

I'm storing a multidimensional array in a cookie.
$this->Cookie->write('Cart',
$products, false, 3600
);
Below is the multidimensional array which I'm storing in the cookie
Array
(
[Cart] => Array
(
[user_id] =>
[product_id] => 92
[quantity] => 1
[date_created] =>
[date_modified] =>
[product_name] => shoes
[price] => 12
)
)
but when i read the cookie, it gives me this output
[{\"Cart\":{\"user_id\":\"\",\"product_id\":\"7\",\"quantity\":\"1\",\"date_created\":\"\",\"date_modified\":\"\",\"product_name\":\"iPhone\",\"price\":\"12\"}}]
below is the code which i'm using to read the cookie
$this->Cookie->read('Cart');
On my local server it works perfectly alright but gives me above mentioned output when I try it on Online server
You could try the following to write
$this->Cookie->write('Cart', serialize($products), false, 3600);
And this to read
unserialize($this->Cookie->read('Cart'));
Your cookie is probably being saved as plain text.

Symfony2, Doctrine 2: getResult Object

$posts = $em->find('Application\BlogBundle\Entity\Post',1);
print_r ($posts);
Why I got it?
Barii\BlogBundle\Entity\Post Object ( [id:Barii\BlogBundle\Entity\Post:private] => 1 [title:Application\BlogBundle\Entity\Post:private] => something [body:Application\BlogBundle\Entity\Post:private] => content )
instead of a simple array like this:
array ( [id] => 1,
[title] => "something",
[body] => "content" )
I use it with Symfony 2.
You have a couple options here. As far as I know, you can't find results as arrays from entity repositories by default. Instead, you can do one of two things:
First, you could implement a toArray() method on your entity object (perhaps through a mapped superclass) that simply returns an array of properties.
Second, you could use Doctrine Query Language to pull the information that you need using the getArrayResult() method, perhaps something like this:
$query = $em->createQuery('SELECT p FROM Application\BlogBundle\Entity\Post p WHERE p.id=:pid');
$query->setParameter('tid', $postId);
$result = $query->getArrayResult(); // shortcut for $query->getResult(Query::HYDRATE_ARRAY);
More in-depth documentation on DQL can be found here.