add max-age slider for search
This commit is contained in:
parent
79aa8f619a
commit
aafd1731ce
19
merge.js
19
merge.js
@ -24,6 +24,10 @@ function getMatchScore(film, query) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
function stripSeason(title) {
|
||||
return normalizeTitle(title).replace(/(saison|season)\s*\d+/g, '').trim();
|
||||
}
|
||||
|
||||
function mergeResults(arrays, query = '', limit = 5) {
|
||||
const merged = [];
|
||||
arrays.flat().forEach(entry => {
|
||||
@ -31,7 +35,19 @@ function mergeResults(arrays, query = '', limit = 5) {
|
||||
let foundIdx = -1;
|
||||
for (let i = 0; i < merged.length; i++) {
|
||||
const m = merged[i];
|
||||
// Match same year AND at least one significant word in common
|
||||
|
||||
// Regroup series/seasons from same source if base title matches (strip "season X"/"saison X")
|
||||
const isSeason = /saison|season/i.test(entry.title) && /saison|season/i.test(m.title);
|
||||
if (
|
||||
m.results[0] && m.results[0].source === entry.source &&
|
||||
isSeason &&
|
||||
stripSeason(m.title) === stripSeason(entry.title)
|
||||
) {
|
||||
foundIdx = i;
|
||||
break;
|
||||
}
|
||||
|
||||
// Default merge: Match same year AND at least one significant word in common
|
||||
if (
|
||||
(m.year ? m.year.toString() : '') === entryYear &&
|
||||
hasSignificantWordOverlap(entry.title, m.title)
|
||||
@ -41,7 +57,6 @@ function mergeResults(arrays, query = '', limit = 5) {
|
||||
}
|
||||
}
|
||||
if (foundIdx >= 0) {
|
||||
console.log(`[MERGE] ${entry.title} (${entryYear}) merged with ${merged[foundIdx].title} (${entryYear})`);
|
||||
merged[foundIdx].results.push({ source: entry.source, ...entry });
|
||||
} else {
|
||||
merged.push({
|
||||
|
@ -7,7 +7,7 @@
|
||||
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background: #1a1a1a; color: #e0e0e0; margin: 0; padding: 20px; font-size: 14px; }
|
||||
.container { max-width: 1200px; margin: auto; }
|
||||
h1 { color: #fff; text-align: center; }
|
||||
.searchbox { margin: 20px 0 30px; display: flex; justify-content: center; }
|
||||
.searchbox { margin: 20px 0 30px; display: flex; justify-content: center; align-items:center; }
|
||||
input[type="text"] { font-size: 1.1em; width: clamp(200px, 60%, 500px); background: #2c2c2c; color: #fff; border: 1px solid #444; border-radius: 4px; padding: 10px 12px; }
|
||||
button { font-size: 1.1em; padding: 10px 18px; background: #007aff; color: #fff; border-radius: 4px; border: none; margin-left: 10px; cursor: pointer; transition: background-color 0.2s; }
|
||||
button:hover { background: #005bb5; }
|
||||
@ -28,6 +28,13 @@
|
||||
.year { color: #aaa; }
|
||||
.loader { text-align: center; font-size: 1.2em; color: #888; }
|
||||
.no-results { text-align: center; font-size: 1.1em; color: #999; margin-top:30px;}
|
||||
.maxagebox { display:flex;align-items:center;margin-left:25px;}
|
||||
.slider-age-badge {
|
||||
font-size:1.25em;font-weight:bold;
|
||||
color:#fff;border-radius:0.5em;padding:2px 10px;
|
||||
min-width:2.5em;display:inline-block;letter-spacing:0.05em;
|
||||
margin-left:10px;transition:background 0.2s;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@ -37,11 +44,15 @@
|
||||
<div class="searchbox">
|
||||
<input type="text" id="q" placeholder="Ex: Dune, Spider-Man, Oppenheimer..." />
|
||||
<button onclick="search()">Rechercher</button>
|
||||
<div class="maxagebox">
|
||||
<label for="maxAge" style="margin-right:9px;">Âge max :</label>
|
||||
<input type="range" id="maxAge" min="3" max="21" value="21" step="1" style="vertical-align:middle;width:140px;" oninput="updateMaxAgeDisplay()">
|
||||
<span id="maxAgeDisplay" class="slider-age-badge">21+</span>
|
||||
</div>
|
||||
</div>
|
||||
<div id="films"></div>
|
||||
</div>
|
||||
<script>
|
||||
// Age color logic
|
||||
function getAgeColor(age) {
|
||||
age = parseInt(age, 10);
|
||||
if (isNaN(age)) return '#999';
|
||||
@ -52,7 +63,6 @@
|
||||
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);
|
||||
@ -71,21 +81,55 @@
|
||||
letter-spacing:0.03em;
|
||||
">${age}+</span>`;
|
||||
}
|
||||
|
||||
// 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*$/, '') + '…';
|
||||
}
|
||||
|
||||
function getAllAges(results) {
|
||||
const ages = [];
|
||||
results.forEach(r => {
|
||||
if (r.source === 'commonsense' && r.age && !isNaN(parseInt(r.age))) {
|
||||
ages.push(parseInt(r.age));
|
||||
}
|
||||
else if (r.source === 'cinecheck') {
|
||||
if (Array.isArray(r.normalizedMarks) && r.normalizedMarks.length) {
|
||||
r.normalizedMarks.forEach(a => { if (!isNaN(parseInt(a))) ages.push(parseInt(a)); });
|
||||
} else if (r.marks && r.marks.length) {
|
||||
r.marks.forEach(a => {
|
||||
const n = parseInt(a);
|
||||
if (!isNaN(n)) ages.push(n);
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (r.source === 'filmages') {
|
||||
if (r.details && r.details.ageLegal && !isNaN(parseInt(r.details.ageLegal))) {
|
||||
ages.push(parseInt(r.details.ageLegal));
|
||||
}
|
||||
if (r.details && r.details.ageSuggested && !isNaN(parseInt(r.details.ageSuggested))) {
|
||||
ages.push(parseInt(r.details.ageSuggested));
|
||||
}
|
||||
}
|
||||
else if (r.age && !isNaN(parseInt(r.age))) {
|
||||
ages.push(parseInt(r.age));
|
||||
}
|
||||
});
|
||||
return ages;
|
||||
}
|
||||
function updateMaxAgeDisplay() {
|
||||
var el = document.getElementById('maxAge');
|
||||
var badge = document.getElementById('maxAgeDisplay');
|
||||
var val = el.value || 21;
|
||||
badge.textContent = val + '+';
|
||||
badge.style.background = getAgeColor(val);
|
||||
}
|
||||
async function search() {
|
||||
const query = document.getElementById('q').value.trim();
|
||||
if (!query) return;
|
||||
window.history.replaceState({}, '', '?q=' + encodeURIComponent(query));
|
||||
const maxAge = parseInt(document.getElementById('maxAge').value, 10);
|
||||
window.history.replaceState({}, '', '?q=' + encodeURIComponent(query) + (maxAge < 21 ? `&maxAge=${maxAge}` : ''));
|
||||
const filmsDiv = document.getElementById('films');
|
||||
filmsDiv.innerHTML = '<p class="loader">Searching...</p>';
|
||||
|
||||
try {
|
||||
const base = window.location.origin;
|
||||
const response = await fetch(`${base}/search?q=${encodeURIComponent(query)}`);
|
||||
@ -93,13 +137,30 @@
|
||||
filmsDiv.innerHTML = `<p class="no-results">Error: ${response.status} ${response.statusText || 'Backend unreachable.'}</p>`;
|
||||
return;
|
||||
}
|
||||
const films = await response.json();
|
||||
|
||||
let films = await response.json();
|
||||
if (!Array.isArray(films) || !films.length) {
|
||||
filmsDiv.innerHTML = '<p class="no-results">No results. Try another query.</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
if (isFinite(maxAge)) {
|
||||
films = films.filter(film => {
|
||||
const uniqueResults = [];
|
||||
const seenSources = new Set();
|
||||
film.results.forEach(r => {
|
||||
if (!seenSources.has(r.source)) {
|
||||
uniqueResults.push(r);
|
||||
seenSources.add(r.source);
|
||||
}
|
||||
});
|
||||
const ages = getAllAges(uniqueResults);
|
||||
if (ages.length === 0) return true;
|
||||
return Math.max(...ages) <= maxAge;
|
||||
});
|
||||
}
|
||||
if (!films.length) {
|
||||
filmsDiv.innerHTML = '<p class="no-results">No results for this max age.</p>';
|
||||
return;
|
||||
}
|
||||
let html = `<table class="results-table">
|
||||
<thead>
|
||||
<tr>
|
||||
@ -109,12 +170,9 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>`;
|
||||
|
||||
films.forEach(film => {
|
||||
// Get first non-empty image from sources
|
||||
const allImgs = film.results.map(r => r.img).filter(Boolean);
|
||||
const mainImg = allImgs.length ? allImgs[0] : null;
|
||||
|
||||
html += `<tr>
|
||||
<td style="vertical-align:top;">
|
||||
<div style="text-align:center;">
|
||||
@ -124,23 +182,20 @@
|
||||
</td>
|
||||
<td class="year" style="vertical-align:top;">${film.year || 'N/A'}</td>
|
||||
<td>`;
|
||||
|
||||
const uniqueResults = [];
|
||||
const seenSources = new Set();
|
||||
film.results.forEach(r => {
|
||||
if (!seenSources.has(r.source)) {
|
||||
uniqueResults.push(r);
|
||||
seenSources.add(r.source);
|
||||
}
|
||||
});
|
||||
uniqueResults.forEach(r => {
|
||||
const uniqueResults = [];
|
||||
const seenSources = new Set();
|
||||
film.results.forEach(r => {
|
||||
if (!seenSources.has(r.source)) {
|
||||
uniqueResults.push(r);
|
||||
seenSources.add(r.source);
|
||||
}
|
||||
});
|
||||
uniqueResults.forEach(r => {
|
||||
html += `<div class="source-block">`;
|
||||
html += `<p class="source-name">${r.source.charAt(0).toUpperCase() + r.source.slice(1)}</p>`;
|
||||
// (No image here anymore)
|
||||
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>`;
|
||||
@ -152,7 +207,6 @@
|
||||
html += `</ul>`;
|
||||
}
|
||||
}
|
||||
// Cinecheck
|
||||
else if (r.source === 'cinecheck') {
|
||||
let numericAges = Array.isArray(r.normalizedMarks) && r.normalizedMarks.length
|
||||
? r.normalizedMarks
|
||||
@ -167,7 +221,6 @@
|
||||
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>`;
|
||||
@ -175,13 +228,12 @@
|
||||
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> ${shortSummary(r.details.indications.join(', '), 100)}</p>`;
|
||||
html += `<p><b>Indications:</b> ${shortSummary(r.details.indications.join(', '), 100)}</p>`;
|
||||
}
|
||||
if (r.details.counterIndications && r.details.counterIndications.length) {
|
||||
html += `<p><b>Contra-indications:</b> ${shortSummary(r.details.counterIndications.join(', '), 100)}</p>`;
|
||||
html += `<p><b>Contra-indications:</b> ${shortSummary(r.details.counterIndications.join(', '), 100)}</p>`;
|
||||
}
|
||||
}
|
||||
// Fallback
|
||||
else {
|
||||
html += `<p><b>Summary:</b> ${shortSummary(r.summary)}</p>`;
|
||||
html += `<p><b>Age:</b> ${ageBadge(r.age)}</p>`;
|
||||
@ -190,7 +242,6 @@
|
||||
});
|
||||
html += `</td></tr>`;
|
||||
});
|
||||
|
||||
html += '</tbody></table>';
|
||||
filmsDiv.innerHTML = html;
|
||||
} catch (error) {
|
||||
@ -198,14 +249,23 @@
|
||||
filmsDiv.innerHTML = `<p class="no-results">Search failed. Check the console.</p>`;
|
||||
}
|
||||
}
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const q = params.get('q');
|
||||
if (q) {
|
||||
document.getElementById('q').value = q;
|
||||
search();
|
||||
}
|
||||
});
|
||||
document.getElementById('maxAge').addEventListener('input', function() {
|
||||
updateMaxAgeDisplay();
|
||||
search();
|
||||
});
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const q = params.get('q');
|
||||
const maxAge = params.get('maxAge');
|
||||
if (maxAge) {
|
||||
document.getElementById('maxAge').value = maxAge;
|
||||
updateMaxAgeDisplay();
|
||||
}
|
||||
if (q) {
|
||||
document.getElementById('q').value = q;
|
||||
search();
|
||||
}
|
||||
});
|
||||
document.getElementById('q').addEventListener('keydown', e => { if (e.key === 'Enter') search(); });
|
||||
</script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user