/** * MyAnimeList.net Scraper * Author: Vamz Spectre / VamxAPI * * Fitur: * 1. Search Anime: Cari judul anime lengkap dengan poster HD, episode, skor, & sinopsis. * 2. Detail Anime: Ambil metadata lengkap (judul, skor, ranking, studio, genre, sinopsis, trailer). * 3. Top Anime: Ambil daftar anime dengan rating tertinggi di MyAnimeList. * * Penggunaan CLI: * curl -sL "https://code.vamxapi.my.id/snippet/raw/myanimelist" -o myanimelist.js * node myanimelist.js "naruto" * node myanimelist.js "https://myanimelist.net/anime/20/Naruto" * node myanimelist.js "top" */ const axios = require('axios'); const cheerio = require('cheerio'); const BASE_URL = 'https://myanimelist.net'; const HEADERS = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.9', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache', }; function cleanImageUrl(imgUrl) { if (!imgUrl) return ''; return imgUrl .replace(/\/r\/\d+x\d+/, '') .replace(/\?s=[a-zA-Z0-9]+/, ''); } /** * Cari anime berdasarkan kata kunci * @param {string} query */ async function searchAnime(query) { if (!query || typeof query !== 'string') { throw new Error('Parameter query pencarian wajib diisi.'); } const searchUrl = `${BASE_URL}/anime.php?q=${encodeURIComponent(query.trim())}`; const response = await axios.get(searchUrl, { headers: HEADERS, timeout: 15000, }); const $ = cheerio.load(response.data); const results = []; $('div.js-categories-seasonal table tr').each((_, el) => { const cols = $(el).find('td'); if (cols.length < 5) return; const imgEl = $(cols[0]).find('img'); const rawImg = imgEl.attr('data-src') || imgEl.attr('src') || ''; const image = cleanImageUrl(rawImg); const titleAnchor = $(cols[1]).find('a').first(); const title = titleAnchor.find('strong').text().trim() || titleAnchor.text().trim(); const link = titleAnchor.attr('href') || ''; const synopsis = $(cols[1]).find('.pt4').text().trim(); const type = $(cols[2]).text().trim(); const episodes = $(cols[3]).text().trim(); const score = $(cols[4]).text().trim(); const idMatch = link.match(/\/anime\/(\d+)/); const id = idMatch ? parseInt(idMatch[1], 10) : null; if (title && link) { results.push({ id, title, type, episodes: episodes === '-' ? null : (parseInt(episodes, 10) || episodes), score: score === '-' ? null : (parseFloat(score) || score), image, synopsis: synopsis || null, url: link, }); } }); return { status: true, query, total: results.length, results, }; } /** * Ambil detail lengkap anime berdasarkan ID atau URL * @param {string|number} input */ async function getAnimeDetail(input) { if (!input) { throw new Error('Parameter input (URL atau Anime ID) wajib diisi.'); } let detailUrl = ''; if (typeof input === 'number' || /^\d+$/.test(input.toString().trim())) { detailUrl = `${BASE_URL}/anime/${input.toString().trim()}`; } else if (input.toString().startsWith('http')) { detailUrl = input.toString().trim(); } else { // Jika bukan URL atau ID, anggap sebagai query pencarian pertama const searchRes = await searchAnime(input.toString()); if (searchRes.results.length === 0) { throw new Error(`Anime dengan kata kunci "${input}" tidak ditemukan.`); } detailUrl = searchRes.results[0].url; } const response = await axios.get(detailUrl, { headers: HEADERS, timeout: 15000, }); const $ = cheerio.load(response.data); const title = $('h1.title-name strong').text().trim() || $('h1.title-name').text().trim(); const englishTitle = $('p.title-english').text().trim() || null; const japaneseTitle = $('.title-japanese').text().trim() || null; const scoreText = $('.score-label').first().text().trim(); const score = scoreText ? (parseFloat(scoreText) || scoreText) : null; const ranked = $('.ranked strong').text().trim() || null; const popularity = $('.popularity strong').text().trim() || null; const members = $('.members strong').text().trim() || null; const posterImg = $('img[itemprop="image"]').attr('data-src') || $('img[itemprop="image"]').attr('src') || ''; const image = cleanImageUrl(posterImg); const synopsis = $('p[itemprop="description"]').text().trim() || null; const trailerIframe = $('.video-promotion a.iframe').attr('href') || null; // Metadata sidebar const meta = {}; $('.spaceit_pad').each((_, el) => { const raw = $(el).text().replace(/\s+/g, ' ').trim(); const idx = raw.indexOf(':'); if (idx !== -1) { const key = raw.slice(0, idx).trim(); const val = raw.slice(idx + 1).trim(); if (key && val) { meta[key] = val; } } }); const extractAnchors = (label) => { let list = []; $('.spaceit_pad').each((_, el) => { const darkText = $(el).find('span.dark_text').text().trim().replace(':', ''); if (darkText.toLowerCase() === label.toLowerCase() || darkText.toLowerCase().startsWith(label.toLowerCase())) { $(el).find('a').each((_, a) => { const val = $(a).text().trim(); if (val && !list.includes(val)) list.push(val); }); } }); return list; }; const idMatch = detailUrl.match(/\/anime\/(\d+)/); const id = idMatch ? parseInt(idMatch[1], 10) : null; const studios = extractAnchors('Studios'); const producers = extractAnchors('Producers'); const licensors = extractAnchors('Licensors'); const genres = extractAnchors('Genres').length > 0 ? extractAnchors('Genres') : extractAnchors('Genre'); const theme = extractAnchors('Theme').length > 0 ? extractAnchors('Theme') : extractAnchors('Themes'); return { status: true, id, title, englishTitle, japaneseTitle, score, ranked, popularity, members, type: meta['Type'] || null, episodes: meta['Episodes'] ? (parseInt(meta['Episodes'], 10) || meta['Episodes']) : null, statusAnime: meta['Status'] || null, aired: meta['Aired'] || null, premiered: meta['Premiered'] || null, broadcast: meta['Broadcast'] || null, producers, licensors, studios, source: meta['Source'] || null, genres, theme, duration: meta['Duration'] || null, rating: meta['Rating'] || null, trailerUrl: trailerIframe, image, synopsis, url: detailUrl, }; } /** * Ambil daftar Top Anime dari MyAnimeList * @param {string} type - 'all' | 'airing' | 'upcoming' | 'tv' | 'movie' | 'ova' | 'special' | 'bypopularity' | 'favorite' */ async function getTopAnime(type = 'all') { let url = `${BASE_URL}/topanime.php`; if (type && type !== 'all') { url += `?type=${encodeURIComponent(type)}`; } const response = await axios.get(url, { headers: HEADERS, timeout: 15000, }); const $ = cheerio.load(response.data); const results = []; $('tr.ranking-list').each((_, el) => { const rank = parseInt($(el).find('.rank span').text().trim(), 10) || null; const titleAnchor = $(el).find('.detail h3 a'); const title = titleAnchor.text().trim(); const link = titleAnchor.attr('href') || ''; const imgEl = $(el).find('img'); const rawImg = imgEl.attr('data-src') || imgEl.attr('src') || ''; const image = cleanImageUrl(rawImg); const score = parseFloat($(el).find('.score span').text().trim()) || null; const infoText = $(el).find('.detail .information').text().replace(/\s+/g, ' ').trim(); const idMatch = link.match(/\/anime\/(\d+)/); const id = idMatch ? parseInt(idMatch[1], 10) : null; if (title && link) { results.push({ rank, id, title, score, info: infoText, image, url: link, }); } }); return { status: true, category: type, total: results.length, results, }; } // Module Exports module.exports = { searchAnime, getAnimeDetail, getTopAnime, }; // CLI Runner if (require.main === module) { const arg = process.argv[2] || 'naruto'; (async () => { try { let res; if (arg.toLowerCase() === 'top' || arg.toLowerCase().startsWith('top:')) { const type = arg.includes(':') ? arg.split(':')[1] : 'all'; res = await getTopAnime(type); } else if (arg.startsWith('http') || /^\d+$/.test(arg)) { res = await getAnimeDetail(arg); } else { res = await searchAnime(arg); } console.log(JSON.stringify(res, null, 2)); } catch (err) { console.error(JSON.stringify({ status: false, error: err.message }, null, 2)); process.exit(1); } })(); }