-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapple_playlist.js
49 lines (44 loc) · 1.73 KB
/
apple_playlist.js
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
const axios = require("axios");
const JSSoup = require("jssoup").default;
const htmlEntities = require("html-entities");
module.exports.getPlaylist = async (url) => {
try {
let playlistObj = {};
const response = await axios.get(url);
let htmlContent = response.data;
let soup = new JSSoup(htmlContent);
//scraping...
const playlistHeaderBlock = soup.find("div", "container-detail-header");
let playlistName, playlistUser;
try {
playlistName = playlistHeaderBlock.find("h1").text.trim();
playlistUser = playlistHeaderBlock
.find("p", "headings__subtitles")
.text.trim();
} catch (err) {
playlistName = "";
playlistUser = "";
}
playlistObj.playlist = htmlEntities.decode(playlistName);
playlistObj.user = htmlEntities.decode(playlistUser);
const tracksInfo = soup.findAll("div", "songs-list-row"); //finding all songs info
playlistObj.songs = [];
for (let track of tracksInfo) {
let songName = track.find("div", "songs-list__col--song").text;
console.log(songName);
let singerNames = track.find("div", "songs-list__col--secondary").text;
let album = track.find("div", "songs-list__col--tertiary").text;
singerNames = singerNames.replace(/\s{2,10}/g, ""); //remove spaces
songName = songName.replace(/\?|<|>|\*|"|:|\||\/|\\/g, ""); //removing special characters which are not allowed in file name
playlistObj.songs.push({
name: htmlEntities.decode(songName),
singer: htmlEntities.decode(singerNames),
album: htmlEntities.decode(album),
});
}
playlistObj.total = playlistObj.songs.length; //total songs count
return playlistObj;
} catch {
return "Some Error";
}
};