yt

package module
v0.0.0-...-c841b71 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: MPL-2.0 Imports: 15 Imported by: 0

README

yt

Go Reference

yt is a Go library for accessing YouTube. To use it, install by running the following:

go get codeberg.org/anabasis/yt

This repository also contains the yt-cli command-line program, which can be found in the cmd/ directory. For more information about yt-cli, read cmd/README.md.

⚙ Features

yt is still currently a work-in-progress, but it does implement the following:

  • search results for YouTube and YouTube Music
  • retrieving data for videos, playlists, and channels
  • deciphering URLs for videos with music and deciphering throttling signatures for videos
  • viewing videos on the "Trending" list and homepage of YouTube

:page_facing_up: License

This repository, including yt and yt-cli, is licensed under the Mozilla Public License Version 2.0. For more details, please see LICENSE.

🙏 Acknowledgments

This would not be possible without yt-dlp and NewPipeExtractor.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecipherSignature

func DecipherSignature(sig string) (string, error)

DecipherSignature parses a "signature cipher" from a video like `s=AAOA...&sp=sig&url=https://rr4---sn-5uaeznld.googlevideo.com/videoplayback...` and returns a deciphered URL and any errors encountered.

func GetBaseJS

func GetBaseJS() (string, error)

GetBaseJS gets the base JavaScript file for the current player version.

func GetDethrottleSignature

func GetDethrottleSignature(streamURL string) (string, error)

GetDethrottleSignature parses a stream URL like `https://rr4---sn-5uaeznld.googlevideo.com/videoplayback?...&n=yYOTSOeAS-63NqMqba&...` and returns the dethrottled value for the `n` key of the URL like `_SM0t1LByqSOuQ` (from `yYOTSOeAS-63NqMqba`) and any errors encountered.

func GetPlayerName

func GetPlayerName() (string, error)

GetPlayerName gets the name of the current player version.

func GetSignatureTimestamp

func GetSignatureTimestamp() (int, error)

func SetCacheDir

func SetCacheDir(dir string)

SetCacheDir sets the cache directory to the supplied value.

Types

type BrowseQuery

type BrowseQuery struct {
	BrowseID string       `json:"browseId"`
	Context  QueryContext `json:"context"`
	Param    Param        `json:"params,omitempty"`
}

