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

Mijnblink 2.0 #16

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
83 changes: 82 additions & 1 deletion trashapis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -627,6 +708,6 @@ apiList.push(afvalwijzerArnhem);
apiList.push(twenteMilieu);
apiList.push(gemeenteHellendoorn);
apiList.push(recycleManager);

apiList.push(mijnBlink);

module.exports = apiList;