-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspider2.js
More file actions
187 lines (149 loc) · 4.69 KB
/
Copy pathspider2.js
File metadata and controls
187 lines (149 loc) · 4.69 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// does the same as spider.js except that it is not supposed to run on the same
// server as Mensa.cafe and Mensabot.
// It calls the Mensa.cafe API instead and sends back the data it got from
// the Mensa France annuaire.
// TODO: handle when cookie is too old
const needle = require('needle');
const cheerio = require('cheerio');
const axios = require('axios');
const moment = require('moment');
const log = require('./mods/log');
const conf = require('./configs');
let cookies = {};
async function getCookies() {
if (! cookies || ! cookies.lemonldap) {
// we get the token
let resp = await needle("get","https://auth.mensa-france.net/");
let $ = cheerio.load(resp.body);
const token = $('#token').attr('value');
log.debug("got token " + token);
// we get the cookie
resp = await needle("post","https://auth.mensa-france.net/", {
token,
user: conf.web.userid,
password: conf.web.password
});
cookies = resp.cookies;
log.debug("got cookie " + cookies.lemonldap);
}
return;
}
async function getDataFromMensannuaire(mid) {
await getCookies();
// we get member page
resp = await needle("get", conf.web.url + 'id=' + mid, {cookies});
let html = resp.body.slice(resp.body.indexOf('<body'));
$ = cheerio.load(html);
let name = $('#identite span:nth-child(2)').text();
if (! name) {
let message = $('#fiche-perso').text().trim();
cookies = null;
throw `Error getting info for ${mid}: ${message}`;
}
let region = $('#identite').text().split("\n")[2].trim().split('-').slice(-1)[0].trim();
let email = $('#contacts div.email a').text();
let birthdate = $('#identite div span').text().split("(")[0].trim().split(" ")[2].trim();
let [bday, bmonth, byear] = birthdate.split("/");
// to avoid SQL injections
bday = bday.slice(0, 2);
bmonth = bmonth.slice(0, 2);
byear = byear.slice(0, 4);
birthdate = byear +'-'+ bmonth +'-'+ bday;
let pays = $('#identite div span a').html().split("<br>").slice(-1)[0].trim().split(",").slice(-1)[0].trim();
let code_postal = null;
let departement = null;
if (pays.toUpperCase() == 'FRANCE') {
code_postal = $('#identite div span a').html().split("<br>").slice(-1)[0].split(" ")[0].slice(0,5);
if (parseInt(code_postal)) {
departement = code_postal.slice(0,-3);
} else {
code_postal = null;
}
}
let adherent_until = $('#fiche-perso table tbody tr td:nth-child(2)').html();
let [aday, amonth, ayear] = adherent_until.split("/");
// to avoid SQL injections
aday = aday.slice(0, 2);
amonth = amonth.slice(0, 2);
ayear = ayear.slice(0, 4);
adherent_until = ayear +'-'+ amonth +'-'+ aday;
let adherent = 0;
if (adherent_until > moment().format("YYYY-MM-DD")) {
adherent = 1;
}
// we check email well formed
if (! /\S+@\S+\.\S+/.test(email)) {
console.error(`Email ${email} not valid for user ${mid}`);
email = '';
}
// remove some un-necessary data
name = name.replace('-information non publique-', '');
let obj = {name, region, email, adherent, adherent_until, birthdate, code_postal, departement, pays};
console.log(obj);
return obj;
}
/**
* Takes users from state 'welcomed' to state 'found' or 'err_not_found'
*/
findNewMensans = async function(old = false) {
let url = conf.spider.url + '/api/spider';
if (old) {
url += '?outdated=1';
}
console.log("fetching", url);
let {data} = await axios.get(url, {
auth: {
username: 'spider',
password: conf.spider.password
}
});
// console.log(data);
let noobs = data.noobs;
let unknowns = data.unknowns;
log.debug("Found " + noobs.length + " new Mensan(s).");
if (noobs.length) {
console.log(noobs);
}
while (noobs.length) {
const drWho = noobs.pop();
await processMensan(drWho);
}
while (unknowns.length) {
const drWho = unknowns.pop();
await processMensan(drWho);
}
}
async function findOldMensans() {
await findNewMensans(1);
}
async function processMensan(drWho) {
// console.log("== WHO ==\n", drWho);
let found;
try {
found = await getDataFromMensannuaire(drWho.mid);
found.mid = drWho.mid;
// nan mais c'est quoi ce sexisme d'un autre age !!! ;-)
found.name = found.name
.replace('Monsieur ', '')
.replace('Madame ', '')
.replace('Mademoiselle ', '');
} catch (err) {
log.error("Failed visiting page of mensan member " + drWho.mid);
console.error(err);
return;
}
// console.log("== Found ==\n", found);
if ( ! found) {
found = { mid: drWho.mid, status: 404};
}
axios.post(conf.spider.url + '/api/spider', found, {
auth: {
username: 'spider',
password: conf.spider.password
}
});
}
// fillEmptyNames();
setInterval(findNewMensans, 60 * 1000);
setInterval(findOldMensans, 24 * 60 * 60 * 1000);
findOldMensans();