genius

📅 2026-07-19T18:31:27.041Z
👁️ 86 katselukertaa
🔓 Julkinen


"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
const cheerio_1 = require("cheerio");

async function search(query, page, type) {
    if (type !== 'lyric') {
        return;
    }
    // Genius的公开搜索API,带上了免费的令牌
    const result = (await axios_1.default.get('https://api.genius.com/search', {
        headers: {
            'Authorization': 'Bearer O49O_yCcq1qT6ndy6jHqQct3VshX8zJ86G3v18Y03248'
        },
        params: {
            q: query
        }
    })).data;

    const hits = result.response.hits || [];
    
    // 严格按照你给的模版格式进行数据映射
    const data = hits.map(hit => {
        const item = hit.result;
        return {
            title: item.title,
            artist: item.artist_names,
            album: item.album ? item.album.name : "Genius",
            id: item.url // 把歌词页面的网址作为 ID 传给下一步
        };
    });

    return {
        isEnd: true,
        data
    };
}

async function getLyric(musicItem) {
    // 这里的 musicItem.id 就是上面传过来的网页链接
    const res = (await axios_1.default.get(musicItem.id, {
        headers: {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
        }
    })).data;
    const $ = (0, cheerio_1.load)(res);
    
    // 解析 Genius 独特的歌词容器,并把 <br> 换行标签转成真正的换行
    let lyricHtml = '';
    $('[data-lyrics-container="true"]').each((index, el) => {
        lyricHtml += $(el).html() + '\n';
    });

    if (!lyricHtml) {
        lyricHtml = $('.lyrics').html() || '';
    }

    // 清洗掉 HTML 标签,变成可以阅读的歌词文本
    const rawLrc = lyricHtml
        .replace(/<br\s*\/?>/gi, '\n')
        .replace(/<[^>]+>/g, '')
        .trim();

    return {
        rawLrc: rawLrc,
    };
}

module.exports = {
    platform: "Genius歌词",
    version: "0.0.1",
    srcUrl: '',
    cacheControl: "no-store",
    supportedSearchType: ['lyric'],
    search,
    getLyric
};