Perl : Exporting a Google Spreadsheet Into xlsx Format Using Net::Google::Drive - perl

I am trying to export google spreasheets into xlsx excel spreasheets using Perl.
I am currently able to get the data in the google spreadsheet from my script but I'd prefer to export the file into a local excel spreadsheet and get the information I need from there.
I can't manage to find a way to export a google spreadsheet into a local xlsx file using Net::Google::Drive
Only binary files can be obtained with the methods in that module.
has anyone managed to perform this ?
If yes, could you share some code ?
Thanks and regards.
X.
--
Here is my current code :
use Net::Google::Drive;
use Net::Google::DataAPI::Auth::OAuth2;
use Storable;
my $client_id = "XXXXXX.apps.googleusercontent.com";
my $client_secret = "YYYYY";
my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
client_id => $client_id,
client_secret => $client_secret,
scope => ['https://www.googleapis.com/auth/drive.readonly'],
);
my $session = retrieve('google_drive.session');
my $restored_token = Net::OAuth2::AccessToken->session_thaw($session,
auto_refresh => 1,
profile => $oauth2->oauth2_webserver,
);
$oauth2->access_token($restored_token);
my $disk = Net::Google::Drive->new(
-client_id => $client_id,
-client_secret => $client_secret,
-access_token => $oauth2->access_token($restored_token),
-refresh_token => $oauth2->refresh_token($restored_token),
);
my $file_id = "XXXXX";
my $dest_file = 'd:\\test.xlsx';
$disk->downloadFile(
-file_id => $file_id,
-dest_file => $dest_file,
);

Related

league/flysystem-aws-s3-v3 get custom metada

I'm messing with the flysystem library (it is amazing, anyway!)
I created some files on the remote s3 bucket defining some custom metadata
$conf = [
'visibility' => 'public',
'Metadata' => [
'Content-type' => 'image/jpeg',
'Generated' => 'true'
]];
$response = $filesystem->write('/test/image.jpg', $image_stream, $conf);
'Generated' => 'true' is a custom metadata and I can find it in AWS Bucket's console.
I'm not able to read the custom metadata "generated" on filesystem resource after
$allFiles = $filesystem->listContents('/path/', true)->toArray();
##UPDATE 1
I understood that I should use the "getWithMetadata" plugin as explained In the documentation: https://flysystem.thephpleague.com/v1/docs/advanced/provided-plugins/#get-file-info-with-explicit-metadata
It seems pretty easy, but it seems there is not any League\Flysystem\Filesystem::addPlugin() method in my src.
Can anybody help me?
to use the S3 FlySystem adapter you need to init the S3 client
$client = new Aws\S3\S3Client($options);
In S3 SDK you could use
$allFiles = $filesystem->listContents($path)->sortByPath()->toArray();
$file = $allFiles[0];
$headers = $client->headObject(array(
"Bucket" => $bucket,
"Key" => $file->path()
));
print_r($headers->toArray());
to get the list of all file's metadata!

Posting metric to google analytics from perl script

I need to generate a google analytic event from my perl cron job.
Based on https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
POST to www.google-analytics.com/collect will accept the metrics.
I tried posting metric using following code, but I am not seeing them in google analytics realtime page.
use LWP::UserAgent;
$ua=LWP::UserAgent->new;
$ua->default_header("Content-type" => "application/x-rm-urlencoded");
my $response = $ua->post("http://www.google-analytics.com/collect",
"v" => 1,
"tid" => "UA-XXXXXXXX-Y",
"cid" => 125,
"t" => "pageview",
"dh" => "Cron",
"dp" => "Cron.php",
"dt" => "CronTitle");
print $response->status_line;'
Response is "200 OK", but I am not seeing the data in google analytics webpage.
I am able to post analytics from my php scripts, but could not post analytics from perl script.
I solved my problem as follows.
Created an utility php script and called that php script from my perl script. Then it worked.
<?php
$ec = $_GET['ec'];
$ea = $_GET['ea'];
$el = $_GET['el'];
$ev = $_GET['ev'];
// Send event tracking as well.
$url = 'http://www.google-analytics.com/collect';
$event_data = array(
'v' => '1',
'tid' => 'UA-XXXXXX-Y',
'cid' => $_SERVER["REMOTE_ADDR"],
't' => 'event',
'ec' => $ec,
'ea' => $ea,
'el' => $el,
'ev' => $ev);
$event_options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($event_data),
),
);
$event_context = stream_context_create($event_options);
$result = file_get_contents($url, false, $event_context);
echo "XXX";
?>

