mymusic.js 1.7 KB
var cheerio = require('cheerio');
var request = require('request');

var url="https://www.genie.co.kr/chart/top200";

var songList=[];
request(url, function(error, response, html)
{
    var $ = cheerio.load(html);

    const $bodyList= $('#body-content > div.newest-list > div > table > tbody > tr');
    console.log($bodyList.length);

    $bodyList.each(function(i, elem)
    {
        if(i<20)
        {
            songList.push(
                {
                    singer: $(this).find("td.info").find("a.artist.ellipsis").text().trim(),
                    song: $(this).find("td.info").find("a.title.ellipsis").text().trim(),
                    url: 'https://www.genie.co.kr/detail/songInfo?xgnm='+$(this).attr("songid")
                }
            );
        }
        else
        {
            return;
        }
    })
    var result='';
    for(var i=0; i<songList.length; i++)
    {
        result+=i+1 + ". "+ songList[i].singer+" - "+songList[i].song+"\n";
        //console.log(i+1 + ". "+ songList[i].singer+" - "+songList[i].song);
    }

    //console.log(result);
    var randnum=function getRandomInt(min, max)
    {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min)) + min; //최댓값은 제외, 최솟값은 포함
    }(0, 20);

    console.log(randnum);
    var newUrl=songList[randnum].url;

    request(newUrl, function(error, response, html)
    {
        var $ = cheerio.load(html);
        

        var lyric=$('#pLyrics > p').text();
        lyric=lyric.substring(0, 150);

        console.log(songList[randnum].song);
        console.log(lyric);
    });
})

if(songList[0]==undefined)
{
    console.log("hhh");
}
console.log(songList[0]);