-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraping.js
More file actions
183 lines (157 loc) · 5.93 KB
/
Copy pathscraping.js
File metadata and controls
183 lines (157 loc) · 5.93 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
import dotenv from 'dotenv';
dotenv.config();
import puppeteer from 'puppeteer';
import mongoose from 'mongoose';
import * as cheerio from 'cheerio';
import Match from '../models/Match.js';
import Standing from '../models/Standing.js';
// 🔹 Club fixe
const CLUB_TEAM_NAME = "AS SAINT-BARTHELEMY D'ANJOU V.B.";
// 🔹 Utilitaires
function parseDateFR(dateStr, timeStr) {
const [day, month, year] = dateStr.split('/').map(Number);
const fullYear = year < 100 ? 2000 + year : year;
const [hours, minutes] = timeStr.split(':').map(Number);
return new Date(fullYear, month - 1, day, hours, minutes).toISOString();
}
function parseSetsDetail(setsStr) {
if (!setsStr) return [];
return setsStr.split(',').map((s, idx) => {
const [scoreFor, scoreAgainst] = s.trim().split(':').map(Number);
return { setNumber: idx + 1, scoreFor: scoreFor || 0, scoreAgainst: scoreAgainst || 0 };
});
}
// 🔹 Scraping d’un championnat
async function scrapeChampionship(champ) {
try {
console.log(`\n🏐 ${champ.name || 'Championnat inconnu'}`);
console.log(`🔗 ${champ.federationUrl}`);
if (!champ.federationUrl) {
console.warn(`⚠️ Championnat ${champ.name || champ._id} n’a pas d’URL, skipping...`);
return;
}
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(champ.federationUrl, { waitUntil: 'networkidle2' });
const html = await page.content();
await browser.close();
const $ = cheerio.load(html);
// -----------------------------
// 🔹 Scraping des matchs
// -----------------------------
console.log('📊 Parsing des matchs...');
const matches = [];
const now = new Date();
$('tr').each((i, tr) => {
const tds = $(tr).find('td');
if (tds.length < 6) return;
const row = [];
tds.each((j, td) => row.push($(td).text().trim()));
const homeTeam = row[3];
const awayTeam = row[5];
if (!homeTeam || !awayTeam) return;
if (homeTeam === CLUB_TEAM_NAME || awayTeam === CLUB_TEAM_NAME) {
const homeAway = homeTeam === CLUB_TEAM_NAME ? 'home' : 'away';
const scoreFor = homeAway === 'home' ? parseInt(row[6]) || 0 : parseInt(row[7]) || 0;
const scoreAgainst = homeAway === 'home' ? parseInt(row[7]) || 0 : parseInt(row[6]) || 0;
const dateISO = parseDateFR(row[1], row[2]);
const setsDetail = parseSetsDetail(row[8]);
const matchDate = new Date(dateISO);
let status = 'scheduled';
if (matchDate <= now && (scoreFor > 0 || scoreAgainst > 0)) {
status = 'played';
}
matches.push({
championshipId: champ._id,
opponentName: homeAway === 'home' ? awayTeam : homeTeam,
date: dateISO,
homeAway,
status,
scoreFor,
scoreAgainst,
setsDetail,
});
}
});
console.log(`🧪 ${matches.length} matchs détectés`);
for (const m of matches) {
const existing = await Match.findOne({
championshipId: champ._id,
opponentName: m.opponentName,
date: m.date,
});
if (existing) {
const needsUpdate =
existing.status !== m.status ||
existing.scoreFor !== m.scoreFor ||
existing.scoreAgainst !== m.scoreAgainst ||
JSON.stringify(existing.setsDetail) !== JSON.stringify(m.setsDetail);
if (needsUpdate) {
await Match.updateOne({ _id: existing._id }, m);
console.log(`🔄 Match mis à jour : ${m.opponentName} (${m.date})`);
}
} else {
await Match.create(m);
console.log(`➕ Match créé : ${m.opponentName} (${m.date})`);
}
}
// -----------------------------
// 🔹 Scraping des standings
// -----------------------------
console.log('📊 Parsing des standings...');
const standings = [];
$('table tbody tr').each((i, tr) => {
const tds = $(tr).find('td');
if (tds.length < 16) return;
const rank = parseInt($(tds[0]).text()) || 0;
const teamName = $(tds[1]).text().trim();
if (!teamName) return;
const points = parseInt($(tds[2]).text()) || 0;
const played = parseInt($(tds[3]).text()) || 0;
const wins = parseInt($(tds[4]).text()) || 0;
const losses = parseInt($(tds[5]).text()) || 0;
const setsFor = parseInt($(tds[13]).text()) || 0;
const setsAgainst = parseInt($(tds[14]).text()) || 0;
const coefficientSets = parseFloat($(tds[15]).text()) || 0;
const pointsFor = parseInt($(tds[16]).text()) || 0;
const pointsAgainst = parseInt($(tds[17]).text()) || 0;
const coefficientPoints = parseFloat($(tds[18]).text()) || 0;
standings.push({
championshipId: champ._id,
teamName,
rank,
played,
wins,
losses,
setsFor,
setsAgainst,
coefficientSets,
pointsFor,
pointsAgainst,
coefficientPoints,
points,
});
});
console.log('💾 Mise à jour des standings dans MongoDB...');
for (const s of standings) {
await Standing.findOneAndUpdate({ championshipId: champ._id, teamName: s.teamName }, s, { upsert: true });
}
console.log(`🎉 ${standings.length} équipes mises à jour ou créées`);
} catch (err) {
console.error('❌ Erreur pendant le scraping :', err);
}
}
// -----------------------------
// 🔹 Connexion MongoDB et scraping de tous les championnats
// -----------------------------
mongoose
.connect(process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/saintbarthvolley')
.then(async () => {
console.log('✅ MongoDB connecté');
const championships = await mongoose.connection.db.collection('championships').find().toArray();
console.log(`🚀 ${championships.length} championnats à scraper`);
for (const champ of championships) {
await scrapeChampionship(champ);
}
})
.finally(() => mongoose.disconnect());