-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch-informations-depute.js
170 lines (154 loc) · 3.83 KB
/
fetch-informations-depute.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* This file fetches all required information from a specific MP (with its ids).
* To execute :
* node fetch-information-depute.js
*
* Authors : Corentin Forler, Pierre Sibut-Bourde, 2021.
*/
/**
* Practical requirements.
*/
const { fetchHtml, parseHtml, zip } = require('./utils');
/**
* @typedef {{
* anneeDebut?: number;
* anneeFin?: number;
* regimePolitique?: string;
* legislature?: string;
* departement?: string;
* groupe?: string;
* }} Mandat
*/
/**
* @typedef {{
* nomComplet?: string
* anneeNaissance?: number
* anneeDeces?: number
* mandats?: Mandat[]
* imageUrl?: string
* }} Depute
*/
/**
* @param {number} id The unique identifier of the député
* @returns {Promise<Depute>}
*/
async function getDeputeAsObject(id) {
const document = await getDocument(id);
const nomComplet = getNomComplet(document);
const [anneeNaissance, anneeDeces] = getAnneesVie(document);
const mandats = getMandats(document);
/** @type {Depute} */
const depute = {
nomComplet,
anneeNaissance,
anneeDeces,
mandats,
imageUrl: getImageUrl(id),
bdUrl: `https://www2.assemblee-nationale.fr/sycomore/fiche/(num_dept)/${id}`,
hasBio: hasBio(document),
};
return depute;
}
/**
* @param {Document} document
* @returns {boolean}
*/
function hasBio(document) {
return document.querySelector('[href="#bio"]') !== null;
}
/**
* @param {Document} document
* @returns {string}
*/
function getNomComplet(document) {
const el = document.querySelector('#haut-contenu-page > article > div.titre-bandeau-bleu > h1');
return el.textContent;
}
/**
* @param {Document} document
* @returns {[number, number] | [number] | []}
*/
function getAnneesVie(document) {
const el = document.querySelector('#haut-contenu-page > article > div.titre-bandeau-bleu > p');
const m = el.textContent.match(/(\d{4})/g);
if (m) {
return m.map(x => +x);
} else {
return [];
}
}
/**
* @param {Document} document
* @returns {Mandats[]}
*/
function getMandats(document) {
const mandats = [];
const elements = document.querySelectorAll('#mandats_an > dl');
for (const el of elements) {
const mandat = extractMandatFromElement(el);
mandats.push(mandat);
}
return mandats.reverse(); // dans l'ordre antichronologique
}
/**
* Extracts a Mandat from an HTMLElement
* @param {HTMLElement} el
* @returns {Mandat}
*/
function extractMandatFromElement(el) {
/** @type {Mandat} */
const mandat = {
anneeDebut: '',
anneeFin: '',
regimePolitique: '',
legislature: '',
departement: '',
groupe: '',
};
const terms = [...el.querySelectorAll('dt')];
const defs = [...el.querySelectorAll('dd')];
const keyValuePairs = zip(terms, defs);
for (const [termEl, defEl] of keyValuePairs) {
const text = defEl.textContent.trim();
const term = termEl.textContent.trim();
switch (term) {
case 'Régime politique':
mandat.regimePolitique = text;
break;
case 'Législature':
mandat.legislature = text;
break;
case 'Mandat': {
const m = text.match(/(\d{4})/g);
if (m) {
[mandat.anneeDebut, mandat.anneeFin] = m;
}
break;
}
case 'Département':
mandat.departement = text;
break;
case 'Groupe':
mandat.groupe = text;
break;
}
}
return mandat;
}
/**
* Function that gets the document body given an id.
* @param {number} id
* @returns {Promise<Document>}
*/
function getDocument(id) {
const url = `https://www2.assemblee-nationale.fr/sycomore/fiche/(num_dept)/${id}`;
return fetchHtml(url).then(parseHtml);
}
/**
* Functions that directly guess the URL of the main image.
* @param {Document} document
*/
function getImageUrl(id) {
return `https://www2.assemblee-nationale.fr/static/sycomore/jpg/${id}.jpg`;
}
module.exports = { getDeputeAsObject };