GithubHelp home page GithubHelp logo

vixalien / muse Goto Github PK

View Code? Open in Web Editor NEW
33.0 7.0 5.0 578 KB

Youtube Music API (InnerTube) client for Deno, Node and the browser

Home Page: https://deno.land/x/muse

License: MIT License

TypeScript 100.00%
deno innertube youtube youtube-music

muse's Introduction

muse

A library to interact with the YouTube Music (InnerTube) api.

Note: This library is still in development, and is not ready for production use.

Usage

muse works on Deno, Node.js, the browser and any javascript environment that supports ES modules and fetch.

You can read the docs for more information about the usage of each function.

Don't forget to replace VERSION with the latest version

Deno

import * as muse from "https://deno.land/x/muse@VERSION/mod.ts";

// you can also use the latest version (not recommended) with
// import * as muse from "https://deno.land/x/muse/mod.ts";

// you can also import directly from github
// import * as muse from "https://raw.githubusercontent.com/vixalien/muse/VERSION/mod.ts";

const search_results = await muse.search("drake");
const rickroll = await muse.get_song("dQw4w9WgXcQ");

Browser

You'll need to use a CDN that supports ES modules, such as esm.sh, jspm.io or skypack.dev.

Proxy

You'll also need to use a proxy server to get around CORS errors. It's a good idea to self host the proxy server (cors-anywhere and deno_deploy_cors_proxy are good options).

import * as muse from "https://esm.sh/libmuse@VERSION";

// import * as muse from "https://jspm.dev/npm:libmuse@VERSION";
// import * as muse from "https://cdn.skypack.dev/libmuse@VERSION";

// setting up our proxy
muse.set_option("fetch", (url, options) => {
  return fetch(`https://proxy.example.com/${url}`, options);
});

const search_results = await muse.search("top radio");

Node

First install using your preferred package manager (npm, yarn, pnpm etc.)

npm install libmuse

Then use it in by importing libmuse. The Node version has the exact same features as the Deno version.

import * as muse from "libmuse";
// commonjs: const muse = require("libmuse");

const artist = await muse.get_artist("UCvyjk7zKlaFyNIPZ-Pyvkng");

For the complete list of operations, see the docs.

Auth

Currently, muse supports oauth authentication by posing as the YouTube TV app.

Here's the flow:

  1. Get a login code
  2. Go to the given login url, and type in the login code on a device that is logged into a google account
  3. Get the OAuth token & refresh tokens
import * as muse from "https://deno.land/x/muse@VERSION/mod.ts";

const auth = muse.get_option("auth");

muse.setup({
  // make sure to persist the token
  store: new DenoFileStore("store/muse-store.json"),
  debug: true,
});

// this is the authentication flow
const auth_flow = async () => {
  if (auth.has_token()) return;
  console.log("Getting login code...");

  const loginCode = await auth.get_login_code();

  console.log(
    `Go to ${loginCode.verification_url} and enter the code: ${loginCode.user_code}`,
  );

  // not necessary, but saves some requests
  alert("Press enter when you have logged in");

  console.log("Loading token...");

  await auth.load_token_with_code(
    loginCode.device_code,
    loginCode.interval,
  );

  console.log("Logged in!", auth._token);
};

// listen to the `requires-login` event, then resolve pass on a function that
// returns a promise that will resolve when the auth flow is complete
auth.addEventListener("requires-login", (event) => {
  const resolve = event.detail;

  resolve(auth_flow);
});

In the future, I plan to add support for other auth methods, such as cookies and Youtube TV login codes.

Storage

You can pass in a storage object to the client to persist the auth token.

import * as muse from "https://deno.land/x/muse@VERSION/mod.ts";

// you can use the "best" store, which is DenoFileStore if available, then LocalStorageStore, then MemoryStore
const client = muse.setup({ store: muse.get_best_store() });

// or you can use any of the built-in stores
const client = muse.setup({ store: new muse.DenoFileStore("/path/to/file.json") });
const client = muse.setup({ store: new muse.LocalStorageStore() });
const client = muse.setup({ store: new muse.MemoryStore() });

// or you can implement your own store
// by extending the Store abstract class
class MyStore extends muse.Store {
  get<T>(key: string): T | null;
  set(key: string, value: unknown): void;
  delete(key: string): void;
}

