limit results, improve age display
This commit is contained in:
parent
f8e9443ef8
commit
41a4ea5837
@ -45,6 +45,11 @@ async function getMovieClassification(movieUrl) {
|
||||
const label = $(el).find('span.vh').text().trim();
|
||||
if (label) marks.push(label);
|
||||
});
|
||||
const normalizedMarks = marks.map(m => {
|
||||
// Extract first number found, else null
|
||||
const n = parseInt((m + '').replace(/\D/g, ''), 10);
|
||||
return isNaN(n) ? null : n;
|
||||
}).filter(x => x !== null);
|
||||
const details = [];
|
||||
$('.c-classificatie__item').each((_, el) => {
|
||||
const type = $(el).find('svg use').first().attr('xlink:href') || '';
|
||||
@ -61,6 +66,7 @@ async function getMovieClassification(movieUrl) {
|
||||
genres,
|
||||
img,
|
||||
marks,
|
||||
normalizedMarks,
|
||||
details,
|
||||
summary
|
||||
};
|
||||
|
30
merge.js
30
merge.js
@ -1,21 +1,28 @@
|
||||
// Utilitaire pour merger les résultats de plusieurs agrégateurs
|
||||
function normalizeTitle(str) {
|
||||
return str ? str.toLowerCase().replace(/[^a-z0-9]/g, '') : '';
|
||||
}
|
||||
|
||||
function normalizeTitle(str) {
|
||||
return str ? str.toLowerCase().replace(/[^a-z0-9]/g, '') : '';
|
||||
// Compute match score: exact > startsWith > includes > other
|
||||
function getMatchScore(film, query) {
|
||||
const nTitle = normalizeTitle(film.title);
|
||||
const nQuery = normalizeTitle(query);
|
||||
if (nTitle === nQuery) return 100;
|
||||
if (nTitle.startsWith(nQuery)) return 80;
|
||||
if (nTitle.includes(nQuery)) return 60;
|
||||
return 10;
|
||||
}
|
||||
function mergeResults(arrays) {
|
||||
|
||||
// Merge and rank by match
|
||||
function mergeResults(arrays, query = '', limit = 5) {
|
||||
const map = {};
|
||||
arrays.flat().forEach(entry => {
|
||||
// Note: only title, fallback if no year
|
||||
const key = normalizeTitle(entry.title) + (entry.year ? '|' + entry.year : '');
|
||||
if (!map[key]) {
|
||||
map[key] = {
|
||||
title: entry.title,
|
||||
year: entry.year,
|
||||
results: []
|
||||
results: [],
|
||||
__raw: entry // For tie-break
|
||||
};
|
||||
}
|
||||
map[key].results.push({
|
||||
@ -23,7 +30,16 @@ function mergeResults(arrays) {
|
||||
...entry
|
||||
});
|
||||
});
|
||||
return Object.values(map);
|
||||
let out = Object.values(map);
|
||||
if (query) {
|
||||
out.forEach(f => f.__score = getMatchScore(f, query));
|
||||
out = out.sort((a, b) => b.__score - a.__score);
|
||||
}
|
||||
// Remove internals, trim to limit
|
||||
return out.slice(0, limit).map(f => {
|
||||
delete f.__score; delete f.__raw;
|
||||
return f;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { mergeResults };
|
||||
|
@ -41,107 +41,148 @@
|
||||
<div id="films"></div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function search() {
|
||||
const query = document.getElementById('q').value.trim();
|
||||
if (!query) return;
|
||||
const filmsDiv = document.getElementById('films');
|
||||
filmsDiv.innerHTML = '<p class="loader">Recherche en cours...</p>';
|
||||
<script>
|
||||
// Age color logic
|
||||
function getAgeColor(age) {
|
||||
age = parseInt(age, 10);
|
||||
if (isNaN(age)) return '#999';
|
||||
if (age <= 6) return '#19c94a';
|
||||
if (age <= 9) return '#68c3ee';
|
||||
if (age <= 12) return '#ffe45e';
|
||||
if (age <= 16) return '#ffb154';
|
||||
if (age <= 18) return '#ff6384';
|
||||
return '#e2525c';
|
||||
}
|
||||
// Show a big colored badge for age, else fallback
|
||||
function ageBadge(age) {
|
||||
if (!age) return '';
|
||||
const c = getAgeColor(age);
|
||||
return `<span class="age-badge" style="
|
||||
display:inline-block;
|
||||
font-size:1.2em;
|
||||
min-width:2.5em;
|
||||
font-weight:bold;
|
||||
color:#222;
|
||||
background:${c};
|
||||
border-radius:0.4em;
|
||||
text-align:center;
|
||||
margin-right:0.7em;
|
||||
padding:0.15em 0.7em;
|
||||
box-shadow:0 2px 8px #0003;
|
||||
letter-spacing:0.03em;
|
||||
">${age}+</span>`;
|
||||
}
|
||||
|
||||
try {
|
||||
const base = window.location.origin;
|
||||
const response = await fetch(`${base}/search?q=${encodeURIComponent(query)}`);
|
||||
if (!response.ok) {
|
||||
// Gilfoyle: "The network, or your server, is garbage."
|
||||
filmsDiv.innerHTML = `<p class="no-results">Erreur: ${response.status} ${response.statusText || 'Impossible de joindre le backend.'}</p>`;
|
||||
return;
|
||||
}
|
||||
const films = await response.json();
|
||||
// Truncate summary at N chars, add ellipsis
|
||||
function shortSummary(str, n = 200) {
|
||||
if (!str) return '-';
|
||||
if (str.length <= n) return str;
|
||||
return str.slice(0, n).replace(/\s+\S*$/, '') + '…';
|
||||
}
|
||||
|
||||
if (!Array.isArray(films) || !films.length) {
|
||||
filmsDiv.innerHTML = '<p class="no-results">Aucun résultat trouvé. Essayez un autre terme.</p>';
|
||||
return;
|
||||
}
|
||||
async function search() {
|
||||
const query = document.getElementById('q').value.trim();
|
||||
if (!query) return;
|
||||
const filmsDiv = document.getElementById('films');
|
||||
filmsDiv.innerHTML = '<p class="loader">Searching...</p>';
|
||||
|
||||
let html = `<table class="results-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Titre</th>
|
||||
<th>Année</th>
|
||||
<th>Sources d'information</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>`;
|
||||
|
||||
films.forEach(film => {
|
||||
html += `<tr>
|
||||
<td>${film.title || 'Titre inconnu'}</td>
|
||||
<td class="year">${film.year || 'N/A'}</td>
|
||||
<td>`;
|
||||
|
||||
film.results.forEach(r => {
|
||||
html += `<div class="source-block">
|
||||
<p class="source-name">${r.source.charAt(0).toUpperCase() + r.source.slice(1)}</p>`;
|
||||
if (r.img) {
|
||||
html += `<img src="${r.img}" alt="Affiche pour ${film.title}">`;
|
||||
}
|
||||
if (r.link) {
|
||||
html += `<p><a href="${r.link}" target="_blank">Voir la fiche détaillée</a></p>`;
|
||||
}
|
||||
|
||||
// CommonSense Media specific
|
||||
if (r.source === 'commonsense') {
|
||||
html += `<p><b>Âge conseillé:</b> ${r.age || '-'}</p>`;
|
||||
html += `<p><b>Résumé (CSM):</b> ${r.summary || r.parentsNeedToKnow || '-'}</p>`;
|
||||
if (r.details && r.details.length) {
|
||||
html += `<p><b>Détails (CSM):</b></p><ul>`;
|
||||
r.details.forEach(d => {
|
||||
html += `<li>${d.type}: ${d.score}/5 - ${d.description || ''}</li>`;
|
||||
});
|
||||
html += `</ul>`;
|
||||
}
|
||||
}
|
||||
// Cinecheck specific
|
||||
else if (r.source === 'cinecheck') {
|
||||
html += `<p><b>Âge(s) (Cinecheck):</b> ${r.marks && r.marks.length ? r.marks.join(', ') : '-'}</p>`;
|
||||
html += `<p><b>Résumé (Cinecheck):</b> ${r.summary || '-'}</p>`;
|
||||
if (r.details && r.details.length) {
|
||||
html += `<p><b>Pictogrammes (Cinecheck):</b> ${r.details.map(d => d.type).join(', ') || '-'}</p>`;
|
||||
}
|
||||
}
|
||||
// Filmages specific
|
||||
else if (r.source === 'filmages') {
|
||||
html += `<p><b>Titre original (Filmages):</b> ${r.details.titleOriginalPage || r.details.titleOriginal || '-'}</p>`;
|
||||
html += `<p><b>Âge légal (Filmages):</b> ${r.details.ageLegal || '-'}</p>`;
|
||||
html += `<p><b>Âge suggéré (Filmages):</b> ${r.details.ageSuggested || '-'}</p>`;
|
||||
html += `<p><b>Résumé (Filmages):</b> ${r.details.summary || '-'}</p>`;
|
||||
html += `<p><b>Synthèse (Filmages):</b> ${r.details.synthesis || '-'}</p>`;
|
||||
if (r.details.indications && r.details.indications.length) {
|
||||
html += `<p><b>Indications:</b> ${r.details.indications.join(', ')}</p>`;
|
||||
}
|
||||
if (r.details.counterIndications && r.details.counterIndications.length) {
|
||||
html += `<p><b>Contre-indications:</b> ${r.details.counterIndications.join(', ')}</p>`;
|
||||
}
|
||||
}
|
||||
// Fallback for any other or new source
|
||||
else {
|
||||
html += `<p><b>Résumé:</b> ${r.summary || '-'}</p>`;
|
||||
html += `<p><b>Âge:</b> ${r.age || '-'}</p>`;
|
||||
}
|
||||
html += `</div>`;
|
||||
});
|
||||
html += `</td></tr>`;
|
||||
});
|
||||
|
||||
html += '</tbody></table>';
|
||||
filmsDiv.innerHTML = html;
|
||||
} catch (error) {
|
||||
// Mike: "Didn't go as planned."
|
||||
console.error('Search function error:', error);
|
||||
filmsDiv.innerHTML = `<p class="no-results">Une erreur s'est produite lors de la recherche. Vérifiez la console.</p>`;
|
||||
try {
|
||||
const base = window.location.origin;
|
||||
const response = await fetch(`${base}/search?q=${encodeURIComponent(query)}`);
|
||||
if (!response.ok) {
|
||||
filmsDiv.innerHTML = `<p class="no-results">Error: ${response.status} ${response.statusText || 'Backend unreachable.'}</p>`;
|
||||
return;
|
||||
}
|
||||
const films = await response.json();
|
||||
|
||||
if (!Array.isArray(films) || !films.length) {
|
||||
filmsDiv.innerHTML = '<p class="no-results">No results. Try another query.</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
let html = `<table class="results-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Title</th>
|
||||
<th>Year</th>
|
||||
<th>Information sources</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>`;
|
||||
|
||||
films.forEach(film => {
|
||||
html += `<tr>
|
||||
<td>${film.title || 'Unknown title'}</td>
|
||||
<td class="year">${film.year || 'N/A'}</td>
|
||||
<td>`;
|
||||
film.results.forEach(r => {
|
||||
html += `<div class="source-block">`;
|
||||
html += `<p class="source-name">${r.source.charAt(0).toUpperCase() + r.source.slice(1)}</p>`;
|
||||
if (r.img) {
|
||||
html += `<img src="${r.img}" alt="Poster for ${film.title}">`;
|
||||
}
|
||||
if (r.link) {
|
||||
html += `<p><a href="${r.link}" target="_blank">View details</a></p>`;
|
||||
}
|
||||
// CommonSense Media
|
||||
if (r.source === 'commonsense') {
|
||||
html += `<p><b>Recommended age:</b> ${ageBadge(r.age)}</p>`;
|
||||
html += `<p><b>Summary (CSM):</b> ${shortSummary(r.summary || r.parentsNeedToKnow)}</p>`;
|
||||
if (r.details && r.details.length) {
|
||||
html += `<p><b>Details (CSM):</b></p><ul>`;
|
||||
r.details.forEach(d => {
|
||||
html += `<li>${d.type}: ${d.score}/5 - ${shortSummary(d.description, 80)}</li>`;
|
||||
});
|
||||
html += `</ul>`;
|
||||
}
|
||||
}
|
||||
// Cinecheck
|
||||
else if (r.source === 'cinecheck') {
|
||||
let numericAges = Array.isArray(r.normalizedMarks) && r.normalizedMarks.length
|
||||
? r.normalizedMarks
|
||||
: (r.marks || []).map(x => {
|
||||
let n = parseInt((x + '').replace(/\d+/, ''), 10);
|
||||
return isNaN(n) ? null : n;
|
||||
}).filter(n => n !== null && !isNaN(n));
|
||||
let minAge = numericAges.length ? Math.min(...numericAges) : '';
|
||||
html += `<p><b>Age(s) (Cinecheck):</b> ${minAge ? ageBadge(minAge) : ''}${r.marks && r.marks.length ? r.marks.join(', ') : '-'}</p>`;
|
||||
html += `<p><b>Summary (Cinecheck):</b> ${shortSummary(r.summary)}</p>`;
|
||||
if (r.details && r.details.length) {
|
||||
html += `<p><b>Pictograms (Cinecheck):</b> ${r.details.map(d => d.type).join(', ') || '-'}</p>`;
|
||||
}
|
||||
}
|
||||
// Filmages
|
||||
else if (r.source === 'filmages') {
|
||||
html += `<p><b>Original title (Filmages):</b> ${r.details.titleOriginalPage || r.details.titleOriginal || '-'}</p>`;
|
||||
html += `<p><b>Legal age (Filmages):</b> ${ageBadge(r.details.ageLegal)}</p>`;
|
||||
html += `<p><b>Suggested age (Filmages):</b> ${ageBadge(r.details.ageSuggested)}</p>`;
|
||||
html += `<p><b>Summary (Filmages):</b> ${shortSummary(r.details.summary)}</p>`;
|
||||
html += `<p><b>Synthesis (Filmages):</b> ${shortSummary(r.details.synthesis, 100)}</p>`;
|
||||
if (r.details.indications && r.details.indications.length) {
|
||||
html += `<p><b>Indications:</b> ${r.details.indications.join(', ')}</p>`;
|
||||
}
|
||||
if (r.details.counterIndications && r.details.counterIndications.length) {
|
||||
html += `<p><b>Contra-indications:</b> ${r.details.counterIndications.join(', ')}</p>`;
|
||||
}
|
||||
}
|
||||
// Fallback
|
||||
else {
|
||||
html += `<p><b>Summary:</b> ${shortSummary(r.summary)}</p>`;
|
||||
html += `<p><b>Age:</b> ${ageBadge(r.age)}</p>`;
|
||||
}
|
||||
html += `</div>`;
|
||||
});
|
||||
html += `</td></tr>`;
|
||||
});
|
||||
|
||||
html += '</tbody></table>';
|
||||
filmsDiv.innerHTML = html;
|
||||
} catch (error) {
|
||||
console.error('Search function error:', error);
|
||||
filmsDiv.innerHTML = `<p class="no-results">Search failed. Check the console.</p>`;
|
||||
}
|
||||
document.getElementById('q').addEventListener('keydown', e => { if (e.key === 'Enter') search(); });
|
||||
</script>
|
||||
}
|
||||
document.getElementById('q').addEventListener('keydown', e => { if (e.key === 'Enter') search(); });
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -49,8 +49,7 @@ app.get('/search', async (req, res) => {
|
||||
console.log('Filmages results:', fa.length);
|
||||
console.log('FilmsTousPublics results:', ftp.length);
|
||||
console.log('FilmsPourEnfants results:', fpe.length);
|
||||
|
||||
const merged = mergeResults([cine, cs, fa, ftp, fpe]);
|
||||
const merged = mergeResults([cine, cs, fa, ftp, fpe], q, 10); // limit=5
|
||||
res.json(merged);
|
||||
} catch (e) {
|
||||
console.error('General search error:', e);
|
||||
|
Loading…
x
Reference in New Issue
Block a user