This repository was archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (44 loc) · 1.32 KB
/
index.js
File metadata and controls
49 lines (44 loc) · 1.32 KB
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
const got = require('got');
const iconv = require('iconv-lite');
const urlencode = require('urlencode');
class FutureDiary {
constructor(opt) {
this.host = opt.host || 'weilairiji.com';
this.username = opt.username || '';
this.password = opt.password || '';
this.endpoint = `http://${this.host}`;
}
async get(uri, parameters) {
const url = this.endpoint + '/api' + uri + '.json';
const {body} = await got.get(url, {
encoding: null,
auth: `${this.username}:${this.password}`,
query: parameters
});
const result = iconv.decode(body, 'gb2312');
return FutureDiary._parseJson(result);
}
async post(uri, parameters) {
const url = this.endpoint + '/api' + uri + '.json';
const {body} = await got.post(url, {
encoding: null,
auth: `${this.username}:${this.password}`,
headers: {'content-type': 'application/x-www-form-urlencoded; charset=gb2312'},
from: true,
body: urlencode.stringify(parameters, {charset: 'gb2312'})
});
const result = iconv.decode(body, 'gb2312');
return FutureDiary._parseJson(result);
}
static _parseJson(string) {
const result = string
.replace(/"reply_to_status_id"="/g, '"reply_to_status_id":"')
.replace(/"reply_to_user_id"="/g, '"reply_to_user_id":"');
try {
return JSON.parse(result);
} catch {
return result;
}
}
}
module.exports = FutureDiary;