-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
594 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.