From 15e05bc80aa4c0b326b09bc5a4c862329936a626 Mon Sep 17 00:00:00 2001 From: XeroxQ <43435060+XeroxQ@users.noreply.github.com> Date: Thu, 20 Sep 2018 10:22:49 +0200 Subject: [PATCH] Update trashapis.js Hey, I added circulusBerkel (apeldoorn and towns around it) to the app. I hope you will add it. Kind Regard, Arie J. Godschalk --- trashapis.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/trashapis.js b/trashapis.js index bb7ea9a..84693a5 100644 --- a/trashapis.js +++ b/trashapis.js @@ -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]; @@ -627,6 +714,7 @@ apiList.push(afvalwijzerArnhem); apiList.push(twenteMilieu); apiList.push(gemeenteHellendoorn); apiList.push(recycleManager); +apiList.push(circulusBerkel); module.exports = apiList;