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

kainet-scraper

v1.2.4

Published

YouTube Music scraper for Kainet Music

Downloads

3

Readme

Installation

Just install the package using NPM

npm i --save kainet-scraper

Or using Yarn

yarn add kainet-scraper

And import it directly using CommonJS

const { retrieveSuggestions } = require("kainet-scraper");

Or using the ES6 syntax

import { retrieveSuggestions } from "kainet-scraper";

The package doesn't provide a default export, but you can alternatively use the wildcard import syntax

import * as KainetScraper from "kainet-scraper";

Usage

Interfaces

This package provides a series of TypeScript interfaces.

export interface YtMusicSong 

export interface YtMusicVideo

export interface YtMusicAlbum

export interface YtMusicPlaylist

export interface YtMusicArtist

export type YtMusicElement // union of all

export type YtMusicTrack // union of song & video

You can take a look at the complete signature here.

Because this is a scraper, not a public API, some of the fields may not be available in every request. The fields that are marked as required will never be null nor undefined, as the request will fail and be retried if they're missing. But the fields marked as optional may be null or undefined if they're missing from the scraped request, so make sure to cast them.

Note: Some of the optional fields are never returned in some of the requests, i.e. when searching playlists, their tracks array will always be empty, as it'll only be available on the getPlaylist request.

Main API

Suggestions

retrieveSuggestions(): Promise<YtMusicPlaylist[]>

Retrieves a list of playlists from the YTMusic suggestions section (even though they cannot be personalized for privacy reasons). This is ideal for a homepage.

Note: The returned playlists will never contain neither trackCount nor tracks.

import { retrieveSuggestions } from "kainet-scraper";

const suggestions = await retrieveSuggestions();

Search

search(type: "songs" | "videos" | "albums" | "playlists" | "artists", query: string): Promise<YtMusicElement[]>

Retrieves a list of search results based on the queried type and text. The result array type is narrowed from the queried type.

Note: The returned albums will never contain tracks.
Note: The returned playlists will never contain tracks.

import { search, SearchType } from "kainet-scraper";

const songs = await search(SearchType.SONGS, "queen");
songs[0]?.artist // type has been narrowed and TS knows they are songs

Album

getAlbum(browseId: string): Promise<YtMusicAlbum | null>

Retrieves the full album information, including a list of songs.

import { getAlbum } from "kainet-scraper";

const album = await getAlbum("MPREb_Ab6a3RgiGIy");

Playlist

getPlaylist(browseId: string): Promise<YtMusicPlaylist | null>

Retrieves the full playlist information, including a list of tracks.

import { getPlaylist } from "kainet-scraper";

const playlist = await getPlaylist("VLRDCLAK5uy_n78qsohMVeSYKIrBgWhYbHPjcepbD8YZo");

Artist

// TODO

The full artist page request has still not been implemented.

The type field

Every interface in this package has an only common field, the type field. It's an string containing the name of the type, and each interface has its own name as its only value. This way, it can be easily used for type narrowing with TypeScript.

For example, the most common situation would be to cast every track of a playlist, as they can be songs or videos, but it's also very useful if your own code uses some kind of type union.

// TypeError because album only exists in song
const albums = playlist.tracks.map(track => track.album);

// type narrowed correctly
const albums = playlist.tracks.map(track => track.type === "song" && track.album);

Other API

There are also some utility functions exported for easing the use of the package.

  • Duration

Songs and videos already have both a duration field in seconds and other formated as a string, but this exchange is very common working with time fields.

import { parseDuration } from "kainet-scraper";

parseDuration.fromText("01:01:01"); // 3661
parseDuration.toText(3661); // "01:01:01"
parseDuration.toDetail(3661); // "1 hour & 1 minute"

Related projects

  • Kainet Music - The web application for which this package was built
  • NewPipe Extractor - The scraper built for NewPipe in Java, which mainly inspired this one
  • node-ytdl-core - A YouTube downloader for Node, useful for complementing this package by downloading and playing the scraped tracks

Contributors

Thanks goes to these wonderful people (emoji key):

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

License

Copyright © 2021 TheDavidDelta & contributors.
This project is GNU GPLv3 licensed.