From c7d1b1cd96906c686dc5efe532444f26c3661488 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 20 Jun 2023 09:25:57 +0200 Subject: [PATCH] Second commit --- .gitignore | 3 ++- README.md | 9 ++++++++- config_template.yml | 9 +++++++++ main.js | 38 ++++++++++++++++++++------------------ package-lock.json | 33 +++++++++++++++++++++++++++++++-- package.json | 7 +++++-- 6 files changed, 75 insertions(+), 24 deletions(-) create mode 100644 config_template.yml diff --git a/.gitignore b/.gitignore index 30bc162..7b0848b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/node_modules \ No newline at end of file +/node_modules +config.yml \ No newline at end of file diff --git a/README.md b/README.md index 8b6bbaf..481cd6e 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,11 @@ Run this commands on project root directory to install Node dependencies. ``` $ npm install -``` \ No newline at end of file +``` + +Copy *config_template.yml*, put the name *config.yml* and configure it. + +## Built With + +* [Git](https://git-scm.com/) +* [Nodejs](https://nodejs.org/) \ No newline at end of file diff --git a/config_template.yml b/config_template.yml new file mode 100644 index 0000000..3ed2aac --- /dev/null +++ b/config_template.yml @@ -0,0 +1,9 @@ +axios: + method: GET + url: https://website.es/api/login + headers: + Authorization: + params: + data: +interval: 500 +values: \ No newline at end of file diff --git a/main.js b/main.js index 2911593..1693f08 100644 --- a/main.js +++ b/main.js @@ -1,22 +1,24 @@ -const axios = require('axios'); +import axios from 'axios'; +import yml from 'js-yaml'; +import fs from 'fs'; +import chalk from 'chalk'; + async function main() { try { - const model = 'myModel'; - const token = 'myToken'; - const reqInterval = 500; // ms - const values = [ - // Put here your values - ]; - - for (let value of values) { - let url = `https://salix.verdnatura.es/api/${model}/${value}/download?access_token=${token}`; - await axios.get(url); - console.log(`Processed: ${value}`); - await new Promise(resolve => setTimeout(resolve, reqInterval)); + const conf = yml.load(fs.readFileSync('./config.yml', 'utf8')); + for (let value of conf.values) { + await axios({ + method: conf.axios.method, + url: conf.axios.url, + headers: conf.axios.headers, + data: conf.axios.data, + }); + console.log(chalk.green('[Processed]', value)); + await new Promise(resolve => setTimeout(resolve, conf.interval)); } - console.log(`It has been processed successfully!`); + console.log(chalk.green.bold('It has been processed successfully!')); } catch (err) { - console.error('Error: ', err.response.data); - } -} -main(); \ No newline at end of file + console.error(chalk.red('[Error]', err.message)); + }; +}; +main(); diff --git a/package-lock.json b/package-lock.json index f5617c2..7ed524b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,20 @@ { - "name": "masive-request", + "name": "massive-request", "lockfileVersion": 3, "requires": true, "packages": { "": { "dependencies": { - "axios": "^1.4.0" + "axios": "^1.4.0", + "chalk": "^5.2.0", + "js-yaml": "^4.1.0" } }, + "node_modules/argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -23,6 +30,17 @@ "proxy-from-env": "^1.1.0" } }, + "node_modules/chalk": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.2.0.tgz", + "integrity": "sha512-ree3Gqw/nazQAPuJJEy+avdl7QfZMcUvmHIKgEZkGL+xOBzRvup5Hxo6LHuMceSxOabuJLJm5Yp/92R9eMmMvA==", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/combined-stream": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", @@ -74,6 +92,17 @@ "node": ">= 6" } }, + "node_modules/js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "dependencies": { + "argparse": "^2.0.1" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", diff --git a/package.json b/package.json index cb17ec7..13b757d 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,8 @@ { "dependencies": { - "axios": "^1.4.0" - } + "axios": "^1.4.0", + "chalk": "^5.2.0", + "js-yaml": "^4.1.0" + }, + "type": "module" }