diff --git a/trashapis.js b/trashapis.js index bb7ea9a..ab441ea 100644 --- a/trashapis.js +++ b/trashapis.js @@ -582,6 +582,87 @@ function recycleManager(postcode, housenumber, country, callback){ }); } +function BlinkAfvalkalender(postcode, housenumber, country, callback) { + if (country !== "NL") { + callback(new Error('unsupported country')); + return; + } + + var wasteTypeMap = { + rest: ["REST"], + gft: ["GFT"], + papier: ["PAPIER"], + kunststof : ["PLASTIC", "PMD"] + }; + + var addressUrl = `https://mijnblink.nl/rest/adressen/${postcode}-${housenumber}/`; + + request(addressUrl, function (err, res) { + if (!err && res.statusCode == 200) { + var address = JSON.parse(res.body); + if(address.length == 1 && 'bagId' in address[0]) { + var bagId = address[0].bagId; + var wasteTypesUrl = `https://mijnblink.nl/rest/adressen/${bagId}/afvalstromen`; + + var year = (new Date()).getFullYear(); + // TODO: Maybe fetch next year as well to prevent missing dates for the next calendar year + var calendarUrl = `https://mijnblink.nl/rest/adressen/${bagId}/kalender/${year}`; + + request(wasteTypesUrl, function (err, res) { + if (!err && res.statusCode == 200) { + var wasteType = JSON.parse(res.body); + if (wasteType.length > 0) { + var collection = {}; + + wasteType.forEach(function (type) { + if(type.slug in wasteTypeMap) { + wasteTypeMap[type.id] = wasteTypeMap[type.slug]; + } + }); + + request(calendarUrl, function (err, res) { + if (!err && res.statusCode == 200) { + var calendar = JSON.parse(res.body); + if (Object.keys(calendar).length > 0) { + Object.keys(calendar).forEach(function(key) { + var day = calendar[key]; + if(wasteTypeMap[day.afvalstroom_id]) { + if ('ophaaldatum' in day) { + var date = day.ophaaldatum.split('-').reverse().join('-'); + wasteTypeMap[day.afvalstroom_id].forEach(function(type) { + if(!collection[type]) { + collection[type] = []; + } + collection[type].push(date); + }); + } + } + }); + + return callback(null, collection); + } else { + return callback(new Error('No collection dates returned for location')); + } + } else { + return callback(new Error('Could not fetch collection dates for location')); + } + }); + } else { + return callback(new Error('No waste types returned for location')); + } + } else { + return callback(new Error('Could not fetch waste types for location')); + } + }); + } else { + return callback(new Error('Invalid location')); + } + } else { + return callback(new Error('Could not fetch location data')); + } + }); +} + function dateFormat(date) { var ad = date.split('-'); return ('0' + ad[0]).slice(-2) + '-' + ('0' + ad[1]).slice(-2) + '-' + ad[2]; @@ -627,6 +708,6 @@ apiList.push(afvalwijzerArnhem); apiList.push(twenteMilieu); apiList.push(gemeenteHellendoorn); apiList.push(recycleManager); - +apiList.push(mijnBlink); module.exports = apiList;