-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (38 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const express = require('express');
const fetch = require('fetch');
const rp = require('request-promise');
const $ = require('cheerio');
// Journaux
const regexParisien = /leparisien/;
const regexMonde = /lemonde/;
//https://www.lemonde.fr/politique/article/2020/01/16/les-parlementaires-socialistes-presentent-leur-contre-reforme-des-retraites_6026083_823448.html
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Starting server at ${port}`);
});
app.use(express.static('public'));
app.use(express.json({
limit: '1mb'
}));
app.get('/test', (request, response) => {
const url = request.query.url;
rp(url)
.then(function (html) {
//success!
if (regexParisien.test(url)) {
response.send(String($('.article_header', html)) + String($('.byline', html)) + String($('.article-section p', html))); // HTML pointé sous forme de String.
} else if (regexMonde.test(url)) {
response.send(String($('.article__heading', html)) + String($('.meta__publisher', html)) + String($('article', html)));
} else {
console.log("else");
response.send("Pas d'article trouvé");
}
})
.catch(function (err) {
console.error("Erreur : " + err);
response.end();
return;
});
});