57 lines
2.2 KiB
JavaScript
57 lines
2.2 KiB
JavaScript
const axios = require('axios');
|
|
const cheerio = require('cheerio');
|
|
const BASE = 'https://www.commonsensemedia.org';
|
|
|
|
async function searchMovies(query) {
|
|
const url = `${BASE}/search/${encodeURIComponent(query)}`;
|
|
const res = await axios.get(url, {
|
|
headers: { 'User-Agent': 'Mozilla/5.0', 'accept-language': 'en-US,en;q=0.9' }
|
|
});
|
|
const $ = cheerio.load(res.data);
|
|
const results = [];
|
|
$('.search-results-list__row').each((_, el) => {
|
|
const type = $(el).find('.media-type').text().trim();
|
|
if (type.toLowerCase() !== 'movie') return; // ignore non-movies
|
|
const title = $(el).find('.search-results-product-title').text().trim();
|
|
const link = $(el).find('a.search-results-product-title').attr('href');
|
|
const absLink = link ? BASE + link : null;
|
|
const img = $(el).find('img.search-results-product-image').attr('src');
|
|
// Pas d'année la plupart du temps sur CSM.
|
|
results.push({ title, year: null, img, link: absLink });
|
|
});
|
|
console.log('CSM search:', results);
|
|
return results;
|
|
}
|
|
|
|
async function getMovieClassification(movieUrl) {
|
|
if (!movieUrl) return {};
|
|
const res = await axios.get(movieUrl, { headers: { 'User-Agent': 'Mozilla/5.0' } });
|
|
const $ = cheerio.load(res.data);
|
|
|
|
const age = $('[data-test="age-rating"]').first().text().replace('age', '').replace('+', '').trim() || null;
|
|
const summary = $('[data-test="review-summary"]').first().text().trim();
|
|
const details = [];
|
|
$('[data-test="product-rating-section"]').each((_, el) => {
|
|
const label = $(el).find('[data-test="rating-section-label"]').text().trim();
|
|
const score = $(el).find('.icon-circle-solid.active,.icon-star-solid.active').length;
|
|
const desc = $(el).find('[data-test="rating-section-description"]').text().trim();
|
|
if (label) details.push({ type: label, score, description: desc });
|
|
});
|
|
|
|
return { age, summary, details };
|
|
}
|
|
|
|
async function searchAndEnrich(query) {
|
|
const results = await searchMovies(query);
|
|
return await Promise.all(results.map(async m => ({
|
|
title: m.title,
|
|
year: m.year,
|
|
img: m.img,
|
|
link: m.link,
|
|
source: 'commonsense',
|
|
...(await getMovieClassification(m.link))
|
|
})));
|
|
}
|
|
|
|
module.exports = { searchAndEnrich };
|