-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Rate Limiter logic. Use limiter package instead of trying to r…
…einvent the wheel
- Loading branch information
Showing
8 changed files
with
824 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import fetch from "node-fetch"; | ||
// import type { PubChemCompound } from "./types"; | ||
// import getNecessaryData from "./parseData.js"; | ||
import { RateLimiter } from "limiter"; | ||
import { RawCompound } from "./types"; | ||
import getNecessaryData from "./parseData.js"; | ||
|
||
const _parseInt = (x: string) => parseInt(x); | ||
|
||
const [startId, endId, compoundId] = process.argv.slice(2).map(_parseInt); | ||
const API_URL = "https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/data/compound/"; | ||
|
||
async function fetchJson(compoundId: number): Promise<RawCompound> { | ||
return (await (await fetch(API_URL + compoundId + "/JSON")).json()) as Promise<RawCompound>; | ||
} | ||
|
||
async function init() { | ||
const ids: number[] = []; | ||
|
||
for (let i = compoundId; i < compoundId + (endId - startId + 1); i++) { | ||
ids.push(i); | ||
} | ||
|
||
if (ids.length === 0) { | ||
return; | ||
} | ||
|
||
const limiter = new RateLimiter({ tokensPerInterval: 1, interval: 250 }); | ||
|
||
async function sendMessage(id: number) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const remainingMessages = await limiter.removeTokens(1); | ||
// const id = getId(); | ||
if (id === undefined) { | ||
return; | ||
} | ||
const result = await fetchJson(id); | ||
return getNecessaryData(result.Record); | ||
} | ||
|
||
ids.forEach(async (id) => { | ||
await sendMessage(id).then((e) => { | ||
console.log(e?.RecordTitle); | ||
}); | ||
}); | ||
} | ||
|
||
init(); |
Oops, something went wrong.