Last active
July 7, 2016 19:06
-
-
Save frankrousseau/55aea37f19e55084b1c165950ac8a972 to your computer and use it in GitHub Desktop.
Example of connector for Cozy Konnectors
This file contains hidden or 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
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var moment = require('moment'); | |
var baseKonnector = require('../lib/base_konnector'); | |
var Bill = require('../models/bill'); | |
var connector = module.exports = baseKonnector.createNew({ | |
name: "Do", | |
fields: { | |
login: "text", | |
password: "password" | |
}, | |
models: [Bill], | |
fetchOperations: [ | |
logIn, | |
parsePage, | |
saveData | |
] | |
}); | |
function logIn(requiredFields, bills, data, next) { | |
var logInOptions = { | |
method: 'GET', | |
jar: true, | |
url: "https://cloud.digitalocean.com/login" | |
}; | |
request(logInOptions, function(err, res, body) { | |
if (err) return next(err); | |
var $ = cheerio.load(body); | |
var token = $("input[name=authenticity_token]").val(); | |
var signInOptions = { | |
method: 'POST', | |
jar: true, | |
url: "https://cloud.digitalocean.com/sessions", | |
form: { | |
'user[email]': requiredFields.login, | |
'user[password]': requiredFields.password, | |
'authenticity_token': token | |
} | |
}; | |
connector.logger.info('Logging in'); | |
request(signInOptions, function(err, res, body) { | |
if (err) { | |
connector.logger.error('Login failed'); | |
connector.logger.raw(err); | |
next(err); | |
} else { | |
connector.logger.info('Login succeeded'); | |
connector.logger.info('Fetch bill info'); | |
var billOptions = { | |
method: 'GET', | |
jar: true, | |
url: "https://cloud.digitalocean.com/settings/billing" | |
}; | |
request(billOptions, function(err, res, body) { | |
if (err) { | |
connector.logger.error('An error occured while fetching bills'); | |
connector.logger.raw(err); | |
next(err); | |
} else { | |
connector.logger.info('Fetch bill info succeeded'); | |
data.html = body; | |
next(); | |
} | |
}); | |
} | |
}); | |
}); | |
}; | |
function parsePage(requiredFields, bills, data, next) { | |
connector.logger.info('Parsing bill pages'); | |
var $ = cheerio.load(data.html); | |
bills.fetched = []; | |
$('table.listing tr').each(function() { | |
var secondCell = $(this).find('td').get(1); | |
if ((secondCell != null) && $(secondCell).html().indexOf('Invoice') > -1) { | |
var firstCell = $($(this).find('td').get(0)); | |
var thirdCell = $($(this).find('td').get(2)); | |
var fourthCell = $($(this).find('td').get(3)); | |
var date = moment(firstCell.html(), 'MMMM D, YYYY'); | |
var pdfurlPrefix = 'https://cloud.digitalocean.com'; | |
var pdfurl = pdfurlPrefix + fourthCell.find('a').attr('href'); | |
var amount = parseFloat(thirdCell.html().replace('$', '')); | |
var bill = { | |
date: date, | |
amount: amount, | |
pdfurl: pdfurl, | |
vendor: 'Digital Ocean', | |
type: 'hosting' | |
}; | |
bills.fetched.push(bill); | |
} | |
}); | |
if (bills.fetched.length === 0) { | |
connector.logger.error("No bills retrieved"); | |
next('no bills retrieved'); | |
} else { | |
connector.logger.info("Bill parsed: " + bills.fetched.length + " found"); | |
next(); | |
} | |
}; | |
function saveData(requiredFields, bills, data, next) { | |
bills.fetched.forEach(function (bill) { | |
Bill.create(bill, function() {}); | |
}); | |
connector.logger.info('All bills were saved.'); | |
next(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment