-
Notifications
You must be signed in to change notification settings - Fork 0
/
delivery.js
127 lines (115 loc) · 3.22 KB
/
delivery.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
/*
Location lookup
*/
function getGpsZip(zip) {
/*
Lookup in DAWA and return result based on zip.
dawa.aws.dk
*/
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
getGpsZipRes(this);
}
};
xhr.open("GET", "https://dawa.aws.dk/postnumre/" + zip);
xhr.send(xhr);
}
function getGpsZipRes(xhr) {
/*
Outputs coordinates
*/
var res = JSON.parse(xhr.responseText);
console.log(res["visueltcenter"][0]);
console.log(res["visueltcenter"][1]);
}
function getGpsStreet(street, zip) {
/*
Lookup in DAWA and return result based on street and zip.
dawa.aws.dk
*/
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
getGpsStreetRes(this);
}
};
xhr.open("GET", "https://dawa.aws.dk/adresser?q=" + street + "&postnr=" + zip);
xhr.send(xhr);
}
function getGpsStreetRes(xhr) {
/*
Outputs coordinates
*/
var res = JSON.parse(xhr.responseText);
console.log(res[0]["adgangsadresse"]["adgangspunkt"]["koordinater"][0]);
console.log(res[0]["adgangsadresse"]["adgangspunkt"]["koordinater"][1]);
}
function getAddressClean(address) {
/*
Input => Snarregade 33
Output => Snarregade
Input => Skovens vej 44
Output => Skovens vej
*/
return (address.replace(/\'/g, '').split(/(\d+)/).filter(Boolean))[0].trim();
}
/*
Delivery lookup
*/
function getBringShops(zip, street) {
/*
Return all bring pakkeshops based on zip and street.
developer.bring.com
*/
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
getBringShopsRes(this);
}
};
xhr.open("GET", "https://api.bring.com/pickuppoint/api/pickuppoint/DK/postalCode/" + zip + ".json?street=" + street);
xhr.send(xhr);
}
function getBringShopsRes(xhr) {
/*
Parses bring pakkeshops.
*/
var res = JSON.parse(xhr.responseText);
var resFind = res["pickupPoint"];
resFind.forEach((item) => {
console.log(item["unitId"]);
console.log(item["name"]);
console.log(item["visitingAddress"]);
console.log(item["visitingPostalCode"]);
});
}
function getPostnordShops(api, zip, street) {
/*
Return all Post Nord pakkeshops based on zip and street.
An API-key is required.
developer.postnord.com
*/
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
getPostnordShopsRes(this);
}
};
xhr.open("GET", "https://api2.postnord.com/rest/businesslocation/v1/servicepoint/findNearestByAddress.json?apikey=" + api + "&countryCode=DK&agreementCountry=DK&postalCode=" + zip + "&streetName=" + street);
xhr.send(xhr);
}
function getPostnordShopsRes(xhr) {
/*
Parses Post Nord pakkeshops.
*/
var res = JSON.parse(xhr.responseText);
var resFind = res["servicePointInformationResponse"]["servicePoints"];
resFind.forEach((item) => {
console.log(item["servicePointId"]);
console.log(item["name"]);
console.log(item["visitingAddress"]["streetName"]);
console.log(item["visitingAddress"]["streetNumber"]);
console.log(item["visitingAddress"]["postalCode"]);
});
}