Skip to content

Commit

Permalink
Updated README, added DHL & cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeTurtle committed Jul 23, 2021
1 parent e0e2713 commit 88de4a5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Node.js module that interfaces with Greek courier companies.
- Γενική Ταχυδρομική (taxydromiki.com)
- Speedex (speedex.com)
- EasyMail (easymail.gr)
- DHL (dhl.com)
- ..and more to come!

## Usage example
Expand All @@ -17,6 +18,7 @@ const geniki = new(require("courier-api").geniki);
const easymail = new(require("courier-api").easymail);
const elta = new(require("courier-api").elta);
const acs = new(require("courier-api").acs);
const dhl = new(require("courier-api").dhl);


(async () => {
Expand All @@ -39,11 +41,15 @@ const acs = new(require("courier-api").acs);
await easymail.get('TRACK_ID')
.then((r) => console.log(r))
.catch((e) => console.error(e));

await dhl.get('TRACK_ID')
.then((r) => console.log(r))
.catch((e) => console.error(e));
});
```
or
```js
const { geniki, speedex, elta, acs, easymail } = require('courier-api');
const { geniki, speedex, elta, acs, easymail, dhl } = require('courier-api');
(async () => {
await new elta().get('TRACK_ID')
.then((result) => console.log(result))
Expand All @@ -62,7 +68,11 @@ const { geniki, speedex, elta, acs, easymail } = require('courier-api');
.catch((error) => console.error(error));

await new easymail().get('TRACK_ID')
.then((r) => console.log(r))
.catch((e) => console.error(e));
.then((result) => console.log(result))
.catch((error) => console.error(error));

await new dhl().get('TRACK_ID')
.then((result) => console.log(result))
.catch((error) => console.error(error));
})();
```
48 changes: 41 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const axios = require("axios");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

class elta {
get(tr) {
Expand All @@ -20,7 +21,6 @@ class elta {
class geniki {
get(tr) {
return new Promise(async(resolve, reject) => {
const { JSDOM } = jsdom;
const data = await axios.get(`https://www.taxydromiki.com/track/${tr}`);
const dom = new JSDOM(data.data, { url: 'https://www.taxydromiki.com' });
const trDOM = new JSDOM(dom.window.document.getElementById('edit-content').innerHTML);
Expand Down Expand Up @@ -55,7 +55,6 @@ class geniki {
class speedex {
get(tr) {
return new Promise(async(resolve, reject) => {
const { JSDOM } = jsdom;
const data = await axios.get(`http://www.speedex.gr/speedex/NewTrackAndTrace.aspx?number=${tr}`);
if (data.data.includes('Δεν βρέθηκαν')) reject({ "status": "no result" })
const dom = new JSDOM(data.data, { url: 'http://www.speedex.gr' });
Expand Down Expand Up @@ -99,6 +98,7 @@ class acs {
get(tr) {
return new Promise(async(resolve, reject) => {
const res = await axios.get(`https://api.acscourier.net/api/parcels/search/${tr}`, {headers: {'accept-language': 'el', 'X-Country': 'GR'}});
if(JSON.parse(JSON.stringify(res.data.count)) == 0) return reject('{"status": "no result"}');
const data = JSON.parse(JSON.stringify(res.data.items[0].statusHistory));
let obj = 0;
let trData = {};
Expand All @@ -112,10 +112,8 @@ class acs {
trData[obj].time = e.controlPointDate.split("T")[1].split('.')[0];

obj += 1;

// 2021-07-19 T 10:16:25.2
})
console.log(Object.values(trData));
resolve(Object.values(trData));
})
}
}
Expand All @@ -124,7 +122,6 @@ class easymail {
get(tr) {
return new Promise(async(resolve, reject) => {
const data = await axios.get(`https://trackntrace.easymail.gr/${tr}`);
const { JSDOM } = jsdom;
const response = data.data;
const dom = new JSDOM(response);
let number = 0;
Expand Down Expand Up @@ -158,4 +155,41 @@ class easymail {
}
}

module.exports = { elta, geniki, speedex, acs, easymail };
class dhl {
get(tr) {
return new Promise(async(resolve, reject) => {
axios.get(`https://api-eu.dhl.com/track/shipments?trackingNumber=${tr}&language=el`, {headers: {'DHL-API-Key': '0kCkU1GnqL0DH434TnFdX4zBznXDQTa4'}})
.then((res) => {
const origin = res.data.shipments[0].events;
let obj = 0;
let trData = {};
origin.forEach((e) => {
if (!trData[obj]) trData[obj] = {};

trData[obj].status = e.description;
trData[obj].place = e.location.address.addressLocality;
trData[obj].date = e.timestamp.split("T")[0];
trData[obj].time = e.timestamp.split("T")[1];

obj += 1;
})
resolve(Object.values(trData).reverse());
})
.catch((err) => {
switch(err.response.status) {
case 404:
reject('{"status": "no result"}');
break;
case 429:
reject('{"status": "ratelimited"}');
break;
default:
reject('{"status": "unspecified error"}');
break;
}
})
})
}
}

module.exports = { elta, geniki, speedex, acs, easymail, dhl };

0 comments on commit 88de4a5

Please sign in to comment.