npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

genius-fetch

v1.0.0

Published

Fetch data from Genius

Downloads

23

Readme

genius-fetch

Fetch data from Genius . Requires a Genius Access Token for certain functions.

Installation

npm i genius-fetch --save

Initialization

// ESM
import Genius from 'genius-fetch';
// CJS
const Genius = require('genius-fetch');

const client = new Genius({
  // Config options go here
});


// You can change or set a config option after initialization. E.g:

client.config({
  accessToken: ...
  debug: true
  ...
});

| Config option | Description | |--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | accessToken | Genius Access Token. Certain API calls require this to be provided. | | rateLimiterEnabled | Whether to rate limit network requests. Default: true. | | rateLimiterOptions | Options passed to the rate limiter, which is just a wrapper around Bottleneck and thus takes the same Bottleneck settings. Default: { maxConcurrent: 5, minTime: 100 }| | cacheEnabled | Whether to cache network responses. Default: true. | | cacheTTL | The time-to-live of cache entries (seconds). Default: 3600. | | maxCacheEntries | The maximum number of cache entries. Once reached, older entries will be removed. Default: 200. | | debug | Whether to log debug messages to the console. Default: false. |

Usage

const matchParams = {
  name: 'Evermore',
  artist: 'Taylor Swift',
};
const options = {
  textFormat: TextFormat.Plain,
  obtainFullInfo: true
};
return client.getSongsByBestMatch(matchParams, options).then((result) => {
  // Do something with result
});

API

Each function returns a Promise that resolves to the fetched data.

Fetching by ID

getSongById(id, options)

getAlbumById(id, options)

getArtistById(id, options)

Fetches a resource by ID. Requires Genius Access Token.

| Option | Description | |------------|----------------------------------------------------------------| | textFormat | Formatting to apply, where applicable: TextFormat.HTML, TextFormat.Plain or TextFormat.DOM. Default: TextFormat.HTML. | | raw | Whether to return raw data. Default: false. |

Returns a Promise that resolves to an object representing the requested resource (i.e., Song, Album or Artist).

Fetching by Name

getSongsByName(name, options)

getAlbumsByName(name, options)

getArtistsByName(name, options)

Fetches resources matching name. Genius Access Token required if obtainFullInfo is set to true in options.

| Option | Description | |----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | limit | The number of results to return (max: 50). Default: 10. | | offset | The offset from which to return results. Default: 0 (i.e. return from first record). | | obtainFullInfo | Whether to fetch full info for each result by calling get<Song/Album/Artist>ById(). If false, only snippet info will be returned. If true, you should make sure you have configured the library with the accessToken config option. Default: false. | | textFormat | Formatting to apply, where applicable: TextFormat.HTML, TextFormat.Plain or TextFormat.DOM. Default: TextFormat.HTML. | | raw | Whether to return raw data. Default false. |

Returns a Promise that resolves to an object with the following properties:

| Property | Description | |--------------|---------------------------------------------------------| | q | The name searched. | | items | An array of objects representing the fetched resources (i.e., Song, Album or Artist). | | limit | The number of results requested. | | offset | The offset from which results are returned. |

Fetching by Best Match

getSongsByBestMatch(matchParams, options)

Returns an array of songs that best matches the criteria specified by matchParams, ordered by relevance. Requires Genius Access Token if album is specified in matchParams, or obtainFullInfo is set to true in options.

This function performs matching on a "best attempt" basis, and does not guarantee that the results returned will fully match the values specified in matchParams.

| Match Param | Description | |-------------|----------------------------------------| | name | Required. The name of the song. | | artist | Artist | | album | Album |

You must specify name and at least one of artist and album in matchParams.

| Option | Description | |----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | limit | The number of results to return (max: 50). Default: 10. | | sampleSize | The number of songs to fetch for best-matching. If it is smaller than limit, it will automatically be set to the same value as limit. Default: 20. | | textFormat | Formatting to apply, where applicable: TextFormat.HTML, TextFormat.Plain or TextFormat.DOM. Default: TextFormat.HTML. | | obtainFullInfo* | Whether to fetch full info for each result by calling getSongById(). If false, only snippet info will be returned. If true, you should make sure you have configured the library with the accessToken config option. Default: false. |

*If album is specified in matchParams, then obtainFullInfo will be overridden with true. This is because the best-match logic requires full info to be obtained.

getSongByBestMatch(matchParams, options)

Convenience function that calls getSongsByBestMatch() and returns the first result (or null if no result is found).

getAlbumsByBestMatch(matchParams, options)

Returns an array of albums that best matches the criteria specified by matchParams, ordered by relevance. Requires Genius Access Token if releaseYear, releaseMonth or releaseDay is specified in matchParams, or obtainFullInfo is set to true in options.

This function performs matching on a "best attempt" basis, and does not guarantee that the results returned will fully match the values specified in matchParams.

| Match Param | Description | |--------------|----------------------------------------| | name | Required. The name of the album. | | artist | Artist | | releaseYear | Year of release. | | releaseMonth | Month of release. | | releaseDay | Day of release. |

You must specify name and at least one of the other parameters in matchParams.

| Option | Description | |----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | limit | The number of results to return (max: 50). Default: 10. | | sampleSize | The number of albums to fetch for best-matching. If it is smaller than limit, it will automatically be set to the same value as limit. Default: 20. | | textFormat | Formatting to apply, where applicable: TextFormat.HTML, TextFormat.Plain or TextFormat.DOM. Default: TextFormat.HTML. | | obtainFullInfo* | Whether to fetch full info for each result by calling getSongById(). If false, only snippet info will be returned. If true, you should make sure you have configured the library with the accessToken config option. Default: false. |

*If releaseYear, releaseMonth or releaseDay is specified in matchParams, then obtainFullInfo will be overridden with true. This is because the best-match logic requires full info to be obtained.

getAlbumByBestMatch(matchParams, options)

Convenience function that calls getAlbumsByBestMatch() and returns the first result (or null if no result is found).

Util Functions

parseSongEmbed(embedValue)

Parses the contents of the embed link contained in a song resource's embed property.

Returns a Promise that resolves to an object with the following properties:

| Property | Description | |--------------|--------------------------------------------------------------------| | linkElements | Array of <link> elements found in the embed content. | | contentParts | Array of HTML elements comprising the 'body' of the embed content. |

Running the Examples

First, provide your Genius Access Token in ./examples/accessToken.ts.

Then:

$ npm run example <name of example script (without .ts file extension)>

E.g.

$ npm run example getSongsByName

Changelog

1.0.0

  • Migrate to TypeScript
  • Package as ESM + CJS hybrid module

0.1.0

  • Initial release

License

MIT