baelorjs

6 min read Original article ↗

baelorjs

node.js libary for the Baelor (Taylor Swift) API.

Contents

Installation

$ npm install baelorjs

Introduction

baelorjs is a simple node.js libary for the Baelor API.

Below is a simple example which shows it fetching a list of albums.

var Baelor = require('baelorjs'); 

var client = new Baelor({ 

  api_key: "xxx" 

});

client.albums({ 

},function(error,albums) { 

  if(error){console.log(error);} 

  else {

    console.log(albums); 

  }

});

Examples

Here are some of the examples of the libary in use

Create an new API user

var Baelor = require('baelorjs'); 

var client = new Baelor({ 

});

client.userCreate({},function(error,user) {

  if(error){console.log(error);} 

  else {

    console.log("Your API key is: " + user.api_key)

  }

});

Getting a list of album names

var Baelor = require('baelorjs'); 

var client = new Baelor({ 

  api_key: "xxx" 

});

client.albums({},function(error,albums) {

  if(error){console.log(error);} 

  else {

    var names = albums.map(function(album){

      return album.name;

    });

    console.log(names);

  }

});

Find the longest line in a song

var Baelor = require('baelorjs'); 

var client = new Baelor({ 

  api_key: "xxx" 

});

client.songLyrics({

  slug: "style" 

},function(error,lyrics) {

  if(error){console.log(error);} 

  else {

    var lines = lyrics.split('\n'); 

    var longest_length = 0;

    var longest_i = 0;

    for (var i in lines) {

      var current = lines[i].length

      if(current>longest_length){

        longest_length = current;

        longest_i = i;

      }

    };

    console.log("The longest line is line %s, and it says: %s",longest_i+1,lines[longest_i]);

  }

});

Object Types

There are multiple different object types that get returned by the APIs. To keep this doc clean, they will all be described up here instead of at each occurance.

Album Object

Description

An object that describes an album

Example

{

  slug: "xxx", 

  name: "xxx", 

  released_at: "xxxx-xx-xxTxx:xx:xx", 

  length: "xx:xx:xx",

  label: "xxx",

  genres: [], 

  producers: [], 

  songs: [], 

  album_cover:{

    image_id: "xxx" 

  }

}

Song Object

Description

An object that describes a song

Example

{

  slug: "xxx", 

  title: "xxx", 

  length: "xx:xx:xx", 

  writers: [], 

  producers: [], 

  album: {} 

}

Image Object

Description

An object that describes an image

Example

{

  data: "xxx", 

  type: "xxx", 

}

User Object

Description

An object that describes an api user

Example

{

  username: "xxx", 

  email_address: "xxx", 

  api_key: "xxx" 

  is_admin: false 

}

Error Object

Description

An object that describes an error

Example

{

  status_code: 0, 

  description: "xxx", 

  details: [] 

}

Supported APIs

.setKey(apikey)

Description

Sets the api key of the Baelor client to use in API requests. Not needed if you pass in the api_key at init.

Arguments
  • apikey is your Baelor API key. You would acquire this calling .userCreate()
    • Required
Example

.albums(values,callback)

Description

Gets a list of albums. Requires a client with an api key.

Arguments
  • values is an object containing values that are required by the API
    • Has no arguments
  • callback is called when the request completes
Example

client.albums({},function(error,albums) {

  if(error){console.log(error);}

  else {

    console.log(albums);

  }

});

.album(values,callback)

Description

Gets an album. Requires a client with an api key.

Arguments
  • values is an object containing values that are required by the API
    • slug is the slug of the album
      • Required
  • callback is called when the request completes
Example

client.album({

  slug: "xxx"

},function(error,album) {

  if(error){console.log(error);}

  else {

    console.log(album);

  }

});

.songs(values,callback)

Description

Gets a list of songs. Requires a client with an api key.

Arguments
  • values is an object containing values that are required by the API
    • Has no arguments
  • callback is called when the request completes
Example

client.songs({},function(error,songs) {

  if(error){console.log(error);}

  else {

    console.log(songs);

  }

});

.song(values,callback)

Description

Gets an song. Requires a client with an api key.

Arguments
  • values is an object containing values that are required by the API
    • slug is the slug of the song
      • Required
  • callback is called when the request completes
Example

client.song({

  slug: "xxx"

},function(error,song) {

  if(error){console.log(error);}

  else {

    console.log(song);

  }

});

.songLyrics(values,callback)

Description

Gets an songs lyrics. Requires a client with an api key.

Arguments
  • values is an object containing values that are required by the API
    • slug is the slug of the song
      • Required
  • callback is called when the request completes
    • error
      • if error occurs, returns an Error Object
      • else returns null
    • lyrics
      • returns a string of lyrics
Example

client.songLyrics({

  slug: "xxx"

},function(error,lyrics) {

  if(error){console.log(error);}

  else {

    console.log(lyrics);

  }

});

.image(values,callback)

Description

Gets an image (To-Improve). Requires a client with an api key.

Arguments
  • values is an object containing values that are required by the API
    • image_id is the id of an image. For example album artwork
      • Required
  • callback is called when the request completes
Example

client.image({

  image_id: "xxx"

},function(error,image) {

  if(error){console.log(error);}

  else {

  }

});

.user(values,callback)

Description

Gets an API user.

Arguments
  • values is an object containing values that are required by the API
    • Has no arguments
  • callback is called when the request completes
Example

client.user({},function(error,user) {

  if(error){console.log(error);}

  else {

    console.log(user);

  }

});

.userCreate(values,callback)

Description

Creates a new API user.

Arguments
  • values is an object containing values that are required by the API
    • username is your desired username.
      • Required
      • Must be between 1 and 25 characters long
      • Must match (?![\s])(?!.*[_-]{2})[a-zA-Z0-9-_]+(?
    • email is your email address.
      • Required
      • Must be a valid email address.
    • password is your desired password.
      • Required
      • Must be longer than 8 characters
      • Must contain at least two of the following; number, lowercase letter, uppercase letter
  • callback is called when the request completes
Example

client.createUser({

  username: "xxx",

  email: "xxx",

  password: "xxx"

},function(error,user) {

  if(error){console.log(error);}

  else {

    console.log(user);

  }

});

Credz

Baelor API created by Alex Forbes-Reed @0xdeafcafe

baelorjs libary created by Jamie Davies @viralpickaxe