GithubHelp home page GithubHelp logo

josephuspaye / lookup-lyrics Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 153 KB

Lookup lyrics of songs using online sources

License: MIT License

TypeScript 53.83% JavaScript 46.17%
songs lyrics nodejs createweekly

lookup-lyrics's Introduction

lookup-lyrics

Node.js CI

🎶 Lookup lyrics of songs using online sources. Designed for use in Node.js (v10 and above).

This project is part of #CreateWeekly, my attempt to create something new publicly every week in 2020.

How it works

Lookup provides a common interface for looking up song lyrics using various online sources. An adapter is used to implement this interface for each source. Currently, the available sources are:

Installation

npm install @josephuspaye/lookup-lyrics --save

Usage

Look up lyrics

The following example looks up lyrics of the song Happy by Pharrell Williams:

const { getLyrics } = require('@josephuspaye/lookup-lyrics');

async function main() {
  try {
    const lyrics = await getLyrics('happy', 'pharrell williams');
    console.log(JSON.stringify(lyrics, null, '  '));
  } catch (error) {
    console.log('unable to look up lyrics', error);
  }
}

main();
View output
{
  "attribution": "Lyrics from Genius.com",
  "song": "Happy",
  "artist": "Pharrell Williams",
  "album": "G I R L (2014)",
  "lines": "[Produced by Pharrell Williams]\n\n[Verse 1]\nIt might seem crazy what I'm 'bout to say\nSunshine she's here, you can take a break\nI'm a hot air balloon that could go to space\nWith the air, like I don't care, baby, by the way\n\n[Chorus]\n(Because I'm happy)\nClap along if you feel like a room without a roof\n(Because I'm happy)\nClap along if you feel like happiness is the truth\n(Because I'm happy)\nClap along if you know what happiness is to you\n(Because I'm happy)\nClap along if you feel like that's what you wanna do\n\n[Verse 2]\nHere come bad news, talking this and that (Yeah!)\nWell, give me all you got, don't hold it back (Yeah!)\nWell, I should probably warn ya, I'll be just fine (Yeah!)\nNo offense to you, donΓÇÖt waste your time, here's why\n\n[Chorus]\n(Because I'm happy)\nClap along if you feel like a room without a roof\n(Because I'm happy)\nClap along if you feel like happiness is the truth\n(Because I'm happy)\nClap along if you know what happiness is to you\n(Because I'm happy)\nClap along if you feel like that's what you wanna do\n\n[Bridge]\nBring me down\nCan't nothing bring me down\nMy level's too high to bring me down\nCan't nothing bring me down, I said\nBring me down\nCan't nothing bring me down\nMy level's too high to bring me down\nCan't nothing bring me down, I said\n\n[Chorus]\n(Because I'm happy)\nClap along if you feel like a room without a roof\n(Because I'm happy)\nClap along if you feel like happiness is the truth\n(Because I'm happy)\nClap along if you know what happiness is to you\n(Because I'm happy)\nClap along if you feel like that's what you wanna do\n(Because I'm happy)\nClap along if you feel like a room without a roof\n(Because I'm happy)\nClap along if you feel like happiness is the truth\n(Because I'm happy)\nClap along if you know what happiness is to you\n(Because I'm happy)\nClap along if you feel like that's what you wanna do\n\n[Bridge]\nBring me down\nCan't nothing bring me down\nMy level's too high to bring me down\nCan't nothing bring me down, I said...\n\n[Chorus]\n(Because I'm happy)\nClap along if you feel like a room without a roof\n(Because I'm happy)\nClap along if you feel like happiness is the truth\n(Because I'm happy)\nClap along if you know what happiness is to you\n(Because I'm happy)\nClap along if you feel like that's what you wanna do\n(Because I'm happy)\nClap along if you feel like a room without a roof\n(Because I'm happy)\nClap along if you feel like happiness is the truth\n(Because I'm happy)\nClap along if you know what happiness is to you\n(Because I'm happy)\nClap along if you feel like that's what you wanna do\n"
}

API

getLyrics()

Look up lyrics for the given song by the given artist.

function getLyrics(
  song: string,
  artist: string,
  options?: {
    language?: LookupLyrics.Language;
    source?: LookupLyrics.Source;
  }
): Promise<LookupLyrics.LyricsResult>;

Types

The following types are used for parameters and return values.

namespace LookupLyrics {
  /**
   * The sources available for looking up lysics.
   */
  type Source = 'genius';

  /**
   * The available languages
   */
  type Language = 'en';

  /**
   * Result of looking up lyrics
   */
  interface LyricsResult {
    /**
     * Attribution for where the lyrics are from
     */
    attribution: string;

    /**
     * The title of the song
     */
    song: string;

    /**
     * The artist of the song
     */
    artist?: string;

    /**
     * The album of the song
     */
    album?: string;

    /**
     * The lyrics text
     */
    lines: string;
  }
}

Errors

getLyrics() may throw errors matching the following interface:

/**
 * A look up error.
 */
interface Error {
  originalError?: any;
  message?: string;
  type: ErrorType;
}

Where type is a member of the following enum:

/**
 * The errors that could occur when looking up lyrics.
 */
enum ErrorType {
  /**
   * The given song is empty
   */
  'SONG_EMPTY' = 'SONG_EMPTY',

  /**
   * The given artist is empty
   */
  'ARTIST_EMPTY' = 'ARTIST_EMPTY',

  /**
   * Request to the source website failed
   */
  'SOURCE_REQUEST_FAILED' = 'SOURCE_REQUEST_FAILED',

  /**
   * The given source was not recognized
   */
  'UNKNOWN_SOURCE' = 'UNKNOWN_SOURCE',

  /**
   * The given song was not found on the source page: perhaps the page format changed
   */
  'NOT_FOUND' = 'NOT_FOUND',

  /**
   * Unable to extract the lyrics from the source page
   */
  'EXTRACTION_FAILED' = 'EXTRACTION_FAILED',
}

Licence

MIT

lookup-lyrics's People

Contributors

josephuspaye avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.