-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethercalc-client.js
126 lines (112 loc) · 3.38 KB
/
ethercalc-client.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
const { ExportToCsv } = require('export-to-csv');
const request = require('request-promise-native');
var testdata = [
{
url: "asdasdas",
text: "iiiiixxxxxxi99999"
}
];
const options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalSeparator: '.',
showLabels: true,
showTitle: false,
title: 'My Awesome CSV',
useTextFile: false,
useBom: true,
useKeysAsHeaders: true,
};
const csvExporter = new ExportToCsv(options);
const putPage = async function (padname, data) {
const csvstr = csvExporter.generateCsv(data, true);
const responseBody = await request({
url: `https://ethercalc.org/_/${padname}`,
method: 'PUT',
headers: {
'Content-Type': 'text/csv'
},
body: csvstr,
})
return responseBody
}
const getPage = async function (padname) {
const response = await request({
url: `https://ethercalc.org/_/${padname}/csv.json`,
method: 'GET',
resolveWithFullResponse: true,
simple: false,//fulfill the promise on 404 as well
})
if ( response.statusCode === 200 ) {
const responseData = JSON.parse(response.body)
if ( Array.isArray(responseData) && Array.isArray(responseData[0]) && responseData[0][0] === '' ) {//this means the page exists but is empty
console.debug('getPage: exists but empty');
return [];
}
const [headLine, ...lines] = responseData
return lines.map(line => line.reduce((obj, v, i) => (obj[headLine[i]] = v, obj), {}))
} else if ( response.statusCode === 404 ) {// page does not exist
return [];
} else {
throw new Error('Getting Etherpad '+padname+' failed!');
}
}
const verifyPage = async function (padname) {
const response = await request({
url: `https://ethercalc.org/_/${padname}/csv.json`,
method: 'GET',
resolveWithFullResponse: true,
simple: false,//fulfill the promise on 404 as well
})
if ( response.statusCode === 200 ) {
const responseData = JSON.parse(response.body)
if ( Array.isArray(responseData) && Array.isArray(responseData[0]) && responseData[0][0] === '' ) {//this means the page exists but is empty
console.debug('getPage: exists but empty');
process.exit(1);
}
const [headLine, ...lines] = responseData
var data = lines.map(line => line.reduce((obj, v, i) => (obj[headLine[i]] = v, obj), {}))
if ( Object.keys(data[0]).indexOf('url') === -1 ) {
console.log('Data does not contain `url` column, data format invalid');
process.exit(4);
} else {
process.exit(0);
}
} else if ( response.statusCode === 404 ) {// page does not exist
console.debug('Page does not exist');
process.exit(2);
} else {
process.exit(3);
}
}
// TODO: this method is not used for now but kept for future use
const updatePage = async function (padname, newdata) {
let olddata = await getPage(padname);
const merged = [...olddata.concat(newdata).reduce((m, o) =>
m.set(o.url, Object.assign(m.get(o.url) || {}, o))
, new Map()).values()];
return putPage(padname, merged);
}
module.exports = {
putPage,
getPage,
updatePage,
verifyPage,
}
const test = async function () {
const testdata = [
{
url: 'https://facebook.com/1',
text: '安安,你好!'
},
{
url: 'https://facebook.com/2',
text: '注意!\n感謝你的注意!'
},
];
const pageName = 'hello_test_page'
const putResult = await putPage(pageName, testdata)
console.log('putResult', putResult)
const getResult = await getPage(pageName)
console.log('getResult', getResult)
}