From 97a4aafb1528011e04acd56cf894162acddda7d3 Mon Sep 17 00:00:00 2001 From: Francisco Solis <30329003+Im-Fran@users.noreply.github.com> Date: Fri, 17 May 2024 14:03:02 -0400 Subject: [PATCH 1/2] rem: se ha removido noticias * Se remueven las noticias de la API debido a que la utem bloquea solicitudes fuera de Chile al sitio de noticias o de la utem. --- src/core/controllers/noticia.controller.ts | 38 ------------------ src/core/routes/index.routes.ts | 2 - src/core/routes/noticias.routes.ts | 11 ------ src/utem/models/noticia.model.ts | 8 ---- src/utem/services/utemNoticiasService.ts | 46 ---------------------- 5 files changed, 105 deletions(-) delete mode 100644 src/core/controllers/noticia.controller.ts delete mode 100644 src/core/routes/noticias.routes.ts delete mode 100644 src/utem/models/noticia.model.ts delete mode 100644 src/utem/services/utemNoticiasService.ts diff --git a/src/core/controllers/noticia.controller.ts b/src/core/controllers/noticia.controller.ts deleted file mode 100644 index dc89703..0000000 --- a/src/core/controllers/noticia.controller.ts +++ /dev/null @@ -1,38 +0,0 @@ -import {NextFunction, Response, Request} from "express"; -import {UtemNoticiasService} from "../../utem/services/utemNoticiasService"; -import {dayjs} from "../../app"; -import GenericError from "../../infrastructure/models/error.model"; - -export class NoticiaController { - public static async getNoticias(req: Request, res: Response, next: NextFunction): Promise { - try { - let query = req.query; - query.desde = query.desde || dayjs().subtract(6, 'month').format('YYYY-MM-DD') // Por defecto desde hace 6 meses - query.hasta = query.hasta || dayjs().format('YYYY-MM-DD') // Por defecto hasta hoy - // Valida que el formato de fecha sea 'YYYY-MM-DD' - const desdeDayjs = dayjs(query.desde, 'YYYY-MM-DD').tz('America/Santiago').startOf('day') - if (query.desde && !desdeDayjs.isValid()) { - const error = GenericError.FORMATO_FECHA_INVALIDO - error.internalCode = 18.1 - return next(error) - } - - const hastaDayjs = dayjs(query.hasta, 'YYYY-MM-DD').tz('America/Santiago').endOf('day') - if (query.hasta && !hastaDayjs.isValid()) { - const error = GenericError.FORMATO_FECHA_INVALIDO - error.internalCode = 18.2 - return next(error) - } - - const noticias = await UtemNoticiasService.getNoticias( - query.por_pagina || 10, - desdeDayjs.toISOString(), - hastaDayjs.toISOString() - ); - - res.status(200).json(noticias); - } catch (error) { - next(error); - } - } -} diff --git a/src/core/routes/index.routes.ts b/src/core/routes/index.routes.ts index 7702148..15827a5 100644 --- a/src/core/routes/index.routes.ts +++ b/src/core/routes/index.routes.ts @@ -5,7 +5,6 @@ import CarreraRouter from "./carrera.routes"; import HorarioRouter from "./horario.routes"; import PermisoRouter from "./permiso.routes"; import UsuarioRouter from "./usuario.routes"; -import NoticiasRouter from "./noticias.routes"; import BecaAlimentacionRoutes from "./beca-alimentacion.routes"; const router: Router = Router(); @@ -17,6 +16,5 @@ router.use("/", CarreraRouter); router.use("/", AsignaturaRouter); router.use("/", PermisoRouter); router.use("/", BecaAlimentacionRoutes) -router.use("/", NoticiasRouter); export default router; diff --git a/src/core/routes/noticias.routes.ts b/src/core/routes/noticias.routes.ts deleted file mode 100644 index ecbbfb4..0000000 --- a/src/core/routes/noticias.routes.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Router } from "express"; -import {NoticiaController} from "../controllers/noticia.controller"; - -const router: Router = Router(); - -router.get( - "/noticias", - NoticiaController.getNoticias -); - -export default router; diff --git a/src/utem/models/noticia.model.ts b/src/utem/models/noticia.model.ts deleted file mode 100644 index badd780..0000000 --- a/src/utem/models/noticia.model.ts +++ /dev/null @@ -1,8 +0,0 @@ -export default interface Noticia { - id: number; - fecha: string; - titulo: string; - subtitulo: string; - imagen: string; - link: string; -} diff --git a/src/utem/services/utemNoticiasService.ts b/src/utem/services/utemNoticiasService.ts deleted file mode 100644 index 77bbad5..0000000 --- a/src/utem/services/utemNoticiasService.ts +++ /dev/null @@ -1,46 +0,0 @@ -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 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) { - const $ = cheerio.load(post.excerpt.rendered) - let imagen: string; - try { - imagen = post._embedded['wp:featuredmedia'][0].source_url - } catch (_) { - imagen = "https://noticias.utem.cl/wp-content/uploads/2017/07/en-preparacion.jpg" - } - - const noticia: Noticia = { - id: post.id, - fecha: `${post.date_gmt}Z`, - titulo: post.title.rendered, - subtitulo: $.text().trim(), - link: post.link, - imagen, - } - noticias.push(noticia) - } - - cache.set(cacheKey, noticias, 900) // 15 minutos - return noticias; - } -} From f7e52122f3ba82fd11faff0072fc5759d8ab0452 Mon Sep 17 00:00:00 2001 From: Francisco Solis <30329003+Im-Fran@users.noreply.github.com> Date: Fri, 17 May 2024 14:03:15 -0400 Subject: [PATCH 2/2] rem: variable de entorno de noticias --- .env.example | 1 - 1 file changed, 1 deletion(-) diff --git a/.env.example b/.env.example index c3b0580..6134b6f 100644 --- a/.env.example +++ b/.env.example @@ -2,7 +2,6 @@ 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"