Can I use Dropzone.js to send files directly to Facebook instead of first uploading them to my sever?

I can post images to a user's event stream using Dropzone.js using HTML, Javascript and PHP:
HTML
<form action="PHP/uploads.php" class='dropzone' id='fbDropZone'></form>
Javascript
Dropzone.autoDiscover = true;
Dropzone.options.fbDropZone = {
init: function () {
fbDropZone = this;
$("#removeAllImages").click(function () {
fbDropZone.removeAllFiles();
});
},
previewTemplate: '<div class="dz-preview dz-file-preview"><div class="dz-details"><div class="dz-filename"><span data-dz-name></span></div><div class="dz-size" data-dz-size></div><img data-dz-thumbnail /></div><div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div><div class="dz-success-mark"><span>✔</span></div><div class="dz-error-mark"><span>✘</span></div><div class="dz-error-message"><span data-dz-errormessage></span></div></div>',
paramName: "file",
maxFilesize: 5,
maxFiles: 1,
autoProcessQueue: false
};
And then this PHP is called when a <DIV> is clicked on:
$config = array(
'appId' => $app_id,
'secret' => $app_secret,
'scope' => $required_permissions,
'fileUpload' => true,
'allowSignedRequest' => false,
);
$facebook = new Facebook($config);
$facebook->setAccessToken($_COOKIE['fbAccessToken']);
$eid = $_COOKIE['eid'];
$ds = DIRECTORY_SEPARATOR;
$storeFolder = 'uploads';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
$photo_return = $facebook->api($eid.'/photos', 'POST', array('source' => '#'.$_FILES['file']['name'], 'message' => 'This post from a DropZone'));
}
Thing is I'd like to pass the image directly from the user's drive to FB without going via my server. Can anyone point me towards some code to do this? Or is it just not possible as security issues prevent me from knowing exactly where the file is on a user's device/computer?
When a user posts a file, you receive the file stream. You may know the user's file path, but it's irrelevant if you're not in the same computer.
As far as I know, you should be able to send the file stream from your server to Facebook directly with no security issues, but sending just a file path would be of no help.
If you really want to send a file without receiving it in your server, then you should program a Javascript-only solution, but that would require a Facebook JS API with file upload support. I'm not aware if such a thing exists.

Using the new 'data' option for CakeEmail attachments with CakePHP 2.4.x

The CakeEmail help page states that the data option has been added as of 2.4, so you no long have to have a physical file to add an attachment to an email.
I've got the following code:
$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf', array('data' => $pdf)))
->send($this->request->data['email-message']);
But whenever I run that I get an Internal Error saying File Not Found: "".
I had a look at the source code (which I'm beginning to learn is often more useful than reading the documentation!): https://github.com/cakephp/cakephp/blob/master/lib/Cake/Network/Email/CakeEmail.php
Changing my code to:
$Email = new CakeEmail('default');
$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf' => array('data' => $pdf, 'mimetype' => 'application/pdf')))
->send($this->request->data['email-message']);
Notice on the attachments line, the array is assigned to the filename variable, rather than passed in as a parameter!
For completeness, if anyone else is reading this and is wondering how I am generating my PDF with CakePDF:
// Create PDF for attachment
$CakePdf = new CakePdf();
$CakePdf->template('claim', 'default');
//get the pdf string returned
$pdf = $CakePdf->output();

Bing Custom Search Engine

What parameters should I use in order to get search result from only a site say abcd.com using bing 2.0 API? I am trying to fetch results in JSON format. Can anybody help?
fresh code to the masses
$query = array
(
'AppId' => <API_KEY>,
'sources' => 'Web',
'query' => 'site:www.tipografix.ro '.$keywords,
'Version' => '2.0',
'Options' => 'EnableHighlighting',
'Web.Count' => $per_page,
'Web.Offset' => $page_num,
'Web.Options' => 'DisableHostCollapsing DisableQueryAlterations'
);
$request = 'http://api.bing.net/json.aspx?'.http_build_query($query);
$response = file_get_contents($request);
$jsonobj = json_decode($response);
Not sure how the JSON format of the API works but in the query parameter put in "site:abcd.com bacon" where bacon is your original query.
I'm using the XML format so if I'd send a request to:
http://api.search.live.net/xml.aspx?Appid=________&query=site%3Aabcd.com+bacon&sources=web&web.count=5&web.offset=0