Full connector snippet - Phyks/konnectors GitHub Wiki

TODO

Here is a complete example of a connector (fetching bills from DigitalOcean in this case). You can also have a look at all the other available connectors for other examples.

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();
};