forked from stemmr/com.athom.trashchecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trashapis.js
632 lines (586 loc) · 24.1 KB
/
trashapis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
"use strict";
var apiList = [];
var http = require('http');
var https = require('https');
var dateFormat = require('dateformat');
var request = require('request');
var cheerio = require('cheerio');
var ical = require('ical');
var DOMParser = require('xmldom').DOMParser;
function afvalapp(postcode, homenumber, country, callback) {
var options = {
host: 'dataservice.deafvalapp.nl',
path: '/dataservice/DataServiceServlet?type=ANDROID&service=OPHAALSCHEMA&land=' +
country + '&postcode=' + postcode + '&straatId=0&huisnr=' + homenumber + '&huisnrtoev='
};
var req = http.get(options, (res) => {
var dates = {};
var curr = '';
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', () => {
var respArray = data.toString().split('\n').join('').split(";");
respArray.pop();
for (var i in respArray) {
if (isNaN(parseInt(respArray[i]))) {
dates[respArray[i]] = [];
curr = respArray[i];
}
else {
dates[curr].push(respArray[i]);
}
}
if (Object.keys(dates).length === 0 && dates.constructor === Object) {
Homey.log('Invalid input');
return callback(null, {});
} else {//validate the response
return callback(null, dates);
}
});
});
req.on('error', function (err) {
Homey.log(err.message);
});
}
function mijnAfvalWijzer(postcode, housenumber, country, callback) {
var fDates = {};
if (country !== "NL") {
callback(new Error('unsupported country'));
return;
}
request(`http://www.mijnafvalwijzer.nl/nl/${postcode}/${housenumber}/`, function (err, res, body) {
if (!err && res.statusCode == 200) {
var $ = cheerio.load(res.body);
$('a.wasteInfoIcon p').each((i, elem) => {
var dateStr = parseDate(elem.children[0].data);
//console.log(elem.attribs.class);
switch (elem.attribs.class) {
case 'gft':
if (!fDates.GFT) fDates.GFT = [];
fDates.GFT.push(dateStr);
break;
case 'papier':
if (!fDates.PAPIER) fDates.PAPIER = [];
fDates.PAPIER.push(dateStr);
break;
case 'restafval':
if (!fDates.REST) fDates.REST = [];
fDates.REST.push(dateStr);
break;
case 'restgft':
if (!fDates.REST) fDates.REST = [];
if (!fDates.GFT) fDates.GFT = [];
fDates.REST.push(dateStr);
fDates.GFT.push(dateStr);
break;
case 'dhm':
if (!fDates.PAPIER) fDates.PAPIER = [];
if (!fDates.PMD) fDates.PMD = [];
fDates.PAPIER.push(dateStr);
fDates.PMD.push(dateStr);
break;
default:
console.log('defaulted', elem.attribs.class);
}
});
console.log(fDates);
return callback(null, fDates);
} else {
return callback(new Error('Invalid location'));
}
});
}
function afvalwijzerArnhem(postcode, housenumber, country, callback) {
var fDates = {};
if (country !== "NL") {
callback(new Error('unsupported country'));
return;
}
var url = `http://www.afvalwijzer-arnhem.nl/applicatie?ZipCode=${postcode}&HouseNumber=${housenumber}&HouseNumberAddition=`;
// console.log(url);
request(url, function (err, res, body) {
if (!err && res.statusCode == 200) {
var $ = cheerio.load(res.body);
$('ul.ulPickupDates li').each((i, elem) => {
var dateStr = dateFormat(elem.children[2].data.trim());
switch (elem.attribs.class) {
case 'gft':
if (!fDates.GFT) fDates.GFT = [];
fDates.GFT.push(dateStr);
break;
case 'papier':
if (!fDates.PAPIER) fDates.PAPIER = [];
fDates.PAPIER.push(dateStr);
break;
case 'restafval':
if (!fDates.REST) fDates.REST = [];
fDates.REST.push(dateStr);
break;
case 'kunststof':
if (!fDates.PLASTIC) fDates.PLASTIC = [];
fDates.PLASTIC.push(dateStr);
break;
default:
console.log('defaulted', elem.attribs.class);
}
});
return callback(null, fDates);
} else {
return callback(new Error('Invalid location'));
}
});
}
function afvalwijzerArnhem(postcode, housenumber, country, callback){
var fDates = {};
if(country !== "NL"){
console.log('unsupported country');
callback(new Error('unsupported country'));
}
var url = `http://www.afvalwijzer-arnhem.nl/applicatie?ZipCode=${postcode}&HouseNumber=${housenumber}&HouseNumberAddition=`;
// console.log(url);
request(url, function(err, res, body){
if(!err && res.statusCode == 200){
//console.log(res);
var $ = cheerio.load(res.body);
$('ul.ulPickupDates li').each((i, elem)=>{
var dateStr =dateFormat(elem.children[2].data.trim());
switch (elem.attribs.class) {
case 'gft':
if(!fDates.GFT) fDates.GFT = [];
fDates.GFT.push(dateStr);
break;
case 'papier':
if(!fDates.PAPIER) fDates.PAPIER = [];
fDates.PAPIER.push(dateStr);
break;
case 'restafval':
if(!fDates.REST) fDates.REST = [];
fDates.REST.push(dateStr);
break;
case 'kunststof':
if(!fDates.PLASTIC) fDates.PLASTIC = [];
fDates.PLASTIC.push(dateStr);
break;
default:
console.log('defaulted', elem.attribs.class);
}
// console.log(i);
// console.log(elem);
// console.log(elem.attribs.class);
// console.log(`${elem.attribs.class}:\t\t${elem.children[2].data.trim()}`);
});
console.log(fDates);
return callback(null, fDates);
} else {
return callback(new Error('Invalid location'));
}
const r = request.defaults({jar: true});
r.post({
url: 'http://afvalkalender.cyclusnv.nl/login.php',
followAllRedirects: true,
form: {
postcode: postcode,
huisnummer: housenumber,
toevoeging: '',
toon: true
}
}, function (err, res, body) {
if (!err && res.statusCode == 200) {
r.get(`http://afvalkalender.cyclusnv.nl/download_ical.php?p=${postcode}&h=${housenumber}&t=`, function (err, res, body) {
if (!err && res.statusCode == 200) {
const dates = {};
const entries = ical.parseICS(body);
for (let i in entries) {
const entry = entries[i];
const dateStr = ('0' + entry.start.getDate()).slice(-2) + '-' + (('0' + (entry.start.getMonth() + 1)).slice(-2)) + '-' + entry.start.getFullYear();
if (entry.description.indexOf('GFT') !== -1) {
if (!dates.GFT) dates.GFT = [];
dates.GFT.push(dateStr);
} else if (entry.description.indexOf('Rest') !== -1) {
if (!dates.REST) dates.REST = [];
dates.REST.push(dateStr);
} else if (entry.description.indexOf('Plastic') !== -1) {
if (!dates.PLASTIC) dates.PLASTIC = [];
dates.PLASTIC.push(dateStr);
}
}
return callback(null, dates);
} else {
return callback(new Error('Unable to download ical file'));
}
});
} else {
return callback(new Error('Unable to login'));
}
});
});
}
function twenteMilieu(postcode, housenumber, country, callback){
var fDates = {};
if(country !== "NL"){
console.log('unsupported country');
callback(new Error('unsupported country'));
}
var startDate = new Date();
startDate = dateFormat(startDate.setDate(startDate.getDate() - 14), "yyyy-mm-dd");
// console.log("startDate is: " + startDate);
var endDate = new Date();
endDate = dateFormat(endDate.setDate(endDate.getDate() + 30), "yyyy-mm-dd");
// console.log("endDate is: " + endDate);
var post_data1 = `companyCode=8d97bb56-5afd-4cbc-a651-b4f7314264b4&postCode=${postcode}&houseNumber=${housenumber}&houseLetter=&houseNumberAddition=`;
var post_options1 = {
host: 'wasteapi.2go-mobile.com',
port: '443',
path: '/api/FetchAdress',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data1)
}
};
var buffer1="";
var buffer2="";
var post_req1 = https.request(post_options1, function(res1) {
if (res1.statusCode == 200){
res1.on( "data", function( chunk1 ) { buffer1 = buffer1 + chunk1; } );
res1.on( "end", function( chunk1 ) {
// console.log("Output fetchAddress is: " + buffer1);
var obj1 = JSON.parse(buffer1);
if(obj1.status){
var uniqueID = obj1.dataList[0].UniqueId;
// console.log("UniqueID: " + uniqueID);
var post_data2 = `companyCode=8d97bb56-5afd-4cbc-a651-b4f7314264b4&uniqueAddressID=${uniqueID}&startDate=${startDate}&endDate=${endDate}`;
var post_options2 = {
host: 'wasteapi.2go-mobile.com',
port: '443',
path: '/api/GetCalendar',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data2)
}
};
var post_req2 = https.request(post_options2, function(res2) {
res2.on( "data", function( chunk2 ) { buffer2 = buffer2 + chunk2; } );
res2.on( "end", function( chunk2 ) {
var obj2 = JSON.parse(buffer2);
if (obj2.status){
for (var i=0; i < Object.keys(obj2.dataList).length; i++){
// console.log("Type afval is: " + obj2.dataList[i]._pickupTypeText);
switch(obj2.dataList[i]._pickupTypeText)
{
case "GREY":
// console.log("REST:");
if(!fDates.REST) fDates.REST = [];
break;
case "GREEN":
// console.log("GFT:");
if(!fDates.GFT) fDates.GFT = [];
break;
case "PAPER":
// console.log("PAPIER:");
if(!fDates.PAPIER) fDates.PAPIER = [];
break;
case "PACKAGES":
// console.log("PLASTIC:");
if(!fDates.PLASTIC) fDates.PLASTIC = [];
break;
}
// console.log("Datum is: " + obj2.dataList[i].pickupDates[0]);
//console.log("Aantal datums: " + Object.keys(obj2.dataList[i].pickupDates).length);
for (var j=0; j < Object.keys(obj2.dataList[i].pickupDates).length; j++){
var date = dateFormat(obj2.dataList[i].pickupDates[j], "dd-mm-yyyy");
switch(obj2.dataList[i]._pickupTypeText)
{
case "GREY":
if(!fDates.REST) fDates.REST = [];
fDates.REST.push(date);
break;
case "GREEN":
if(!fDates.GFT) fDates.GFT = [];
fDates.GFT.push(date);
break;
case "PAPER":
if(!fDates.PAPIER) fDates.PAPIER = [];
fDates.PAPIER.push(date);
break;
case "PACKAGES":
if(!fDates.PLASTIC) fDates.PLASTIC = [];
fDates.PLASTIC.push(date);
break;
}
}
}
console.log(fDates);
return callback(null, fDates);
}else{
console.log("Er is iets fout gegaan bij het ophalen vd data");
return callback(new Error('Invalid location'));
}
});
});
// post the data
post_req2.write(post_data2);
post_req2.end();
}else{
console.log("Postcode niet gevonden status = false");
return callback(new Error('Invalid location'));
}
});
} else {
console.log("Postcode niet gevonden != 200");
return callback(new Error('Invalid location'));
}
});
// post the data
post_req1.write(post_data1);
post_req1.end();
}
function gemeenteHellendoorn(postcode, housenumber, country, callback){
if(country !== "NL"){
console.log('unsupported country');
callback(new Error('unsupported country'));
}
var DOMParser = new DOMParser();
var startDate = new Date();
startDate = dateFormat(startDate.setDate(startDate.getDate() - 14), "yyyy-mm-dd");
// console.log("startDate is: " + startDate);
var endDate = new Date();
endDate = dateFormat(endDate.setDate(endDate.getDate() + 90), "yyyy-mm-dd");
// console.log("endDate is: " + endDate);
var body1 = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'+
'<soap12:Body><GetAddresses xmlns="http://www.meurs-software.nl/afval-ris">'+
`<ZipCode>${postcode}</ZipCode>`+
`<HouseNumber>${housenumber}</HouseNumber>`+
'<HouseLetter></HouseLetter>'+
'</GetAddresses></soap12:Body></soap12:Envelope>';
var postRequest1 = {
host: "hellendoornportal-srvc.2go-mobile.com",
path: "/ReportService.asmx",
port: 80,
method: "POST",
headers: {
'Cookie': "cookie",
'Content-Type': 'text/xml',
'Content-Length': Buffer.byteLength(body1)
}
};
var result = "";
var fDates = {};
var buffer1 = "";
var uniqueID = "";
var req1 = http.request( postRequest1, function( res1 ) {
if (res1.statusCode == 200) {
// console.log( res1.statusCode );
console.log("Aanroep Hellendoorn met: " + postcode + housenumber + country);
var buffer1 = "";
res1.on( "data", function( data1 ) { buffer1 = buffer1 + data1; } );
res1.on( "end", function( data1 ) {
// console.log( buffer1 );
var doc1 = DOMParser.parseFromString(buffer1,"text/xml");
// console.log("statusCode is: " + doc1.getElementsByTagName("StatusCode")[0].childNodes[0].data);
if (doc1.getElementsByTagName("StatusCode")[0].childNodes[0].data == "Ok" && doc1.getElementsByTagName("Addresses")[0].childNodes.length > 0){
var uniqueIDObject = doc1.getElementsByTagName("UniqueId");
uniqueID = uniqueIDObject[0].childNodes[0].data;
var body2 = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'+
'<soap12:Body><GetContainerDates xmlns="http://www.meurs-software.nl/afval-ris">'+
`<UniqueAddressId>${uniqueID}</UniqueAddressId>`+
`<Start>${startDate}</Start>`+
`<End>${endDate}</End>`+
'</GetContainerDates></soap12:Body></soap12:Envelope>';
var postRequest2 = {
host: "hellendoornportal-srvc.2go-mobile.com",
path: "/ReportService.asmx",
port: 80,
method: "POST",
headers: {
'Cookie': "cookie",
'Content-Type': 'text/xml',
'Content-Length': Buffer.byteLength(body2)
}
};
var req2 = http.request( postRequest2, function( res2 ) {
if (res2.statusCode == 200){
// console.log( res2.statusCode );
var buffer2 = "";
// console.log("uniqueID is: ", uniqueID);
res2.on( "data", function( data2 ) {buffer2 = buffer2 + data2; });
res2.on( "end", function( data2 ) {
var doc2 = DOMParser.parseFromString(buffer2,"text/xml");
if (doc2.getElementsByTagName("StatusCode")[0].childNodes[0].data == "Ok"){
var trashCodeObject = doc2.getElementsByTagName("Code");
var numberOfCodes = trashCodeObject.length;
// console.log("Code is: ", doc.getElementsByTagName("Code")[0].childNodes[0].data);
// console.log("Aantal gevonden Code velden: ", numberOfCodes);
for (var i=0; i < numberOfCodes; i++){
switch(trashCodeObject[i].childNodes[0].data)
{
case "00":
// console.log("REST:");
if(!fDates.REST) fDates.REST = [];
break;
case "11":
// console.log("GFT:");
if(!fDates.GFT) fDates.GFT = [];
break;
case "22":
// console.log("PAPIER:");
if(!fDates.PAPIER) fDates.PAPIER = [];
break;
case "66":
// console.log("PLASTIC:");
if(!fDates.PLASTIC) fDates.PLASTIC = [];
break;
}
var numberOfDates = trashCodeObject[i].parentNode.lastChild.childNodes.length;
// console.log("Aantal gevonden Datums: ", numberOfDates);
for (var j=0; j < numberOfDates; j++){
var date = trashCodeObject[i].parentNode.lastChild.childNodes[j].childNodes[0].nodeValue;
// console.log(dateFormat(date, "dd-mm-yyyy"));
switch(trashCodeObject[i].childNodes[0].data)
{
case "00":
if(!fDates.REST) fDates.REST = [];
fDates.REST.push(dateFormat(date, "dd-mm-yyyy"));
break;
case "11":
if(!fDates.GFT) fDates.GFT = [];
fDates.GFT.push(dateFormat(date, "dd-mm-yyyy"));
break;
case "22":
if(!fDates.PAPIER) fDates.PAPIER = [];
fDates.PAPIER.push(dateFormat(date, "dd-mm-yyyy"));
break;
case "66":
if(!fDates.PLASTIC) fDates.PLASTIC = [];
fDates.PLASTIC.push(dateFormat(date, "dd-mm-yyyy"));
break;
}
// console.log(dateFormat(date, "dd-mm-yyyy"));
}
// console.log(trashCodeObject[i].getElementsByTagName("string")[0]);
}
console.log(fDates);
return callback(null, fDates);
} else {
console.log("Ophalen van ophaaldata is mislukt!");
return callback(new Error('Invalid location'));
}
});
} else {
console.log("Er is iets fout gegaan!");
return callback(new Error('Invalid location'));
}
});
req2.write( body2 );
req2.end();
} else {
console.log("Er is iets fout gegaan!");
return callback(new Error('Invalid location'));
}
});
} else {
console.log("Er is iets fout gegaan!");
return callback(new Error('Invalid location'));
}
});
req1.write( body1 );
req1.end();
}
function recycleManager(postcode, housenumber, country, callback){
if(country !== "NL"){
console.log('unsupported country');
callback(new Error('unsupported country'));
}
var fDates = {};
console.log("Recyclemanager met: " + postcode + " " + housenumber);
var url = `https://vpn-wec-api.recyclemanager.nl/v2/calendars?postalcode=${postcode}&number=${housenumber}`;
request(url, function(err, res, body){
if(!err && res.statusCode == 200){
// console.log(res);
var obj1 = JSON.parse(res.body);
if (obj1.status == "success"){
for (var i=0; i < 2; i++){
// console.log("Maand is: " + dateFormat(obj1.data[i].title));
for (var j=0; j < obj1.data[i].occurrences.length; j++){
var dateStr = dateFormat(obj1.data[i].occurrences[j].from.date, "dd-mm-yyyy");
switch (obj1.data[i].occurrences[j].title) {
case 'Groente en fruit':
if(!fDates.GFT) fDates.GFT = [];
fDates.GFT.push(dateStr);
break;
case 'Papier':
if(!fDates.PAPIER) fDates.PAPIER = [];
fDates.PAPIER.push(dateStr);
break;
case 'Restafval':
if(!fDates.REST) fDates.REST = [];
fDates.REST.push(dateStr);
break;
case 'PMD':
if(!fDates.PLASTIC) fDates.PLASTIC = [];
fDates.PLASTIC.push(dateStr);
break;
default:
console.log('defaulted', obj1.data[i].occurrences[j]);
}
}
}
console.log(fDates);
return callback(null, fDates);
} else {
console.log("Postcode niet gevonden!");
return callback(new Error("Postcode niet gevonden!"));
}
} else {
console.log("Probleem met aanroep API!");
return callback(new Error("Probleem met aanroep API!"));
}
});
}
function dateFormat(date) {
var ad = date.split('-');
return ('0' + ad[0]).slice(-2) + '-' + ('0' + ad[1]).slice(-2) + '-' + ad[2];
}
function parseDate(dateString) {
var dateArray = dateString.split(" ");
var fullString = dateArray[1] + '-'; //day of the month(already padded)
var months = [
'januari',
'februari',
'maart',
'april',
'mei',
'juni',
'juli',
'augustus',
'september',
'oktober',
'november',
'december',
];
var monthNum = months.indexOf(dateArray[2]) + 1;
if (monthNum > 0) {
var monthString = (monthNum).toString();
if (monthString.length === 1) {
monthString = '0' + monthString;
}
fullString += monthString + '-';
} else {
console.log('This should not be possible...');
return 'erroneous date';
}
fullString += new Date().getFullYear();
return fullString;
}
apiList.push(afvalapp);
apiList.push(mijnAfvalWijzer);
apiList.push(afvalwijzerArnhem);
apiList.push(twenteMilieu);
apiList.push(gemeenteHellendoorn);
apiList.push(recycleManager);
module.exports = apiList;