-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathapi.js
89 lines (79 loc) · 2.23 KB
/
api.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
import https from "https"
import { decode } from "./api_utils.js"
class API {
WORDS_TYPE = {
"NEW": "NEW",
"REVIEW": "REVIEW"
}
baseOptions = {
hostname: "apiv3.shanbay.com",
method: "GET",
"Content-Type": "application/json",
headers: {},
};
constructor(cookie) {
this.baseOptions.headers["Cookie"] = cookie
}
async getDefaultMaterialBookIdApi() {
const materialBookOpts = { ...this.baseOptions, path: '/wordsapp/user_material_books/current' };
return new Promise((resolve, reject) => {
const req = https.request(materialBookOpts, (res) => {
let results = '';
res.on('data', (chunk) => {
results = results + chunk;
})
res.on("end", () => {
try {
const id = JSON.parse(results).materialbook_id;
resolve(id);
} catch (e) {
reject(e.message);
}
})
})
req.on('error', reject);
req.end();
})
}
async getWordsInPageApi(page, materialBookId, wordsType) {
const newOptions = {
...this.baseOptions,
path: `/wordsapp/user_material_books/${materialBookId}/learning/words/today_learning_items?ipp=10&page=${page}&type_of=${wordsType}`
}
return new Promise((resolve, reject) => {
let results = "";
let req = https.request(newOptions,
function (res) {
res.on("data", function (chunk) {
results = results + chunk;
});
res.on("end", async function () {
const toDecodeData = JSON.parse(results).data
// if you are not remember new word, send nothing
if (!toDecodeData) {
return
}
const resultJson = decode(toDecodeData);
resolve(resultJson)
})
});
req.on("error", reject);
req.end();
});
}
async getWordsAllApi(materialBookId, wordsType) {
let page = 1;
let words = [];
while (true) {
const data = await this.getWordsInPageApi(page, materialBookId, wordsType);
const wordsInPage = data.objects;
if (wordsInPage.length === 0) {
break;
}
words = words.concat(wordsInPage);
page++;
}
return words;
}
}
export default API;