Skip to content

Commit e287072

Browse files
committed
Initial commit
0 parents  commit e287072

5 files changed

Lines changed: 123 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.idea
3+
.vscode
4+
package-lock.json

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Express API
2+
======
3+
4+
## Examples
5+
6+
Read codes in [index.js](./index.js)
7+
8+
### Init
9+
10+
```javascript
11+
const ExpressApi = require('./src/api');
12+
const api = new ExpressApi();
13+
const api = new ExpressApi({ tokenV2, cookie }); // Optional, you can load tokenV2 and cookie from last state. But we recommend to init new token every launch.
14+
15+
await api.init(); // If tokenV2 and cookie is loaded from last state, you can skip this call.
16+
```
17+
18+
### Get
19+
20+
```javascript
21+
const data = await api.get(packageNo, {company: companyCode});
22+
```
23+
24+
If you don't know which company is carrying this package, you can ignore `company` options.
25+
26+
```javascript
27+
const data = await api.get(packageNo);
28+
```
29+
30+
## License
31+
32+
No Licenses. Just use as you like.

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const ExpressApi = require('./src/api');
2+
3+
async function main(num) {
4+
const api = new ExpressApi();
5+
const tokenV2 = await api.init();
6+
console.log(`TokenV2: ${tokenV2}`);
7+
const result = await api.get(num);
8+
console.log(JSON.stringify(result, null, 2));
9+
}
10+
11+
if (process.argv.length < 3) {
12+
console.log('Please specify a package number to query. For example: "node ./index.js 123456789012"');
13+
return;
14+
}
15+
main(process.argv[2]);

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "du-express-api",
3+
"version": "0.0.1",
4+
"description": "Example of using Du Express Api",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js"
8+
},
9+
"author": "fython",
10+
"license": "MIT",
11+
"dependencies": {
12+
"axios": "^0.19.2",
13+
"axios-cookiejar-support": "^1.0.0",
14+
"tough-cookie": "^4.0.0"
15+
}
16+
}

src/api.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const axios = require('axios');
2+
const axiosCookieJarSupport = require('axios-cookiejar-support').default;
3+
const tough = require('tough-cookie');
4+
5+
const GET_TOKEN_URL = 'https://www.baidu.com/baidu?isource=infinity&iname=baidu&itype=web&tn=02003390_42_hao_pg&ie=utf-8&wd=%E5%BF%AB%E9%80%92';
6+
const TOKEN_PATTERN = /tokenV2=(.*?)"/;
7+
const GET_EXPRESS_URL = 'https://express.baidu.com/express/api/express';
8+
const BROWSER_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36';
9+
10+
class ExpressApi {
11+
constructor({ cookie, tokenV2 } = {}) {
12+
this.tokenV2 = tokenV2;
13+
this.axios = axios.create({
14+
withCredentials: true,
15+
headers: {
16+
'User-Agent': BROWSER_UA,
17+
}
18+
});
19+
axiosCookieJarSupport(this.axios);
20+
this.axios.defaults.jar = new tough.CookieJar();
21+
if (cookie) {
22+
this.axios.defaults.headers.common['Cookie'] = cookie;
23+
}
24+
}
25+
26+
async init() {
27+
const res = await this.axios.get(GET_TOKEN_URL);
28+
if (res.status !== 200) {
29+
throw new Error('Cannot get token by du search pages');
30+
}
31+
const pageData = res.data;
32+
const regRes = TOKEN_PATTERN.exec(pageData);
33+
if (regRes.length <= 0) {
34+
throw new Error('Cannot find token from response data');
35+
}
36+
this.tokenV2 = regRes[1];
37+
return this.tokenV2;
38+
}
39+
40+
async get(num, { company } = {}) {
41+
const opts = {
42+
params: {
43+
tokenV2: this.tokenV2,
44+
appid: 4001,
45+
nu: num,
46+
},
47+
};
48+
if (company) {
49+
opts.params.com = company;
50+
}
51+
const res = await this.axios.get(GET_EXPRESS_URL, opts);
52+
return res.data;
53+
}
54+
}
55+
56+
module.exports = ExpressApi;

0 commit comments

Comments
 (0)