throttle requests

This commit is contained in:
karl.hudgell 2020-06-24 10:54:55 +01:00
parent 1e00780395
commit 46c8642534
3 changed files with 14 additions and 4 deletions

10
app.js
View File

@ -1,9 +1,10 @@
require('dotenv').config();
var request = require("request");
cachedRequest = require('cached-request')(request)
cacheDirectory = "./cache";
cachedRequest.setCacheDirectory(cacheDirectory);
var throttledRequest = require('throttled-request')(request);
throttledRequest.configure({requests: 8, milliseconds: 60000}); //send 1 request per second
cachedRequest = require('cached-request')(throttledRequest);
cachedRequest.setCacheDirectory("./cache");
const logger = require('perfect-logger');
const TelegramBot = require('node-telegram-bot-api');
@ -84,6 +85,7 @@ bot.on('message', (msg) => {
};
cachedRequest(options, function (error, response, body) {
console.log(response)
if (error) throw new Error(error);
// console.log(body);
@ -123,6 +125,8 @@ bot.on('message', (msg) => {
};
request(options, function (error, response, body) {
console.log(response)
if (error) throw new Error(error);
let jsonBody = JSON.parse(body)
bot.sendMessage(msg.chat.id, jsonBody.content)

5
package-lock.json generated
View File

@ -772,6 +772,11 @@
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-1.2.0.tgz",
"integrity": "sha1-/x7R5hFp0Gs88tWI4YixjYhH4X4="
},
"throttled-request": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/throttled-request/-/throttled-request-0.1.1.tgz",
"integrity": "sha1-QUa3C3elrsozxGMyzxPrnHkelxs="
},
"to-iso-string": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/to-iso-string/-/to-iso-string-0.0.2.tgz",

View File

@ -19,6 +19,7 @@
"node-telegram-bot-api": "^0.50.0",
"perfect-logger": "^2.0.1",
"rapidapi-connect": "0.0.6",
"request": "^2.88.2"
"request": "^2.88.2",
"throttled-request": "^0.1.1"
}
}