Skip to content

Instantly share code, notes, and snippets.

@xDimGG
Created January 26, 2018 19:14
Show Gist options
  • Save xDimGG/02359bcc6f0b35b91badf0cbf0bea558 to your computer and use it in GitHub Desktop.
Save xDimGG/02359bcc6f0b35b91badf0cbf0bea558 to your computer and use it in GitHub Desktop.
Query & Scrape Genius Lyrics
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());
{
"author": "lloti <dimggyt@gmail.com> (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