Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update trashapis.js #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions trashapis.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,93 @@ function recycleManager(postcode, housenumber, country, callback){
});
}

function circulusBerkel(postcode, homenumber, country, callback) {

if (country !== "NL") {
callback(new Error('unsupported country'));
return;
}
//if (!isDataValid(postcode, homenumber, country)) {
// return callback(new Error('unsupported city'));
//}

try {
//Get a session token
request('https://mijn.circulus-berkel.nl/', (err, response, body) => {
let cookie = response.headers['set-cookie'];
let authenticityToken = null;
for (var i = 0; i < cookie.length; i++) {
if (cookie[i].startsWith('CB_SESSION')) { var j = cookie[i].indexOf('___AT='); var k = cookie[i].indexOf('&', j); authenticityToken = cookie[i].substring(j + 6, k); }
}
var headers = { 'Content-Type': 'application/json', 'Cookie': cookie };
var options = {
url: 'https://mijn.circulus-berkel.nl/register/zipcode.json',
method: 'POST',
form: { authenticityToken: authenticityToken, zipCode: postcode, number: homenumber },
headers: headers
};

var startDate = new Date(); //startDate.set
startDate = dateFormat(startDate.setDate(startDate.getDate() - 14), "yyyy-mm-dd");

var endDate = new Date();
endDate = dateFormat(endDate.setDate(endDate.getDate() + 90), "yyyy-mm-dd");

//Get a security token
request(options, function (err, res, body) {
let cookie = res.headers['set-cookie'];
var headers = { 'Content-Type': 'application/json', 'Cookie': cookie };
var options = {
url: 'https://mijn.circulus-berkel.nl/afvalkalender.json?from='+ startDate + '&till=' + endDate,
method: 'GET',
headers: headers
};
//Execute the real trash request
request(options, function (err, res, body) {
let dates = {}
var json_body = JSON.parse(body);
var o = json_body.customData.response.garbage;
for (var i = 0; i < o.length; i++) {
var key = o[i].code;
switch (key) {
case 'PMD':
case 'GFT':
case 'REST':
break;
case 'PAP':
key = 'PAPIER';
break;
case 'BEST':
key = 'TEXTIEL';
break;
default:
}
addToDates(key, o[i].dates, dates);
}
return callback(null, dates);
});
});
});

} catch (ex) {
//res.write('Error: ' + ex);
}

}

function addToDates(key, datesToAdd, dates) {
for (var i = 0; i < datesToAdd.length; i++) {
var date = datesToAdd[i];
if (!date) continue;
if (dates[key] == null) dates[key] = [];
var arrDate = date.split('-');
var dutchDate = arrDate[2] + "-" + arrDate[1] + "-" + arrDate[0];
dates[key].push(dutchDate);
}
}



function dateFormat(date) {
var ad = date.split('-');
return ('0' + ad[0]).slice(-2) + '-' + ('0' + ad[1]).slice(-2) + '-' + ad[2];
Expand Down Expand Up @@ -627,6 +714,7 @@ apiList.push(afvalwijzerArnhem);
apiList.push(twenteMilieu);
apiList.push(gemeenteHellendoorn);
apiList.push(recycleManager);
apiList.push(circulusBerkel);


module.exports = apiList;