diff --git a/scripts/scrape-onepiece.ts b/scripts/scrape-onepiece.ts index dcfb2c3..344fcac 100644 --- a/scripts/scrape-onepiece.ts +++ b/scripts/scrape-onepiece.ts @@ -704,12 +704,20 @@ function extractEpithets($: cheerio.CheerioAPI): string[] { * Returns both normalized ID and URL */ async function extractDevilFruit($: cheerio.CheerioAPI, lang: 'en' | 'fr'): Promise { - // dfname or dfename are used as data-source for devil fruit in the infobox, we check both to be safe - const fruit = $('[data-source="dfname"] .pi-data-value, [data-source="dfename"] .pi-data-value').first(); - const link = fruit.find('a').first(); - if (link.length === 0) return null; + // dfname, dfename, or dfnomf may be used depending on the wiki language and page format. + const fruit = $( + lang === 'fr' + ? '[data-source="dfnomf"] .pi-data-value, [data-source="dfname"] .pi-data-value, [data-source="dfename"] .pi-data-value' + : '[data-source="dfname"] .pi-data-value, [data-source="dfename"] .pi-data-value, [data-source="dfnomf"] .pi-data-value' + ).first(); + if (fruit.length === 0) return null; - let fruitTitle = fruit.text().trim().toLowerCase().includes('smile') ? fruit.text().trim() : link.text().trim(); + const fruitClone = fruit.clone(); + fruitClone.find('sup, s, del, strike').remove(); + const fruitText = fruitClone.text().replace(/\s+/g, ' ').trim(); + const link = fruit.find('a').first(); + + let fruitTitle = link.length > 0 ? link.text().trim() : fruitText; if (!fruitTitle) return null; let translatedName = null; @@ -717,17 +725,28 @@ async function extractDevilFruit($: cheerio.CheerioAPI, lang: 'en' | 'fr'): Prom if (lang === 'en') { const translatedNameBox = $('[data-source="dfename"] .pi-data-value').first(); if (translatedNameBox.length > 0) { - translatedName = translatedNameBox.text().trim(); + const translatedClone = translatedNameBox.clone(); + translatedClone.find('sup, s, del, strike').remove(); + translatedName = translatedClone.text().replace(/\s+/g, ' ').trim(); } } else if (lang === 'fr') { const translatedNameBox = $('[data-source="dfnomf"] .pi-data-value').first(); if (translatedNameBox.length > 0) { - translatedName = translatedNameBox.text().trim(); + const translatedClone = translatedNameBox.clone(); + translatedClone.find('sup, s, del, strike').remove(); + translatedName = translatedClone.text().replace(/\s+/g, ' ').trim(); } } const href = link.attr('href'); - if (!href || !href.startsWith('/wiki/')) return null; + if (!href || !href.startsWith('/wiki/')) { + return { + devilFruitId: normalizeId(fruitTitle), + devilFruitTitle: fruitTitle, + devilFruitTranslatedName: translatedName || null, + devilFruitUrl: '' + }; + } const cleanUrl = href.replace('/wiki/', '');