ytmusicapi

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2023 License: MIT Imports: 13 Imported by: 0

README

YTMusic API -- Go client for YouTube Music

An unofficial YTMusic client to retrieve data from the YouTube Music API. This is achieved by emulating the web client using the user's request headers for authentication.

Features

  • Playlist actions

    • Create a new playlist
    • Delete Playlist
    • Get playlist
  • Search(might not return needed information for results that aren't songs)

  • more coming...

Inspiration

This project was inspired by sigma67/ytmusicapi.

Getting Started

  1. Install the package
    go get -d github.com/prettyirrelevant/ytmusicapi
    
  2. Retrieve YTMusic authentication headers using the instructions provided here.
  3. Paste the headers into a file called ytmusic_headers.txt.
  4. Install the CLI tool needed parse the headers.
     go install github.com/prettyirrelevant/ytmusicapi/cmd/ytmusicapi@latest
    
  5. Run the command ytmusicapi setup and ensure that the ytmusic_headers.txt is present in the same directory where the command is run.
  6. A new file ytmusic_headers.json will be created. Copy the content of this file and add to your environment variables.
    export YTMUSIC_HEADERS_JSON=<lorem ipsum dolor>
    
  7. Happy tinkering!

Example

package main

import (
    "fmt"

    "github.com/prettyirrelevant/ytmusicapi"
)

func main() {
    // this should be called once throughout.
    ytmusicapi.Setup()

    // retrieve a playlist
    playlist, err := ytmusicapi.GetPlaylist("playlistID")
    if err != nil {
        fmt.Println("An error occured: ", err.Error())
    }
    fmt.Printf("Playlist: %+v", playlist)
}

Contributing

You feel something is missing or discovered a bug? Kindly open a pull request or an issue respectively.

Documentation

Index

Constants

View Source
const (
	PUBLIC   playlistPrivacyStatus = "PUBLIC"
	PRIVATE  playlistPrivacyStatus = "PRIVATE"
	UNLISTED playlistPrivacyStatus = "UNLISTED"

	NoFilter                 searchFilter = ""
	SongsFilter              searchFilter = "songs"
	AlbumsFilter             searchFilter = "albums"
	VideosFilter             searchFilter = "videos"
	UploadsFilter            searchFilter = "uploads"
	ArtistesFilter           searchFilter = "artists"
	PlaylistsFilter          searchFilter = "playlists"
	FeaturedPlaylistsFilter  searchFilter = "featured_playlists"
	CommunityPlaylistsFilter searchFilter = "community_playlists"

	NoScope      searchScope = ""
	LibraryScope searchScope = "library"
	UploadsScope searchScope = "uploads"
)
View Source
const BaseAPIURL = "https://music.youtube.com/youtubei/v1/"

Variables

This section is empty.

Functions

func CreatePlaylist

func CreatePlaylist(title, description string, privacyStatus playlistPrivacyStatus, sourcePlaylist string, videoIDs []string) (string, error)

CreatePlaylist creates a new playlist on YTMusic

func DeletePlaylist

func DeletePlaylist(playlistID string) error

DeletePlaylist deletes a playlist.

func Setup

func Setup()

Setup initializes the library. NOTE: This must be called before any other function.

Types

type GetContinuationsOpts

type GetContinuationsOpts[T Track | SearchResult] struct {
	ParseFunc        ParseFunc[T]
	Limit            int
	RequestFunc      RequestFunc
	ContinuationType string
	Endpoint         string
	Body             map[string]any
}

type ParseFunc

type ParseFunc[T Track | SearchResult] func(payload gjson.Result) []T

type Playlist

type Playlist struct {
	ID            string
	Title         string
	Description   string
	PrivacyStatus string
	Creator       PlaylistCreator
	Tracks        []Track
}

func GetPlaylist

func GetPlaylist(playlistID string, limit int) (Playlist, error)

GetPlaylist returns a struct containing all items in a playlist.

If `limit` is set to 0, all tracks are retrieved.

type PlaylistCreator

type PlaylistCreator struct {
	ID   string
	Name string
}

type RequestFunc

type RequestFunc func(endpoint string, body map[string]any, additionalParams ...string) (string, error)

type SearchResult

type SearchResult struct {
	Category   string
	ResultType string
	VideoID    string
	Title      string
	Artistes   []string
}

func SearchTracks

func SearchTracks(query string, filter searchFilter, scope searchScope, limit int, shouldIgnoreSpelling bool) ([]SearchResult, error)

SearchTracks returns a list of tracks from YTMusic that match the query provided.

Currently, this is tailored to return information relating to songs.

type Track

type Track struct {
	ID       string
	Title    string
	Artistes []string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL