Skip to content

Commit

Permalink
Merge pull request #215 from teknologi-umum/feat/eval-stock-use-gold
Browse files Browse the repository at this point in the history
feat(eval): use gold for acquiring stock data
  • Loading branch information
elianiva authored Jan 8, 2024
2 parents dd91d9f + 034e612 commit 5137125
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 67 deletions.
67 changes: 16 additions & 51 deletions src/services/eval/stock.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import got from "got";
import cheerio from "cheerio";

export async function fetchStock(stockCode) {
if (typeof stockCode !== "string") throw "Kode saham harus berupa string";
if (!/^[A-Z]{4}(?:-[A-Z][A-Z\d]{0,2})?$/.test(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
}
);
Expand All @@ -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;
}
15 changes: 1 addition & 14 deletions src/services/eval/superpowers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/eval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 5137125

Please sign in to comment.