Skip to content

Commit

Permalink
PATCH
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobrunia committed Jan 29, 2024
1 parent c28275e commit dd8074a
Show file tree
Hide file tree
Showing 5 changed files with 594 additions and 1 deletion.
43 changes: 43 additions & 0 deletions controlers/news-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import axios, {isCancel, AxiosError} from 'axios';
import * as cheerio from 'cheerio';

class NewsController {
//memory_numbers
async returnCnnNews(request, response, next) {
try {
function generateUrl() {
const currentDate = new Date();
const day = String(currentDate.getDate()).padStart(2, '0');
const month = String(currentDate.getMonth() + 1).padStart(2, '0');
const year = String(currentDate.getFullYear()).slice(-2);
return `https://edition.cnn.com/middleeast/live-news/israel-hamas-war-gaza-news-${month}-${day}-${year}/index.html`;
}

const newsPostsArray = [];

axios
.get(generateUrl())
.then((res) => {
const html = res.data;
let $ = cheerio.load(html);

$('.sc-bwzfXH.sc-eXEjpC.iGQwpp').each((index, element) => {
// Получение содержимого каждого элемента
const content = $(element).text(); // Получение текстового содержимого элемента

// Помещение элемента и его содержимого в массив
newsPostsArray.push({ element: $(element).html(), content });
});

response.json(newsPostsArray);
})
.catch((error) => {
console.log(error);
});
} catch (error) {
next(error);
}
}
}

export const newsController = new NewsController();
Loading

0 comments on commit dd8074a

Please sign in to comment.