Merge branch 'main' into vuepress

This commit is contained in:
Zechariah 2022-03-29 18:45:44 +08:00
commit 25177e26d9
3 changed files with 96 additions and 38 deletions

View File

@ -11,7 +11,7 @@
"url": "https://github.com/zS1L3NT/ts-npm-ytmusic-api" "url": "https://github.com/zS1L3NT/ts-npm-ytmusic-api"
}, },
"scripts": { "scripts": {
"test": "ts-mocha --timeout 10000 src/__tests__/**/*.spec.ts", "test": "ts-mocha --timeout 30000 src/__tests__/**/*.spec.ts",
"docs:dev": "vuepress dev docs", "docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs" "docs:build": "vuepress build docs"
}, },

View File

@ -214,38 +214,96 @@ export default class YTMusic {
} }
/** /**
* Searches YouTube Music API for content * Searches YouTube Music API for results
* *
* @param query Query string * @param query Query string
* @param category Type of search results to receive
*/ */
public async search(query: string, category: "SONG"): Promise<SongDetailed[]> public async search(query: string): Promise<SearchResult[]> {
public async search(query: string, category: "VIDEO"): Promise<VideoDetailed[]>
public async search(query: string, category: "ARTIST"): Promise<ArtistDetailed[]>
public async search(query: string, category: "ALBUM"): Promise<AlbumDetailed[]>
public async search(query: string, category: "PLAYLIST"): Promise<PlaylistFull[]>
public async search(query: string): Promise<SearchResult[]>
public async search(query: string, category?: string) {
const searchData = await this.constructRequest("search", { const searchData = await this.constructRequest("search", {
query, query,
params: params: null
{ })
SONG: "Eg-KAQwIARAAGAAgACgAMABqChAEEAMQCRAFEAo%3D",
VIDEO: "Eg-KAQwIABABGAAgACgAMABqChAEEAMQCRAFEAo%3D", return traverseList(searchData, "musicResponsiveListItemRenderer").map(SearchParser.parse)
ALBUM: "Eg-KAQwIABAAGAEgACgAMABqChAEEAMQCRAFEAo%3D", }
ARTIST: "Eg-KAQwIABAAGAAgASgAMABqChAEEAMQCRAFEAo%3D",
PLAYLIST: "Eg-KAQwIABAAGAAgACgBMABqChAEEAMQCRAFEAo%3D" /**
}[category!] || null * Searches YouTube Music API for songs
*
* @param query Query string
*/
public async searchSong(query: string): Promise<SongDetailed[]> {
const searchData = await this.constructRequest("search", {
query,
params: "Eg-KAQwIARAAGAAgACgAMABqChAEEAMQCRAFEAo%3D"
}) })
return traverseList(searchData, "musicResponsiveListItemRenderer").map( return traverseList(searchData, "musicResponsiveListItemRenderer").map(
{ SongParser.parseSearchResult
SONG: SongParser.parseSearchResult, )
VIDEO: VideoParser.parseSearchResult, }
ARTIST: ArtistParser.parseSearchResult,
ALBUM: AlbumParser.parseSearchResult, /**
PLAYLIST: PlaylistParser.parseSearchResult * Searches YouTube Music API for videos
}[category!] || SearchParser.parse *
* @param query Query string
*/
public async searchVideo(query: string): Promise<VideoDetailed[]> {
const searchData = await this.constructRequest("search", {
query,
params: "Eg-KAQwIABABGAAgACgAMABqChAEEAMQCRAFEAo%3D"
})
return traverseList(searchData, "musicResponsiveListItemRenderer").map(
VideoParser.parseSearchResult
)
}
/**
* Searches YouTube Music API for artists
*
* @param query Query string
*/
public async searchArtist(query: string): Promise<ArtistDetailed[]> {
const searchData = await this.constructRequest("search", {
query,
params: "Eg-KAQwIABAAGAAgASgAMABqChAEEAMQCRAFEAo%3D"
})
return traverseList(searchData, "musicResponsiveListItemRenderer").map(
ArtistParser.parseSearchResult
)
}
/**
* Searches YouTube Music API for albums
*
* @param query Query string
*/
public async searchAlbum(query: string): Promise<AlbumDetailed[]> {
const searchData = await this.constructRequest("search", {
query,
params: "Eg-KAQwIABAAGAEgACgAMABqChAEEAMQCRAFEAo%3D"
})
return traverseList(searchData, "musicResponsiveListItemRenderer").map(
AlbumParser.parseSearchResult
)
}
/**
* Searches YouTube Music API for playlists
*
* @param query Query string
*/
public async searchPlaylist(query: string): Promise<PlaylistFull[]> {
const searchData = await this.constructRequest("search", {
query,
params: "Eg-KAQwIABAAGAAgACgBMABqChAEEAMQCRAFEAo%3D"
})
return traverseList(searchData, "musicResponsiveListItemRenderer").map(
PlaylistParser.parseSearchResult
) )
} }

View File

@ -37,27 +37,27 @@ queries.forEach(query => {
}) })
it("Search Songs", async () => { it("Search Songs", async () => {
const songs = await ytmusic.search(query, "SONG") const songs = await ytmusic.searchSong(query)
expect(songs, LIST(SONG_DETAILED)) expect(songs, LIST(SONG_DETAILED))
}) })
it("Search Videos", async () => { it("Search Videos", async () => {
const videos = await ytmusic.search(query, "VIDEO") const videos = await ytmusic.searchVideo(query)
expect(videos, LIST(VIDEO_DETAILED)) expect(videos, LIST(VIDEO_DETAILED))
}) })
it("Search Artists", async () => { it("Search Artists", async () => {
const artists = await ytmusic.search(query, "ARTIST") const artists = await ytmusic.searchArtist(query)
expect(artists, LIST(ARTIST_DETAILED)) expect(artists, LIST(ARTIST_DETAILED))
}) })
it("Search Albums", async () => { it("Search Albums", async () => {
const albums = await ytmusic.search(query, "ALBUM") const albums = await ytmusic.searchAlbum(query)
expect(albums, LIST(ALBUM_DETAILED)) expect(albums, LIST(ALBUM_DETAILED))
}) })
it("Search Playlists", async () => { it("Search Playlists", async () => {
const playlists = await ytmusic.search(query, "PLAYLIST") const playlists = await ytmusic.searchPlaylist(query)
expect(playlists, LIST(PLAYLIST_FULL)) expect(playlists, LIST(PLAYLIST_FULL))
}) })
@ -70,49 +70,49 @@ queries.forEach(query => {
}) })
it("Get details of the first song result", async () => { it("Get details of the first song result", async () => {
const songs = await ytmusic.search(query, "SONG") const songs = await ytmusic.searchSong(query)
const song = await ytmusic.getSong(songs[0]!.videoId) const song = await ytmusic.getSong(songs[0]!.videoId)
expect(song, SONG_FULL) expect(song, SONG_FULL)
}) })
it("Get details of the first video result", async () => { it("Get details of the first video result", async () => {
const videos = await ytmusic.search(query, "VIDEO") const videos = await ytmusic.searchVideo(query)
const video = await ytmusic.getVideo(videos[0]!.videoId) const video = await ytmusic.getVideo(videos[0]!.videoId)
expect(video, VIDEO_FULL) expect(video, VIDEO_FULL)
}) })
it("Get details of the first artist result", async () => { it("Get details of the first artist result", async () => {
const artists = await ytmusic.search(query, "ARTIST") const artists = await ytmusic.searchArtist(query)
const artist = await ytmusic.getArtist(artists[0]!.artistId) const artist = await ytmusic.getArtist(artists[0]!.artistId)
expect(artist, ARTIST_FULL) expect(artist, ARTIST_FULL)
}) })
it("Get the songs of the first artist result", async () => { it("Get the songs of the first artist result", async () => {
const artists = await ytmusic.search(query, "ARTIST") const artists = await ytmusic.searchArtist(query)
const songs = await ytmusic.getArtistSongs(artists[0]!.artistId) const songs = await ytmusic.getArtistSongs(artists[0]!.artistId)
expect(songs, LIST(SONG_DETAILED)) expect(songs, LIST(SONG_DETAILED))
}) })
it("Get the albums of the first artist result", async () => { it("Get the albums of the first artist result", async () => {
const artists = await ytmusic.search(query, "ARTIST") const artists = await ytmusic.searchArtist(query)
const albums = await ytmusic.getArtistAlbums(artists[0]!.artistId) const albums = await ytmusic.getArtistAlbums(artists[0]!.artistId)
expect(albums, LIST(ALBUM_DETAILED)) expect(albums, LIST(ALBUM_DETAILED))
}) })
it("Get details of the first album result", async () => { it("Get details of the first album result", async () => {
const albums = await ytmusic.search(query, "ALBUM") const albums = await ytmusic.searchAlbum(query)
const album = await ytmusic.getAlbum(albums[0]!.albumId) const album = await ytmusic.getAlbum(albums[0]!.albumId)
expect(album, ALBUM_FULL) expect(album, ALBUM_FULL)
}) })
it("Get details of the first playlist result", async () => { it("Get details of the first playlist result", async () => {
const playlists = await ytmusic.search(query, "PLAYLIST") const playlists = await ytmusic.searchPlaylist(query)
const playlist = await ytmusic.getPlaylist(playlists[0]!.playlistId) const playlist = await ytmusic.getPlaylist(playlists[0]!.playlistId)
expect(playlist, PLAYLIST_FULL) expect(playlist, PLAYLIST_FULL)
}) })
it("Get the videos of the first playlist result", async () => { it("Get the videos of the first playlist result", async () => {
const playlists = await ytmusic.search(query, "PLAYLIST") const playlists = await ytmusic.searchPlaylist(query)
const videos = await ytmusic.getPlaylistVideos(playlists[0]!.playlistId) const videos = await ytmusic.getPlaylistVideos(playlists[0]!.playlistId)
expect(videos, LIST(PLAYLIST_VIDEO)) expect(videos, LIST(PLAYLIST_VIDEO))
}) })