const axios = require('axios'); const cheerio = require('cheerio'); const BASE_URL = 'https://www.lyricsify.com'; async function _getPageHTML(url) { const headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36', 'cookie': 'SID=saoa3o8c96582nk37ut8fpv80p; _ga=GA1.1.25914292.1759896350; _ga_DBTJEER29X=GS2.1.s1759896349$o1$g0$t1759896360$j49$l0$h0' }; try { const response = await axios.get(url, { headers }); return cheerio.load(response.data); } catch (error) { console.error(`Error: Gagal mengambil halaman ${url}. Status: ${error.response?.status}`); return null; } } async function _scrapeLyricsPage($) { const title = $('h1.textStyle-primary').text().replace(/lrc lyrics download/i, '').trim().replace(/"/g, ''); const artist = $('h2.textStyle-secondary a').text().replace(/Lyrics/i, '').trim(); let lyrics = $('div[id^="lyrics_"][id$="_details"]').html(); if (lyrics) { lyrics = lyrics .replace(/
/g, '\n') .replace(/\[[^\]]*\]/g, '') .replace(/\n{3,}/g, '\n\n') .trim(); } else { lyrics = "Lirik tidak ditemukan."; } return { title, artist, lyrics }; } async function _scrapeSearchPage($) { const results = []; $('div.li a.title').each((_, element) => { const fullTitle = $(element).text().trim(); const url = BASE_URL + $(element).attr('href'); const [artist, title] = fullTitle.split(' - ').map(s => s.trim()); results.push({ title, artist, url }); }); return results; } async function _scrapeTopListPage($) { const result = {}; $('div.ls').each((_, container) => { const categoryTitle = $(container).find('h2').text().trim(); if (categoryTitle) { const items = []; $(container).find('ul.row li a.li').each((_, element) => { const rank = $(element).find('span').first().text().trim(); const title = $(element).find('strong').text().trim(); const artist = $(element).find('small').text().trim(); const url = BASE_URL + $(element).attr('href'); const entry = {}; if (rank) entry.rank = rank; if (title) entry.title = title; if (artist) entry.artist = artist; if (url) entry.url = url; items.push(entry); }); result[categoryTitle] = items; } }); return result; } async function lyricsify(url) { const parsedUrl = new URL(url); const pathname = parsedUrl.pathname; const $ = await _getPageHTML(url); if (!$) return null; if (pathname.startsWith('/lyrics/')) { return _scrapeLyricsPage($); } if (pathname === '/search') { return _scrapeSearchPage($); } if (pathname.startsWith('/top-songs') || pathname.startsWith('/top-albums') || pathname.startsWith('/top-artists')) { return _scrapeTopListPage($); } console.error('URL tidak didukung. Gunakan URL lirik, pencarian, atau halaman top list.'); return null; } module.exports = lyricsify;