mymusic.js
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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]);