Skip to content

Commit

Permalink
Change Rate Limiter logic. Use limiter package instead of trying to r…
Browse files Browse the repository at this point in the history
…einvent the wheel
  • Loading branch information
MT410 committed Feb 18, 2022
1 parent ac64ae9 commit 49c88bc
Show file tree
Hide file tree
Showing 8 changed files with 824 additions and 6 deletions.
7 changes: 5 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"no-console": 1,
"eqeqeq": "warn",
"no-cond-assign": 0,
"no-unused-vars": 1,
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error"
],
"no-extra-semi": "warn",
"semi": "warn"
},
Expand All @@ -25,4 +28,4 @@
"experimentalObjectRestSpread": true
}
}
}
}
156 changes: 155 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
"dev": "nodemon --ext ts --exec \"npm run build-dev\"",
"start": "npm run build && NODE_ENV=production node dist/app.js",
"test": "NODE_ENV=test mocha --check-leaks -r ts-node/register -r tsconfig-paths/register \"test/**/*.spec.ts\"",
"test:watch": "NODE_ENV=test mocha --check-leaks -r ts-node/register -r tsconfig-paths/register \"test/**/*.spec.ts\" -w --watch-files \"test/**/*.spec.ts\",\"src/**/*.ts\""
"test:watch": "NODE_ENV=test mocha --check-leaks -r ts-node/register -r tsconfig-paths/register \"test/**/*.spec.ts\" -w --watch-files \"test/**/*.spec.ts\",\"src/**/*.ts\"",
"import:pubchem": "node dist/pubchem-import/index.js"
},
"dependencies": {
"express": "^4.17.2"
"express": "^4.17.2",
"limiter": "^2.0.1",
"node-fetch": "^3.2.0"
},
"type": "module",
"devDependencies": {
Expand All @@ -33,4 +36,4 @@
"tsconfig-paths": "^3.12.0",
"typescript": "^4.5.5"
}
}
}
48 changes: 48 additions & 0 deletions src/pubchem-import/index.ts
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();
Loading

0 comments on commit 49c88bc

Please sign in to comment.