diff --git a/src/services/eval/stock.js b/src/services/eval/stock.js index 686527b..177a594 100644 --- a/src/services/eval/stock.js +++ b/src/services/eval/stock.js @@ -1,5 +1,4 @@ import got from "got"; -import cheerio from "cheerio"; export async function fetchStock(stockCode) { if (typeof stockCode !== "string") throw "Kode saham harus berupa string"; @@ -7,10 +6,13 @@ export async function fetchStock(stockCode) { throw `Kode saham ${stockCode} tidak valid`; } + const requestSearchParams = new URLSearchParams(); + requestSearchParams.set("stockCode", stockCode); + const { statusCode, body } = await got.get( - `https://www.duniainvestasi.com/bei/summaries/${stockCode}`, + `https://gold.teknologiumum.com/stock?${requestSearchParams.toString()}`, { - responseType: "text", + responseType: "json", throwHttpErrors: false } ); @@ -19,55 +21,18 @@ export async function fetchStock(stockCode) { throw `Gagal mendapatkan data saham ${stockCode}`; } + const response = { + name: body.symbol, + close: body.close, + previous: body.open, + high: body.high, + low: body.low, + volume: body.volume, + value: body.close + }; - const $ = cheerio.load(body); - const name = $("#CONTENT h3").text(); - if (/Kode saham ".*" tidak ditemukan./.test(name)) throw name; + if (response.name === "") throw stockCode; - return { - name: name, - close: parseInt( - $( - "#CONTENT>:first-child>:nth-last-child(2)>:first-child>:nth-child(2)>:first-child>:last-child>div" - ) - .text() - .replaceAll(",", "") - ), - previous: parseInt( - $( - "#CONTENT>:first-child>:nth-last-child(2)>:first-child>:nth-child(2)>:nth-child(2)>:last-child>div" - ) - .text() - .replaceAll(",", "") - ), - high: parseInt( - $( - "#CONTENT>:first-child>:nth-last-child(2)>:first-child>:nth-child(2)>:nth-child(5)>:last-child>div" - ) - .text() - .replaceAll(",", "") - ), - low: parseInt( - $( - "#CONTENT>:first-child>:nth-last-child(2)>:first-child>:nth-child(2)>:nth-child(6)>:last-child>div" - ) - .text() - .replaceAll(",", "") - ), - volume: parseInt( - $( - "#CONTENT>:first-child>:nth-last-child(2)>:first-child>:nth-child(3)>:first-child>:last-child>div" - ) - .text() - .replaceAll(",", "") - ), - value: parseInt( - $( - "#CONTENT>:first-child>:nth-last-child(2)>:first-child>:nth-child(3)>:nth-child(3)>:last-child>div" - ) - .text() - .replaceAll(",", "") - ) - }; + return response; } diff --git a/src/services/eval/superpowers.js b/src/services/eval/superpowers.js index 473ddf2..f928779 100644 --- a/src/services/eval/superpowers.js +++ b/src/services/eval/superpowers.js @@ -19,26 +19,13 @@ export async function resolveStocks(source) { } // 4 or more stockCodes: throw error - if (stockCodes.size > 3) throw "Terlalu banyak kode saham"; + if (stockCodes.size > 5) throw "Terlalu banyak kode saham"; - let i = 0; for (const stockCode of stockCodes) { - // 1 stockCode: no delay - // 2 stockCodes: 5000ms delay - // 3 stockCodes: 2500ms delay - /* eslint-disable no-await-in-loop */ - if (i > 0) { - await new Promise((resolve) => - setTimeout(resolve, 5000 / (stockCodes.size - 1)) - ); - } - /* eslint-enable no-await-in-loop */ - /* eslint-disable no-await-in-loop */ const stock = await fetchStock(stockCode); /* eslint-enable no-await-in-loop */ stockByCode[stockCode] = stock; - i++; } // sort descending to prevent shorter queries to be diff --git a/tests/eval.test.js b/tests/eval.test.js index cbf8c27..7e17e18 100644 --- a/tests/eval.test.js +++ b/tests/eval.test.js @@ -226,10 +226,10 @@ test("should not be able to evaluate ASII without dollar sign", async () => { assert.equal(result, "Tidak boleh mengakses ASII"); }); -test("should not be able to evaluate more than 3 cashtags", async () => { +test("should not be able to evaluate more than 5 cashtags", async () => { let result; try { - result = await safeEval("$ASII + $BBCA + $GGRM + $TLKM", [resolveStocks]); + result = await safeEval("$ASII + $BBCA + $GGRM + $TLKM + $AAPL + $MSFT", [resolveStocks]); } catch (error) { result = error; }