-
Notifications
You must be signed in to change notification settings - Fork 1
/
comment.js
87 lines (78 loc) · 2.64 KB
/
comment.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
const fetch = require('node-fetch');
function objectToUrlEncoded(obj) {
return Object.keys(obj).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`).join('&');
}
async function fetchData(baseUrl, queryParams, bodyObject, headers = { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }) {
const queryString = objectToUrlEncoded(queryParams);
const url = `${baseUrl}?${queryString}`;
const body = objectToUrlEncoded(bodyObject);
try {
const response = await fetch(url, { method: 'POST', headers, body });
return await response.text();
} catch (error) {
console.error('Error:', error);
throw error; // 外部调用会处理异常
}
}
function processData(rawData) {
const lines = rawData.split('\n');
const cleanData = lines[3]?.trim();
const json = JSON.parse(cleanData);
const data = json[0][2];
return data;
}
const getExtComments = async (keyword, options = {}) => {
const {
limit = 10,
lang = "all",
sort = "recent"
} = options;
function sortValue(input) {
switch(input) {
case "helper":
return 1;
case "recent":
return 2;
case "low-rate-first":
return 3;
case "high-rate-first":
return 4;
}
}
const baseUrl = 'https://chromewebstore.google.com/_/ChromeWebStoreConsumerFeUi/data/batchexecute';
const queryParams = {
'rpcids': 'x1DgCd',
'bl': 'boq_chrome-webstore-consumerfe-ui_20240428.07_p0',
'hl': 'zh-CN',
'soc-app': '1',
'soc-platform': '1',
'soc-device': '1',
'rt': 'c'
};
const bodyObject = {
'f.req': `[[["x1DgCd","[\\"${keyword}\\",[${limit}],${sortValue(sort)},null,null,[\\"${lang}\\"]]",null,"generic"]]]`
};
try {
const rawData = await fetchData(baseUrl, queryParams, bodyObject);
const comments = processData(rawData);
// const result = JSON.prase(comments);
const parsedData = JSON.parse(comments);
const usefulData = parsedData[1].map(entry => {
return {
userId: entry[0],
name: entry[1][0],
avatarUrl: entry[1][1],
rating: entry[2],
review: entry[3],
version: entry[11]
};
});
const num = usefulData.length;
return { success: true, error: null, number: num, data: usefulData };
} catch (error) {
return { success: false, error: error.message, data: null };
}
};
module.exports = {
getExtComments
};