Skip to content

Commit

Permalink
used geolocation api to get data which will be spent to airtable to u…
Browse files Browse the repository at this point in the history
…pdate

relates to #18
  • Loading branch information
Armand-Lluka committed Oct 5, 2018
1 parent 8a72c3a commit 2317ad9
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 239 deletions.
31 changes: 31 additions & 0 deletions backupshit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Stringy PostCodeObj
const postData = querystring.stringify(postcodeObj)

// Post options
const options = {
hostname: 'api.postcodes.io',
port: 443,
path: '/postcodes',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length,
}
}

const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)

res.on('postData', (d) => {
process.stdout.write(d)
})
})

req.on('error', (error) => {
next(error)
})

req.write(postData)
console.log(postData);

req.end()
74 changes: 43 additions & 31 deletions controllers/airtable.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
// const https = require('https')
// const querystring = require('querystring')
const request = require('request')

module.exports = (req, res, next) => {

// set up airtable api
var Airtable = require('airtable');
Airtable.configure({
endpointUrl: 'https://api.airtable.com',
apiKey: 'keyYBKUirvMxeaey5'
});
var base = Airtable.base('apphdQNWTLdRQbOOg');

// set up storage array to be sent to frontend
let storage = [];

// setup postcodesIO

var PostcodesIO = require('postcodesio-client');
var postcodes = new PostcodesIO('https://api.postcodes.io', {
headers: { 'User-Agent': 'MyAwesomeApp/1.0.0' } // default {} - extra headers
});

// start query
base('fonthilldummy').select({
// Selecting the first 3 records in Grid view:
Expand All @@ -21,7 +30,9 @@ module.exports = (req, res, next) => {
view: "no_geolocation"
}).eachPage(function page(records, fetchNextPage) {
// This function (`page`) will get called for each page of records.
records.forEach(function (record) {
const airtableRecords = records

airtableRecords.forEach(function (record) {
postcodeIdObj = {
id: record.id,
postcode: record.fields.Postcode,
Expand All @@ -30,38 +41,39 @@ module.exports = (req, res, next) => {
storage.push(postcodeIdObj)
});
// create postcode object to be sent to geolocation api
// let postcodeObj = {
// "postcodes": storage.map(postcode => {
// return postcode.postcode
// })
// }

// create array of postcodes
let postcodeArr = storage.map(postcode => {
return postcode.postcode
});
let postcodeObj = {
"postcodes": storage.map(postcode => {
return postcode.postcode
})
}

request.post('https://api.postcodes.io/postcodes', {
json: postcodeObj,

}, (error,res,body) => {
if (error) {
// console.log(error)
next(error)
return
}
console.log(`statusCode: ${res.statusCode}`)


for (let i = 0; i < body.result.length; i++) {
for(let j = i; j< storage.length; j++) {
// console.log(storage[j].coordinates)
storage[j].coordinates = {
longitude: body.result[i].result.longitude,
latitude: body.result[i].result.latitude,
}

}
}
console.log(storage)
return storage;

postcodeArr.forEach(postcode => {
postcodes
.lookup(postcode)
.then(function (postcode) {
coordinates = { lat: postcode.latitude, lng: postcode.longitude }
postcodeIdObj.coordinates = coordinates;
console.log(postcodeIdObj);
}, function (error) {
next(error);
});
})


// base('fonthilldummy').update('recavzIUC1T3IT1uO', {
// "Postcode": "N4 3HF"
// }, function(err, record) {
// if (err) { console.error(err); return; }
// console.log(record.get('Postcode'));
// });



// To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again.
// If there are no more records, `done` will get called.
Expand Down
Loading

0 comments on commit 2317ad9

Please sign in to comment.