// then use it accordingly
const client = muse.setup({ store: new MyStore() });

// Do note that setup() can be called multiple times, but it's not recommended. 
// this is because setup overrides the global store, so if you call setup()
// multiple times, other options set before will be ignored. example:

muse.setup({ auth: { /* custom auth options */ } });
muse.setup({ store: /* custom store */ });

// the above will only use the custom store, and ignore the custom auth options

// if you need to configure options many times use `muse.set_option`
muse.set_option("store", /* custom store */)

Operations

I'm currently targetting to match the ytmusicapi's capabilities.

search

  • search
  • search suggestions

browsing

  • home
  • get artist
  • get artist albums
  • get album
  • get album browse id
  • get user
  • get user playlists
  • get song
  • get song related
  • get lyrics
  • get tasteprofile
  • set tasteprofile

explore

  • get explore
  • get mood categories
  • get mood playlists
  • get charts
  • get new releases

watch

  • get queue get watch playlist

library

  • get library
  • get library playlists
  • get library songs
  • get library albums
  • get library artists
  • get library subscriptions
  • get liked songs
  • get history
  • add history item
  • remove history items
  • rate song
  • edit song library status
  • rate playlist
  • subscribe artists
  • unsubscribe artists

playlists

  • get playlist
  • create playlist
  • edit playlist
  • delete playlist
  • add playlist items
  • remove playlist items

uploads

  • get library uploads
  • get library upload songs
  • get library upload artists
  • get library upload albums
  • get library upload artist
  • get library upload album
  • upload song (doesn't currectly work because the TV client can't do uploads)
  • delete upload entity

Acknowledgements

  • ytmusicapi - The inspiration for this library
  • Youtube Internal Clients - The source of the client names and versions
  • many random gists and blog posts whose links I've lost

muse's People

Contributors

ozonezone avatar vixalien avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

muse's Issues

GET_ALBUM returns the videoId of the Youtube video instead of the actual music track from the ytMusic.

Consider the foloowing code

const album = "MPREb_jcNZuIQM5me"; // Album: Curtain Call 2 - Singer: Eminem - Album Link: https://music.youtube.com/playlist?list=OLAK5uy_niAPdRC-UM6CbmUaldt6_FUgIhSb2gayM

get_album(album).then(data => {
    const tracksLength = data.tracks.length; // 35 Songs

    const track = data.tracks[0] // First Song i.e. Godzilla (feat. Juice WRLD) - VideoID: r_0JjYUe5jo
})

The get_album returns the videoId of the actual youtube video(r_0JjYUe5jo). How can we get the videoId of the song from ytmusic(5UpETVVdRaA)?

Thanks & Regards,
Utkarsh Vishnoi

MuseError: JSONPath expression "sectionListRenderer.contents[0],gridRenderer" returned nothing

I'm trying to use get_artist_albums() but it returns the error:

