Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(noticias-endpoint): agregado un endpoint de noticias #15

Merged
merged 10 commits into from
Dec 5, 2023
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PORT=3000
LOG_LEVEL=info

UTEM_URL="https://www.utem.cl"
SSO_UTEM_URL="https://sso.utem.cl"
MI_UTEM_URL="https://mi.utem.cl"
PASAPORTE_UTEM_URL="https://pasaporte.utem.cl"
Expand Down
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,21 @@ FCM_SERVER_KEY=AAAAPEuk7fI:APA91bGG9UrjuLX8kt1DWVwz...
SISEI_KEY=123456
```

| **Variable** | **Descripción** |
|----------------------|---------------------------------------------------------------------------------------------|
| `PORT` | Puerto donde correrá el servicio, se utiliza ´443´ para habilitar las consultas ´HTTPS´ |
| `SENTRY_URL` | URL de Sentry para el manejo de errores |
| `PRIVATE_KEY_PATH` | Ubicación del archivo correspondiente a la llave privada del certificado HTTPS |
| `CERTIFICATE_PATH` | Ubicación del archivo correspondiente al certificado HTTPS |
| `MI_UTEM_URL` | URL de la página web Mi.UTEM |
| `ACADEMIA_UTEM_URL` | URL de la página web de Academia.UTEM |
| `ACADEMIA_CLIENT_SECRET` | Token para generar URLs de login del SSO para Academia.UTEM |
| `PASAPORTE_UTEM_URL` | URL de la página web de Pasaporte.UTEM |
| `SSO_UTEM_URL` | URL de la página web de SSO.UTEM |
| `REQ_REF` | Opcional. Valor del parámetro `ref` que se agregará a las consultas a las distintas webs. |
| `GOOGLE_APPLICATION_CREDENTIALS` | Ubicación del archivo de credenciales del proyecto Firebase. |
| `FCM_SERVER_KEY` | Llave del servidor de FCM para enviar notificaciones. |
| **Variable** | **Descripción** |
|----------------------|------------------------------------------------------------------------------------------|
| `PORT` | Puerto donde correrá el servicio, se utiliza ´443´ para habilitar las consultas ´HTTPS´ |
| `SENTRY_URL` | URL de Sentry para el manejo de errores |
| `PRIVATE_KEY_PATH` | Ubicación del archivo correspondiente a la llave privada del certificado HTTPS |
| `CERTIFICATE_PATH` | Ubicación del archivo correspondiente al certificado HTTPS |
| `UTEM_URL` | URL de la página web UTEM |
| `MI_UTEM_URL` | URL de la página web Mi.UTEM |
| `ACADEMIA_UTEM_URL` | URL de la página web de Academia.UTEM |
| `ACADEMIA_CLIENT_SECRET` | Token para generar URLs de login del SSO para Academia.UTEM |
| `PASAPORTE_UTEM_URL` | URL de la página web de Pasaporte.UTEM |
| `SSO_UTEM_URL` | URL de la página web de SSO.UTEM |
| `REQ_REF` | Opcional. Valor del parámetro `ref` que se agregará a las consultas a las distintas webs. |
| `GOOGLE_APPLICATION_CREDENTIALS` | Ubicación del archivo de credenciales del proyecto Firebase. |
| `FCM_SERVER_KEY` | Llave del servidor de FCM para enviar notificaciones. |
| `SISEI_KEY` | Llave para que el equipo de SISEI pueda hacer algunas consultas. Puede ser cualquier valor. |

## Ejecución
Expand Down
14 changes: 14 additions & 0 deletions src/core/controllers/noticia.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {NextFunction, Response, Request} from "express";
import {NoticiasService} from "../../utem/services/noticias.service";

export class NoticiaController {
public static async getNoticias(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const noticias = await NoticiasService.getNoticias(req.query.por_pagina || 10);

res.status(200).json(noticias);
} catch (error) {
next(error);
}
}
}
2 changes: 2 additions & 0 deletions src/core/routes/index.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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";

const router: Router = Router();

Expand All @@ -14,5 +15,6 @@ router.use("/", HorarioRouter);
router.use("/", CarreraRouter);
router.use("/", AsignaturaRouter);
router.use("/", PermisoRouter);
router.use("/", NoticiasRouter);

export default router;
11 changes: 11 additions & 0 deletions src/core/routes/noticias.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Router } from "express";
import {NoticiaController} from "../controllers/noticia.controller";

const router: Router = Router();

router.get(
"/noticias",
NoticiaController.getNoticias
);

export default router;
8 changes: 8 additions & 0 deletions src/utem/models/noticia.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default interface Noticia {
id: number;
fecha: string;
titulo: string;
subtitulo: string;
imagen: string;
link: string;
}
35 changes: 35 additions & 0 deletions src/utem/services/noticias.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import axios from "axios";
import Noticia from "../models/noticia.model";
import * as cheerio from 'cheerio'

export class NoticiasService {
Im-Fran marked this conversation as resolved.
Show resolved Hide resolved
static async getNoticias(porPagina: number){
if(porPagina > 20) {
porPagina = 20;
Im-Fran marked this conversation as resolved.
Show resolved Hide resolved
}
const posts = await axios.get(`${process.env.UTEM_URL}/wp-json/wp/v2/posts?_embed&per_page=${porPagina}`);
const noticias: Noticia[] = []

for (const post of posts.data) {
const $ = cheerio.load(post.excerpt.rendered)
let imagen;
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,
Im-Fran marked this conversation as resolved.
Show resolved Hide resolved
titulo: post.title.rendered,
subtitulo: $.text().trim(),
link: post.link,
imagen,
}
noticias.push(noticia)
}

return noticias
}
}