Created
January 26, 2018 19:14
-
-
Save xDimGG/02359bcc6f0b35b91badf0cbf0bea558 to your computer and use it in GitHub Desktop.
Query & Scrape Genius Lyrics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { get } = require('snekfetch'); | |
const { load } = require('cheerio'); | |
/** | |
* Get the lyrics for a song using the Genius API | |
* | |
* @param {string} query What to search for | |
* @returns {Promise<String>} Lyrics | |
*/ | |
module.exports = query => | |
get('https://genius.com/api/search/song') | |
.query({ | |
q: query.replace(/[[(].*?[)\]]/g, '').trim() || query, | |
per_page: 1, | |
}) | |
.then(res => | |
res.body.response.sections[0].hits.length | |
? get(res.body.response.sections[0].hits[0].result.url) | |
: Promise.reject(new Error(404)) | |
) | |
.then(res => load(res.text)) | |
.then($ => $('.lyrics').text().trim()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"author": "lloti <[email protected]> (https://dim.codes)", | |
"name": "genius-lyrics-scraper", | |
"version": "1.0.0", | |
"description": "Query & Scrape Genius Lyrics", | |
"private": true, | |
"dependencies": { | |
"cheerio": "^1.0.0-rc.2", | |
"snekfetch": "*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment