From a899763dd7f51ab30bb41d47899078211218a9cc Mon Sep 17 00:00:00 2001 From: Francisco Solis <30329003+Im-Fran@users.noreply.github.com> Date: Fri, 10 May 2024 13:39:23 -0400 Subject: [PATCH 1/2] =?UTF-8?q?patch:=20se=20repara=20endpoint=20de=20noti?= =?UTF-8?q?cias=20*=20Agregado=20cach=C3=A9=20al=20endpoint=20de=20noticia?= =?UTF-8?q?s=20*=20Se=20elimina=20un=20paquete=20no=20utilizado=20*=20Se?= =?UTF-8?q?=20agrega=20user=20agent=20a=20la=20solicitud=20de=20noticias?= =?UTF-8?q?=20*=20Se=20utiliza=20endpoint=20de=20noticias=20en=20lugar=20d?= =?UTF-8?q?e=20la=20p=C3=A1gina=20inicial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + .pnp.cjs | 12 ------------ package.json | 1 - src/utem/services/utemNoticiasService.ts | 18 ++++++++++++++++-- yarn.lock | 10 ---------- 5 files changed, 17 insertions(+), 25 deletions(-) diff --git a/.env.example b/.env.example index 6134b6f..c3b0580 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,7 @@ PORT=3000 LOG_LEVEL=info UTEM_URL="https://www.utem.cl" +NOTICIAS_UTEM_URL="https://noticias.utem.cl" SSO_UTEM_URL="https://sso.utem.cl" MI_UTEM_URL="https://mi.utem.cl" PASAPORTE_UTEM_URL="https://pasaporte.utem.cl" diff --git a/.pnp.cjs b/.pnp.cjs index 02675f8..ee1a9d6 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -64,7 +64,6 @@ const RAW_RUNTIME_STATE = ["ts-jest", "virtual:1849da143e39ef6d8de11437a0258f87ba57808890f99eb219773c9d4759e61f63665a1affcaf3c86086dd6b7868cb8a3540ff3e9b847b3d36cf70048e9bb909#npm:29.1.2"],\ ["ts-node", "virtual:1849da143e39ef6d8de11437a0258f87ba57808890f99eb219773c9d4759e61f63665a1affcaf3c86086dd6b7868cb8a3540ff3e9b847b3d36cf70048e9bb909#npm:10.9.2"],\ ["typescript", "patch:typescript@npm%3A5.3.3#optional!builtin::version=5.3.3&hash=e012d7"],\ - ["user-agents", "npm:1.1.127"],\ ["uuid", "npm:9.0.1"],\ ["winston", "npm:3.11.0"]\ ],\ @@ -10315,7 +10314,6 @@ const RAW_RUNTIME_STATE = ["ts-jest", "virtual:1849da143e39ef6d8de11437a0258f87ba57808890f99eb219773c9d4759e61f63665a1affcaf3c86086dd6b7868cb8a3540ff3e9b847b3d36cf70048e9bb909#npm:29.1.2"],\ ["ts-node", "virtual:1849da143e39ef6d8de11437a0258f87ba57808890f99eb219773c9d4759e61f63665a1affcaf3c86086dd6b7868cb8a3540ff3e9b847b3d36cf70048e9bb909#npm:10.9.2"],\ ["typescript", "patch:typescript@npm%3A5.3.3#optional!builtin::version=5.3.3&hash=e012d7"],\ - ["user-agents", "npm:1.1.127"],\ ["uuid", "npm:9.0.1"],\ ["winston", "npm:3.11.0"]\ ],\ @@ -13765,16 +13763,6 @@ const RAW_RUNTIME_STATE = "linkType": "HARD"\ }]\ ]],\ - ["user-agents", [\ - ["npm:1.1.127", {\ - "packageLocation": "./.yarn/cache/user-agents-npm-1.1.127-3a065810bd-630eed3c5d.zip/node_modules/user-agents/",\ - "packageDependencies": [\ - ["user-agents", "npm:1.1.127"],\ - ["lodash.clonedeep", "npm:4.5.0"]\ - ],\ - "linkType": "HARD"\ - }]\ - ]],\ ["util", [\ ["npm:0.10.3", {\ "packageLocation": "./.yarn/cache/util-npm-0.10.3-f43de5ccbb-648120d93d.zip/node_modules/util/",\ diff --git a/package.json b/package.json index b98e319..c98e28d 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,6 @@ "readline": "^1.3.0", "ts-node": "^10.9.2", "typescript": "^5.3.3", - "user-agents": "^1.1.127", "uuid": "^9.0.1", "winston": "^3.11.0" }, diff --git a/src/utem/services/utemNoticiasService.ts b/src/utem/services/utemNoticiasService.ts index d7d74f7..77bbad5 100644 --- a/src/utem/services/utemNoticiasService.ts +++ b/src/utem/services/utemNoticiasService.ts @@ -1,10 +1,23 @@ import axios from "axios"; import Noticia from "../models/noticia.model"; import * as cheerio from 'cheerio' +import {HashUtils} from "../../infrastructure/utils/hash.utils"; +import {cache} from "../../app"; +import randomUseragent from 'random-useragent'; export class UtemNoticiasService { static async getNoticias(porPagina: number, desde: string, hasta: string){ - const posts = await axios.get(`${process.env.UTEM_URL}/wp-json/wp/v2/posts?_embed&per_page=${porPagina}&before=${hasta}&after=${desde}`) + const cacheKey = HashUtils.sha512(`noticias:${porPagina}:${desde}:${hasta}`) + const cached = cache.get(cacheKey) + if(cached != undefined) { + return cached; + } + + const posts = await axios.get(`${process.env.NOTICIAS_UTEM_URL}/wp-json/wp/v2/posts?_embed&per_page=${porPagina}&before=${hasta}&after=${desde}`, { + headers: { + 'User-Agent': randomUseragent.getRandom(), + } + }) const noticias: Noticia[] = [] for (const post of posts.data) { @@ -27,6 +40,7 @@ export class UtemNoticiasService { noticias.push(noticia) } - return noticias + cache.set(cacheKey, noticias, 900) // 15 minutos + return noticias; } } diff --git a/yarn.lock b/yarn.lock index 972b3c4..c1dff4a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7887,7 +7887,6 @@ __metadata: ts-jest: "npm:^29.1.2" ts-node: "npm:^10.9.2" typescript: "npm:^5.3.3" - user-agents: "npm:^1.1.127" uuid: "npm:^9.0.1" winston: "npm:^3.11.0" languageName: unknown @@ -10957,15 +10956,6 @@ __metadata: languageName: node linkType: hard -"user-agents@npm:^1.1.127": - version: 1.1.127 - resolution: "user-agents@npm:1.1.127" - dependencies: - lodash.clonedeep: "npm:^4.5.0" - checksum: 10/630eed3c5df62b4b5a21e323548f0070e72cd5dc97cd94a9131b46e5e0957f059e879a6654fcd409d600fb9c83a80cd808a09b5d4f9b99c28a9a5db4f08fc0d2 - languageName: node - linkType: hard - "util-deprecate@npm:^1.0.1, util-deprecate@npm:^1.0.2, util-deprecate@npm:~1.0.1": version: 1.0.2 resolution: "util-deprecate@npm:1.0.2" From 3b484fff3912ec72c86ff9acb953751b1c9d667b Mon Sep 17 00:00:00 2001 From: Francisco Solis <30329003+Im-Fran@users.noreply.github.com> Date: Fri, 10 May 2024 13:44:06 -0400 Subject: [PATCH 2/2] patch: se agrega variable de entorno NOTICIAS_UTEM_URL --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index df0f563..82fd42b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -33,6 +33,7 @@ jobs: yarn yarn test env: + NOTICIAS_UTEM_URL: ${{ vars.NOTICIAS_UTEM_URL || 'https://noticias.utem.cl' }} UTEM_URL: ${{ vars.UTEM_URL || 'https://www.utem.cl' }} SSO_UTEM_URL: ${{ vars.SSO_UTEM_URL || 'https://sso.utem.cl' }} MI_UTEM_URL: ${{ vars.MI_UTEM_URL || 'https://mi.utem.cl' }}