Skip to content

Instantly share code, notes, and snippets.

@trungnt2910
Last active January 17, 2023 10:35
Show Gist options
  • Save trungnt2910/312f73e6969f4185a12e027449d9ba04 to your computer and use it in GitHub Desktop.
Save trungnt2910/312f73e6969f4185a12e027449d9ba04 to your computer and use it in GitHub Desktop.
Lấy điểm từ https://tracuu.hanoi.edu.vn/
async function FetchScore(sbd) {
// The magic string, obtained by inspecting the GET request.
const magicString = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKLTM5NDc5MjE0NQ9kFgICAw9kFgICAQ88KwAIAQAPFgIeDl8hVXNlVmlld1N0YXRlZ2QWAmYPZBYCAgEPZBYEZg9kFgJmD2QWAmYPZBYGAgEPPCsABgEADxYCHgVWYWx1ZQUGMDQwMTcwZGQCBQ88KwARAwAPFgQeC18hRGF0YUJvdW5kZx4LXyFJdGVtQ291bnQCAWQBEBYBAgEWATwrAAUBABYCHgpIZWFkZXJUZXh0BQRNw7RuFgFmDBQrAAAWAmYPZBYEAgEPZBYGZg8PFgIeBFRleHQFBjA0MDE3MGRkAgEPDxYCHwUFCVRpbiBo4buNY2RkAgIPDxYCHwUFBTE4LjYwZGQCAg8PFgIeB1Zpc2libGVoZGQCBw8PZA8QFgFmFgEWAh4OUGFyYW1ldGVyVmFsdWUFBjA0MDE3MBYBZmRkAgEPZBYCZg9kFgJmD2QWBgIBDzwrAAYBAA8WAh8BZWRkAgUPPCsAEQMADxYEHwJnHwNmZAEQFgAWABYADBQrAABkAgcPD2QPEBYBZhYBFgIfB2UWAWZkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAwUQQVNQeFBhZ2VDb250cm9sMQUcQVNQeFBhZ2VDb250cm9sMSRBU1B4QnV0dG9uMQUcQVNQeFBhZ2VDb250cm9sMSRBU1B4QnV0dG9uM1HJZ%2F6WUqpwzIZjiuN%2Bv3%2B%2BWi0ZgoC1gwbhFOmikFiR&__EVENTVALIDATION=%2FwEdAAJbZEDZ%2BCVXGieReMH5wELh5tBVQB%2FetO1iD9MZSDo%2FTK96XZ27Y9J6lgSiWAo2LpXTLbkK7HrDY%2BpYi4Z49jxL&ASPxPageControl1=%7B%26quot%3BactiveTabIndex%26quot%3B%3A0%7D&ASPxPageControl1%24ASPxTextBox1%24State=%7B%26quot%3BrawValue%26quot%3B%3A%26quot%3B040170%26quot%3B%7D&ASPxPageControl1%24ASPxTextBox1=040170&ASPxPageControl1%24ASPxButton1=Xem&ASPxPageControl1%24ASPxTextBox3%24State=%7B%26quot%3BrawValue%26quot%3B%3A%26quot%3B%26quot%3B%7D&ASPxPageControl1%24ASPxTextBox3=Nh%E1%BA%ADp+S%E1%BB%91+b%C3%A1o+danh+&DXScript=1_304%2C1_185%2C1_298%2C1_211%2C1_188%2C1_182%2C1_296%2C1_299%2C1_209%2C1_217%2C1_198&DXCss=1_40%2C1_50%2C1_53%2C1_51%2C1_4%2C1_17%2C1_14%2C1_16%2C1_13%2C0_3962%2C0_3966";
// The initial number used to obtain the magic string.
const magicInitialNumber = "040170"
// The element ID of the table containing the results.
const magicTableId = "ASPxPageControl1_GridView1"
let table = new DOMParser().parseFromString(await (await (fetch("https://tracuu.hanoi.edu.vn/", {
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language": "en-US,en;q=0.9,vi;q=0.8",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded",
"pragma": "no-cache",
"upgrade-insecure-requests": "1"
},
"referrer": "https://tracuu.hanoi.edu.vn/",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": magicString.replaceAll(magicInitialNumber, sbd),
"method": "POST",
"mode": "cors",
"credentials": "omit"
}))).text(), "text/html").getElementById(magicTableId).children[0];
let headerRow = table.children[0];
let dataRow = table.children[1];
let obj = {};
for (let i = 0; i < headerRow.children.length; ++i) {
obj[headerRow.children[i].innerText] = dataRow.children[i].innerText;
}
return obj;
}
async function Loop(prefix, first, last) {
let a = [];
for (let i = first; i <= last; ++i) {
let sbd = prefix.toString() + i.toString();
try {
a.push(await FetchScore(sbd));
} catch (e) {
console.error("Không thấy kết quả cho " + sbd + ", lỗi: " + e);
}
}
return a;
}
async function GetScores(prefix, first, last, config) {
var arr = await Loop(prefix, first, last);
if (config.sort) {
arr = arr.sort((a, b) => {
if (a.diem == "Bỏ thi" && b.diem == "Bỏ thi") return 0;
if (a.diem == "Bỏ thi") return 1;
if (b.diem == "Bỏ thi") return -1;
return Number.parseFloat(b[config.scoreColumnName]) - Number.parseFloat(a[config.scoreColumnName]);
});
}
return arr;
}
// Get scores from 040170 to 040182
const prefix = "0401"
const first = 70;
const last = 82;
const scoreColumnName = "Kết quả";
const sort = false;
console.log(await GetScores(prefix, first, last, {scoreColumnName: scoreColumnName, sort: sort}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment