Skip to content

Instantly share code, notes, and snippets.

@harrietty
Last active July 21, 2017 13:58
Show Gist options
  • Save harrietty/f2bcf125dd39117afc03e8a706b366b6 to your computer and use it in GitHub Desktop.
Save harrietty/f2bcf125dd39117afc03e8a706b366b6 to your computer and use it in GitHub Desktop.
const request = require('axios');
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB.DocumentClient();
const { differenceWith, isEqual } = require('lodash');
const { extractListingsFromHTML } = require('./helpers');
module.exports.getdonkeyjobs = (event, context, callback) => {
let newJobs, allJobs;
request('https://www.thedonkeysanctuary.org.uk/vacancies')
.then(({ data }) => {
allJobs = extractListingsFromHTML(data);
// Retrieve yesterday's jobs
return dynamo.scan({
TableName: 'donkeyjobs'
}).promise();
})
.then(response => {
// Figure out which jobs are new
let yesterdaysJobs = response.Items[0] ? response.Items[0].jobs : [];
newJobs = differenceWith(allJobs, yesterdaysJobs, isEqual);
// Get the ID of yesterday's jobs which can now be deleted
const jobsToDelete = response.Items[0] ? response.Items[0].listingId : null;
// Delete old jobs
if (jobsToDelete) {
return dynamo.delete({
TableName: 'donkeyjobs',
Key: {
listingId: jobsToDelete
}
}).promise();
} else return;
})
.then(() => {
// Save the list of today's jobs
return dynamo.put({
TableName: 'donkeyjobs',
Item: {
listingId: new Date().toString(),
jobs: allJobs
}
}).promise();
})
.then(() => {
callback(null, { jobs: newJobs });
})
.catch(callback);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment