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

spotilink

v1.4.10

Published

Parses Spotify links into Lavalink track objects.

Downloads

30

Readme

npm npm All Contributors

spotilink

A simple module to convert Spotify URLs into song titles for Lavalink to parse into track objects. No need to bother renewing your Spotify access token every time, because it will handle and renew your Spotify token for you.

Prerequisites

  • A Spotify app client. You can log in and create one in https://developer.spotify.com/dashboard
  • Lavalink API https://github.com/Frederikam/Lavalink

Installation

// For npm
npm install spotilink

// For yarn
yarn add spotilink

Simple Usage

const { SpotifyParser } = require('spotilink');

const spotifyID = ''; // Your Spotify app client ID
const spotifySecret = ''; // Your Spotify app client secret
const node = {
	host: 'localhost',
	port: 1234,
	password: 'password'
};

const spotilink =  new SpotifyParser(node, spotifyID, spotifySecret);

// Get a song title
const song = await spotilink.getTrack('1Cv1YLb4q0RzL6pybtaMLo'); // { artists: [ "Surfaces" ], name: "Sunday Best" }

// Get all songs from an album
const album = await spotilink.getAlbumTracks('7tcs1X9pzFvcLOPuhCstQJ'); // [ { artists: [ "Kygo", "Valerie Broussard" ], name: "The Truth" }, ... ]

// Get all songs from a playlist
const playlist = await spotilink.getPlaylistTracks('37i9dQZEVXbMDoHDwVN2tF') // [ { arists: [ "Cardi B", "Megan Thee Stallion" ], name: "WAP (feat. Megan Thee Stallion)" }, ... ]

// Fetch song from the Lavalink API
const track = await spotilink.fetchTrack(song) // { track: "", info: {} }
				.catch(() => console.log("No track found."));

// Fetch songs from playlists from the Lavalink API
const tracks = [];
await Promise.all(album.map(async (name) => tracks.push(await spotilink.fetchTrack(name))));
// 'tracks' will now contain Lavalink track objects.
// SpotifyParser#fetchTrack will only return the track object, giving you complete freedom and control on how you handle the Lavalink tracks. :)

The following methods below, if true is passed as the second parameter, will call Spotilink#fetchTrack and return a Lavalink object (an array of them for getAlbumTracks and getPlaylistTracks) instead of song titles and artists.

// Get a song in the form of a Lavalink object.
const lavalinkSong = await spotilink.getTrack('1Cv1YLb4q0RzL6pybtaMLo', true);
// Get all songs from an album in the form of an array of Lavalink objects.
const album = await spotilink.getAlbumTracks('7tcs1X9pzFvcLOPuhCstQJ', true);
// Get all songs from a playlist in the form of an array of Lavalink objects.
const playlist = await spotilink.getPlaylistTracks('37i9dQZEVXbMDoHDwVN2tF', true);

You can use custom functions to manipulate the search results. The default search results are filtered to those auto-generated by YouTube, but you can disable this using the autoGeneratedOnly option. For the three main methods used, you can pass an object containing the functions for filtering and sorting. By default, no filtering and sorting takes place — whichever track that Lavalink may have received first will be returned.

// Disable filtering search results to only those auto-generated by YouTube.
spotilink.getTrack('track ID', , { autoGeneratedOnly: false });

// Prioritize Lavalink tracks with the same duration as the Spotify track.
spotilink.getTrack('track ID', , { prioritizeSameDuration: true });

// Use a custom synchronous function for filtering search results
// The synchronous function being passed must return a boolean type variable
spotilink.getAlbumTracks('album ID', , { customFilter: (lavalinkTrack, spotifyTrack) => lavalinkTrack.info.title === spotifyTrack.name })

// Use a custom synchronous function for sorting search results
// The synchronous function being passed must return a number type variable
// Variables `comparableTrack` and `compareToTrack` are of LavalinkTrack types
spotilink.getPlaylistTracks('playlist ID', , { customSort: (comparableTrack, compareToTrack, spotifyTrack) => comparableTrack.info.title === spotifyTrack.name ? -1 : 1 })

Please note that if you use the option prioritizeSameDuration, the other options mentioned will be unused. The options customFilter and customSort however, may be used together as long as prioritizeSameDuration is set to false.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!