Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eval): use gold for acquiring stock data #215

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

Check warning on line 11 in src/services/eval/stock.js

View check run for this annotation

Codecov / codecov/patch

src/services/eval/stock.js#L9-L11

Added lines #L9 - L11 were not covered by tests
const { statusCode, body } = await got.get(
`https://www.duniainvestasi.com/bei/summaries/${stockCode}`,
`https://gold.teknologiumum.com/stock?${requestSearchParams.toString()}`,

Check warning on line 13 in src/services/eval/stock.js

View check run for this annotation

Codecov / codecov/patch

src/services/eval/stock.js#L13

Added line #L13 was not covered by tests
{
responseType: "text",
responseType: "json",

Check warning on line 15 in src/services/eval/stock.js

View check run for this annotation

Codecov / codecov/patch

src/services/eval/stock.js#L15

Added line #L15 was not covered by tests
throwHttpErrors: false
}
);
Expand All @@ -19,55 +21,18 @@
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
};

Check warning on line 32 in src/services/eval/stock.js

View check run for this annotation

Codecov / codecov/patch

src/services/eval/stock.js#L24-L32

Added lines #L24 - L32 were not covered by tests
Comment on lines +24 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

biar apa wkwkwk

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gainget


const $ = cheerio.load(body);
const name = $("#CONTENT h3").text();

if (/Kode saham ".*" tidak ditemukan./.test(name)) throw name;
if (response.name === "") throw stockCode;

Check warning on line 35 in src/services/eval/stock.js

View check run for this annotation

Codecov / codecov/patch

src/services/eval/stock.js#L35

Added line #L35 was not covered by tests

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;

Check warning on line 37 in src/services/eval/stock.js

View check run for this annotation

Codecov / codecov/patch

src/services/eval/stock.js#L37

Added line #L37 was not covered by tests
}
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
Loading