MuseError: JSONPath expression "sectionListRenderer.contents[0],gridRenderer" returned nothing
    at j (https://deno.land/x/[email protected]/util.ts:33:11)
    at get_artist_albums (https://deno.land/x/[email protected]/mixins/browsing.ts:259:16)
    at eventLoopTick (ext:core/01_core.js:64:7)
    at async getYouTubeMusic (file:///home/deno/functions/discography/index.ts:59:20)
    at async Object.handler (file:///home/deno/functions/discography/index.ts:81:13)
    at async handleHttp (ext:sb_core_main_js/js/http.js:107:17) {
  name: "MuseError",
  code: 10
}

It could be because I don't know what to put in as "params"? I just put in an empty string because I can't seem to find info about what those params should be? https://deno.land/x/[email protected]/mod.ts?s=get_artist_albums

Unable to get the get_artist_albums working.

Hi,
Below is my working code so far. I am able to get the artist details but it fails to fetch the albums. I even tried with a time delay, and I tried with passing channelId, browseId from the albums, but none seem to work. Could you please assist with the issue?

const { get_artist, search, get_artist_albums } = require("libmuse");

search("eminem", { filter: "artists" }).then(data => {
    // console.log(data)
    const artist = data.top_result.browseId;
    // get_artist(artist).then(data => get_artist_albums(artist, {}).then(data => console.log(data)));
    get_artist(artist).then(data => {
        console.log(data)
        setTimeout(function(){ get_artist_albums(data.albums.browseId, data.albums.params).then(data => console.log(data)) }, 3000);
    });
})

Adding search filter returns no result

Adding a filter to the search function returns no result, or at least i think so. Did i at least do everything correctly?

Here is my code:
search("Change Deftones", { filter: "songs" }).then(data => console.log(data));

Here is the result.

{
  top_result: null,
  did_you_mean: null,
  categories: [ { title: 'Songs', filter: 'songs', results: [Array] } ],    
  continuation: 'EqgDEg9DaGFuZ2UgRGVmdG9u...',
  autocorrect: null,
  filters: [
    'songs',
    'videos',
    'albums',
    'featured_playlists',
    'community_playlists',
    'artists'
  ]
}

v1

necessities for v1

  • robust testing
  • good docs

Option Proxy seems like not working

Code:

<!doctype html> <!-- i think this is not related with issue, look JS code block below -->
<html lang="">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>YouTube Music</title>
  <link rel="stylesheet" href="css/style.css">
  <link rel="stylesheet" href="css/youtubemusic.css">
  <meta name="description" content="">

  <meta property="og:title" content="YouTube Music">
  <meta property="og:site_name" content="YouTube Music">
  <meta name="description" content="Unofficial YouTube Music Web">
  <meta property="og:type" content="website">
  <!--
  <meta property="og:url" content="https://music.youtube.com/">
  <meta property="al:ios:app_name" content="YouTube Music">
  <meta property="al:android:app_name" content="YouTube Music">
  <meta name="twitter:url" content="https://music.youtube.com/">
  <meta name="twitter:app:name:iphone" content="YouTube Music">
  <meta name="twitter:app:name:ipad" content="YouTube Music">
  <meta property="og:image" content="">
  <meta property="og:image:alt" content="">
  -->

  <link rel="icon" href="https://music.youtube.com/favicon.ico" sizes="16x16" type="image/x-icon">
  <link rel="icon" href="https://music.youtube.com/img/favicon_32.png" sizes="32x32" type="image/png">
  <link rel="icon" href="https://music.youtube.com/img/favicon_48.png" sizes="48x48" type="image/png">
  <link rel="icon" href="https://music.youtube.com/img/favicon_96.png" sizes="96x96" type="image/png">
  <link rel="icon" href="https://music.youtube.com/img/favicon_144.png" sizes="144x144" type="image/png">
  <link rel="apple-touch-icon" href="https://music.youtube.com/img/favicon_96.png">

  <link rel="manifest" href="youtubemusic.webmanifest">
</head>

<body>

<p>Hellified world!</p>



<script src="js/youtubemusic.js" type="module"></script>
</body>
</html>
//import { search, set_option } from "https://esm.sh/[email protected]";
//import { search, set_option } from "https://jspm.dev/npm:[email protected]";
//import { search, set_option } from "https://cdn.skypack.dev/[email protected]";
import * as muse from "https://esm.sh/[email protected]";
muse.set_option("proxy", "http://45.32.254.253:8000/"); // VPS Running(deleted now): https://raw.githubusercontent.com/justjavac/deno_deploy_cors_proxy/main/main.ts

muse.search("top radio").then(data => {
  console.log("search results", data);
});

image

tested on 0.0.97 and 0.0.31

[bug] get_playlist always returns 200 items

steps to reproduce:

const { get_playlist, search } = require('libmuse');

const main = async () => {
  const val = await get_playlist('PLv4y5OVUmyFjx4uf_sQunpq8nlQiar_iQ', {
    limit: 800,
  });
  console.log(val.tracks.length);
};

// expected output: 800 (or 900 as in ytmusicapi)
// actual output: 200
main();

ytmusicapi counterpart:

from ytmusicapi import YTMusic
yt = YTMusic()
print(len(yt.get_playlist('PLv4y5OVUmyFjx4uf_sQunpq8nlQiar_iQ', limit=800)['tracks']))
# output is 900

possible fix:
lovegaoshi@641ebed would do, as in ytmusicapi

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.