-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (34 loc) · 1.2 KB
/
index.js
File metadata and controls
53 lines (34 loc) · 1.2 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
const axios = require("axios");
const cheerio = require("cheerio");
async function getArtistImages(artistName,count){
let srcArray= [];
try{
const pages_no = await getNumberOfImagesPage(artistName);
loop1:
for(let i=1; i<=pages_no; i++){
const { data } = await axios.get(`https://last.fm/music/${artistName}/+images?page=${i}`);
const $ = cheerio.load(data);
const images = $("li.image-list-item-wrapper img");
loop2:
for(let i=0; i<images.length; i++){
if(count === i) break loop1;
const image = images[i];
let src = $(image).attr("src")
src = src.replace("avatar170s/","");
srcArray.push(src);
}
}
} catch(e){
console.log(e);
return srcArray;
}
return srcArray;
}
async function getNumberOfImagesPage(artistName){
const { data } = await axios.get(`https://last.fm/music/${artistName}/+images`);
const $ = cheerio.load(data);
const pages = $("li.pagination-page")
if(pages.length === 0) return 1;
return (parseInt($(pages[pages.length - 1]).find("a").text().replace(/ /g, "")));
}
exports.getArtistImages = getArtistImages;