BrowseQuery is the data sent to the YouTube browse endpoint (https://www.youtube.com/youtubei/v1/browse) after being JSON-encoded.

type Caption

type Caption struct {
	Name         string
	LanguageCode string
	BaseURL      string
}

type Channel

type Channel struct {
	SimpleChannel

	Links        map[string]string
	Banner       []Thumbnail
	TVBanner     []Thumbnail
	MobileBanner []Thumbnail

	TotalVideos int
	Videos      []SimpleVideo

	IsFamilySafe       bool
	AvailableCountries []string
}

Channel is a representation of the more complete data for a channel retrieved from the channel page on YouTube.

func GetChannel

func GetChannel(id string) (Channel, error)

GetChannel returns data for a channel ID as Channel struct with any errors encountered.

type ClientContext

type ClientContext struct {
	ClientName    string `json:"clientName"`
	ClientVersion string `json:"clientVersion"`
	SDKVersion    int    `json:"androidSdkVersion,omitempty"`
	Language      string `json:"hl,omitempty"`
}

ClientContext is a struct for wrapping client information for video queries.

type ContentPlaybackContext

type ContentPlaybackContext struct {
	SignatureTimestamp int `json:"signatureTimestamp"`
}

type DecipherFunctionData

type DecipherFunctionData struct {
	DecipherFunction string `json:"function"`
	DecipherObject   string `json:"object"`
}

DecipherFunctionData is the data necessary for deciphering a "signature cipher" for a video and is the format for the data is cached.

type MusicParam

type MusicParam Param

SearchParam and MusicParam specify search type for YouTube and YouTube Music, respectively.

const (
	MusicParamSongs              MusicParam = "EgWKAQIIAWoMEAMQBBAOEAoQCRAF"
	MusicParamVideos             MusicParam = "EgWKAQIQAWoMEAMQBBAOEAoQCRAF"
	MusicParamAlbums             MusicParam = "EgWKAQIYAWoMEAMQBBAOEAoQCRAF"
	MusicParamArtists            MusicParam = "EgWKAQIgAWoMEAMQBBAOEAoQCRAF"
	MusicParamFeaturedPlaylists  MusicParam = "EgeKAQQoADgBagwQAxAEEA4QChAJEAU="
	MusicParamCommunityPlaylists MusicParam = "EgeKAQQoAEABagwQAxAEEA4QChAJEAU="
)

Music search parameters are used for specifying types for YouTube Music search.

type MusicSearchResults

type MusicSearchResults struct {
	Songs              *[]SimpleVideo
	Videos             *[]SimpleVideo
	Albums             *[]SimpleAlbumPlaylist
	Artists            *[]SimpleArtistChannel
	FeaturedPlaylists  *[]SimplePlaylist
	CommunityPlaylists *[]SimplePlaylist
}

MusicSearchResults is a representation of search results returned from YouTube Music search.

func GetMusicSearchResults

func GetMusicSearchResults(query string, param MusicParam, lang string) (MusicSearchResults, error)

GetMusicSearchResults returns search results for a YouTube Music search with the supplied query, search types, and language.

type Param

type Param string

Param is a representation of the parameters supplied for specifying search types.

type PlaybackContext

type PlaybackContext struct {
	ContentPlaybackContext ContentPlaybackContext `json:"contentPlaybackContext"`
}

type Playlist

type Playlist struct {
	SimplePlaylist

	Views          int
	Description    string
	LastUpdateDate time.Time

	Videos []SimpleVideo
}

Playlist is a representation of the more complete data for a playlist retrieved from the playlist page on YouTube.

func GetPlaylist

func GetPlaylist(id string) (Playlist, error)

GetPlaylist returns data for a playlist ID as Playlist struct with any errors encountered.

type QueryContext

type QueryContext struct {
	Client ClientContext `json:"client"`
}

QueryContext is a struct for wrapping the ClientContext struct for video queries.

type SearchParam

type SearchParam Param

SearchParam and MusicParam specify search type for YouTube and YouTube Music, respectively.

const (
	SearchParamVideo    SearchParam = "EgIQAQ=="
	SearchParamChannel  SearchParam = "EgIQAg=="
	SearchParamPlaylist SearchParam = "EgIQAw=="
	SearchParamMovie    SearchParam = "EgIQBA=="
)

Search parameters are used for specifying types for YouTube search.

type SearchQuery

type SearchQuery struct {
	Context QueryContext `json:"context"`
	Query   string       `json:"query"`
	Param   Param        `json:"params"`
}

SearchQuery is the data sent to the YouTube search endpoint (https://www.youtube.com/youtubei/v1/search) after being JSON-encoded.

type SearchResults

type SearchResults struct {
	Videos    *[]SimpleVideoSearchResult
	Playlists *[]SimplePlaylist
	Channels  *[]SimpleChannel
}

SearchResults is a representation of search results returned from YouTube search.

func GetSearchResults

func GetSearchResults(query string, param SearchParam, lang string) (SearchResults, error)

GetSearchResults returns search results for a YouTube search with the supplied query, search types, and language.

type SimpleAlbumPlaylist

type SimpleAlbumPlaylist struct {
	ID    string
	Title string

	IsExplicit  bool
	ReleaseYear int
	AlbumType   string

	ArtistChannelID   string
	ArtistChannelName string

	Thumbnails []Thumbnail
}

SimpleAlbumPlaylist is a representation of the data retrieved from search results for an album on YouTube Music.

type SimpleArtistChannel

type SimpleArtistChannel struct {
	ID          string
	Name        string
	Subscribers int

	Thumbnails []Thumbnail
}

SimpleArtistChannel is a representation of the data retrieved from search results for an artist on YouTube Music.

type SimpleChannel

type SimpleChannel struct {
	ID       string
	Name     string
	Username string

	Avatars []Thumbnail

	ShortDescription string
	Subscribers      int
	IsVerified       bool
}

SimpleArtistChannel is a representation of data retrieved from search results for an artist on YouTube Music.

type SimplePlaylist

type SimplePlaylist struct {
	ID          string
	Title       string
	VideoNumber int

	CreatorChannelID   string
	CreatorChannelName string

	Thumbnails []Thumbnail
}

SimplePlaylist is a representation of the data retrieved from search results for a playlist on YouTube.

type SimpleVideo

type SimpleVideo struct {
	ID       string
	Title    string
	Duration time.Duration

	ChannelID   string
	ChannelName string

	Thumbnails []Thumbnail
}

SimpleVideo is a representation of the simplified data retrieved for videos on YouTube on channel pages and some search results.

func GetFrontPageVideos

func GetFrontPageVideos() ([]SimpleVideo, error)

GetFrontPageVideos returns the list of videos that are on the YouTube front page (https://www.youtube.com) as a slice of SimpleVideo structs. These videos will be random videos since no user data is saved (no user recommendations).

func GetTrendingVideos

func GetTrendingVideos() ([]SimpleVideo, error)

GetTrendingVideos returns the list of videos that are currently trending as a slice of SimpleVideo structs.

type SimpleVideoSearchResult

type SimpleVideoSearchResult struct {
	SimpleVideo

	ShortDescription    string
	RelativePublishDate string
	Views               int
}

SimpleVideoSearchResult is a representation of the data retrieved from search results for a video on YouTube.

type StreamFormat

type StreamFormat struct {
	ITag     int
	URL      string
	MimeType string
	Bitrate  int
}

StreamFormat is a representation of the data present for a stream format for a YouTube video.

type StreamingFormats

type StreamingFormats struct {
	StaticFormats   []StreamFormat
	AdaptiveFormats []StreamFormat
}

StreamingFormats is a representation of the data present for streaming formats for a YouTube video.

type ThirdPartyEmbed

type ThirdPartyEmbed struct {
	EmbedURL string `json:"embedUrl"`
}

ThirdPartyEmbed is a struct for wrapping the embed URL for video queries.

type Thumbnail

type Thumbnail struct {
	URL    string
	Width  int
	Height int
}

Thumbnail is a representation of the data present for thumbnails.

type Video

type Video struct {
	SimpleVideo

	Views       int
	UploadDate  time.Time
	PublishDate time.Time

	Description string
	Tags        []string
	Category    string

	AvailableCountries []string
	RatingsAllowed     bool
	IsCrawlable        bool
	IsLive             bool
	IsPrivate          bool
	IsRestricted       bool
	IsUnlisted         bool

	StreamingData StreamingFormats
	CaptionData   []Caption
}

Video is a representation of the more complete data for a video retrieved from the video page on YouTube.

func GetVideo

func GetVideo(id string) (Video, error)

GetVideo returns data for a video ID as Video struct with any errors encountered.

type VideoQuery

type VideoQuery struct {
	VideoID         string          `json:"videoId"`
	Context         QueryContext    `json:"context"`
	ThirdParty      ThirdPartyEmbed `json:"thirdParty,omitempty"`
	PlaybackContext PlaybackContext `json:"playbackContext,omitempty"`
}

VideoQuery is the data sent to the YouTube player endpoint (https://www.youtube.com/youtubei/v1/player) after being JSON-encoded.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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