-
Notifications
You must be signed in to change notification settings - Fork 0
/
getArtists.js
56 lines (41 loc) · 1.15 KB
/
getArtists.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
50
51
52
53
54
55
56
import { writeFileSync } from 'fs'
import { createCache } from './cache'
import { request } from './utils/request.js'
import { JSDOM } from 'jsdom'
const cache = createCache('artists')
function getURL (page) {
return `https://www.last.fm/tag/hip-hop/artists?page=${page}`
}
async function requestPage (url) {
const cacheHasURL = await cache.has(url)
if (cacheHasURL) {
return await cache.get(url)
}
if (!cacheHasURL) {
const requestObject = await request(url)
const html = requestObject.data
await cache.set(url, html)
return html
}
}
function getArtists (page) {
const dom = new JSDOM(page).window.document
const artists = []
dom.querySelectorAll('a.link-block-target').forEach(a => {
artists.push(a.textContent.trim())
})
return artists
}
const NUMBER_OF_PAGES = 48
let artists = []
async function main () {
for (let i = 1; i <= NUMBER_OF_PAGES; i++) {
const pageURL = getURL(i)
const page = await requestPage(pageURL)
artists = artists.concat(getArtists(page))
console.log(`Page ${i} done!`)
}
writeFileSync('./output/artists.txt', artists.join('---'))
console.log('All done!')
}
main()