Last active
November 10, 2015 12:13
-
-
Save dulichan/64e672354a7136a1df4b to your computer and use it in GitHub Desktop.
Personal Finance microservice to write CSV data to Google Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module['exports'] = function echoHttp (hook) { | |
var body = hook.params; // This gives all the JSON body parameters in an object. | |
//Using a small security measure ;) | |
if(body.secret != "dgfdg45345jf0248234234"){ | |
hook.res.end("Unauthorized"); | |
return; | |
} | |
var https = require('https'), | |
parse = require('csv-parse'), | |
fs = require('fs'), | |
GoogleSpreadsheet = require("google-spreadsheet"); | |
var TRANSACTION_SHEET_ID = "6665"; | |
var HISTORY_SHEET_ID = "dfgdfg7"; | |
// spreadsheet key is the long id in the sheets URL | |
var my_sheet = new GoogleSpreadsheet('uwoiruweoiruwoierwer-og'); | |
// The JSON credential token obtained from Google - https://developers.google.com/drive/web/auth/web-server | |
var creds = { | |
}; | |
/* | |
Download the file from location and invoke the call back | |
*/ | |
var getFile = function(location , callback){ | |
https.get(location, function(response) { | |
// Continuously update stream with data | |
var body = ''; | |
response.on('data', function(d) { | |
body += d; | |
}); | |
response.on('end', function() { | |
callback(body); | |
}); | |
}); | |
}; | |
try{ | |
getFile(body.attachmentURL, function(data){ | |
// Parsing the downloaded data into CSV | |
parse(data, {comment: '#'}, function(err, output){ | |
// Output is an array | |
my_sheet.useServiceAccountAuth(creds, function(err){ | |
// Use this to find the id of the sheet | |
my_sheet.getInfo( function( err, sheet_info ){ | |
console.log( sheet_info.title + ' is loaded' ); | |
var sheet1 = sheet_info.worksheets[0]; | |
// log the sheet to get the id | |
console.log(sheet1); | |
}); | |
var currentMonth = new Date().getMonth(); | |
/* | |
Insert current months records to the sheet and everything to history | |
sheet | |
*/ | |
var insertRows = function(output){ | |
for (i = 1; i < output.length; i++) { | |
var record = output[i]; | |
var date = new Date(record[0]); | |
var dataObj = { | |
date : record[0], | |
category: record[2], | |
amount: record[3], | |
currency: record[4], | |
description : record[6] | |
}; | |
// Check for current month | |
if (date.getMonth() == currentMonth){ | |
my_sheet.addRow( TRANSACTION_SHEET_ID, dataObj ); | |
} | |
my_sheet.addRow(HISTORY_SHEET_ID, dataObj); | |
} | |
//hook.res.end("Success"); | |
} | |
/* | |
Delete all rows of the current month on the sheet. This is because | |
there is no id for the record. | |
*/ | |
var deleteRows = function(row_data){ | |
for (j = 0; j < row_data.length; j++) { | |
if(new Date(row_data[j].date).getMonth() == currentMonth){ | |
row_data[j].del(); | |
} | |
} | |
} | |
/* | |
Read the transaction sheet | |
*/ | |
my_sheet.getRows( TRANSACTION_SHEET_ID, { | |
start: 1, // start index | |
num: 500, // number of rows to pull | |
orderby: 'date' // column to order results by | |
}, function(err, row_data){ | |
deleteRows(row_data); | |
//Backup the transactions in the history sheet | |
my_sheet.getRows( HISTORY_SHEET_ID, { | |
start: 1, // start index | |
num: 2000, // number of rows to pull | |
orderby: 'date' // column to order results by | |
}, function(err, row_data){ | |
// delete everything before | |
for (j = 0; j < row_data.length; j++) { | |
row_data[j].del(); | |
} | |
call the function to insert history and transaction rows | |
insertRows(output); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}catch(e){ | |
console.log(e); | |
} | |
}; | |
// Running locally using Node. | |
module['exports']({params: {secret: "dgfdg45345jf0248234234" ,attachmentURL:"CSV URL"}}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment