-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSongURLs.js
65 lines (47 loc) · 1.51 KB
/
getSongURLs.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
57
58
59
60
61
62
63
64
65
import { readFileSync, writeFileSync } from 'fs'
import { createCache } from './cache'
import { requestAuth } from './utils/request.js'
require('dotenv').config()
const ACCESS_TOKEN = process.env.GENIUS_API_TOKEN
const songURLCache = createCache('songURLs')
async function getSongs (artist) {
const searchURL = `https://api.genius.com/search?q=${artist}&per_page=20`
const response = await requestPage(searchURL, songURLCache)
const hits = response.response.hits.filter(hit => {
return hit.result.primary_artist.name.toLowerCase() === artist.toLowerCase()
})
return getSongData(hits, artist)
}
async function requestPage (url, cache) {
const cacheHasURL = await cache.has(url)
if (cacheHasURL) {
return await cache.get(url)
}
if (!cacheHasURL) {
const requestObject = await requestAuth(url, ACCESS_TOKEN)
const html = requestObject.data
await cache.set(url, html)
return html
}
}
function getSongData (hits, artist) {
const songData = []
hits.forEach(hit => {
songData.push([
artist,
hit.result.url
])
})
return songData
}
let songData = []
async function main () {
const artists = readFileSync('./output/artists-cleaned.txt').toString().split('---')
for (const artist of artists) {
const songsByArtist = await getSongs(artist)
songData = songData.concat(songsByArtist)
console.log(`Collected ${songsByArtist.length} songs by ${artist}`)
}
writeFileSync('./output/songURLs.csv', songData.map(song => song.join('---')).join('\n'))
}
main()