Updated the search functions
This commit is contained in:
parent
14a4f1a12c
commit
babd2bc2a0
|
|
@ -11,7 +11,7 @@
|
|||
"url": "https://github.com/zS1L3NT/ts-npm-ytmusic-api"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "ts-mocha --timeout 10000 src/__tests__/**/*.spec.ts"
|
||||
"test": "ts-mocha --timeout 30000 src/__tests__/**/*.spec.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.25.0",
|
||||
|
|
|
|||
106
src/YTMusic.ts
106
src/YTMusic.ts
|
|
@ -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 category Type of search results to receive
|
||||
*/
|
||||
public async search(query: string, category: "SONG"): Promise<SongDetailed[]>
|
||||
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) {
|
||||
public async search(query: string): Promise<SearchResult[]> {
|
||||
const searchData = await this.constructRequest("search", {
|
||||
query,
|
||||
params:
|
||||
{
|
||||
SONG: "Eg-KAQwIARAAGAAgACgAMABqChAEEAMQCRAFEAo%3D",
|
||||
VIDEO: "Eg-KAQwIABABGAAgACgAMABqChAEEAMQCRAFEAo%3D",
|
||||
ALBUM: "Eg-KAQwIABAAGAEgACgAMABqChAEEAMQCRAFEAo%3D",
|
||||
ARTIST: "Eg-KAQwIABAAGAAgASgAMABqChAEEAMQCRAFEAo%3D",
|
||||
PLAYLIST: "Eg-KAQwIABAAGAAgACgBMABqChAEEAMQCRAFEAo%3D"
|
||||
}[category!] || null
|
||||
params: null
|
||||
})
|
||||
|
||||
return traverseList(searchData, "musicResponsiveListItemRenderer").map(SearchParser.parse)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(
|
||||
{
|
||||
SONG: SongParser.parseSearchResult,
|
||||
VIDEO: VideoParser.parseSearchResult,
|
||||
ARTIST: ArtistParser.parseSearchResult,
|
||||
ALBUM: AlbumParser.parseSearchResult,
|
||||
PLAYLIST: PlaylistParser.parseSearchResult
|
||||
}[category!] || SearchParser.parse
|
||||
SongParser.parseSearchResult
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches YouTube Music API for videos
|
||||
*
|
||||
* @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
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -37,27 +37,27 @@ queries.forEach(query => {
|
|||
})
|
||||
|
||||
it("Search Songs", async () => {
|
||||
const songs = await ytmusic.search(query, "SONG")
|
||||
const songs = await ytmusic.searchSong(query)
|
||||
expect(songs, LIST(SONG_DETAILED))
|
||||
})
|
||||
|
||||
it("Search Videos", async () => {
|
||||
const videos = await ytmusic.search(query, "VIDEO")
|
||||
const videos = await ytmusic.searchVideo(query)
|
||||
expect(videos, LIST(VIDEO_DETAILED))
|
||||
})
|
||||
|
||||
it("Search Artists", async () => {
|
||||
const artists = await ytmusic.search(query, "ARTIST")
|
||||
const artists = await ytmusic.searchArtist(query)
|
||||
expect(artists, LIST(ARTIST_DETAILED))
|
||||
})
|
||||
|
||||
it("Search Albums", async () => {
|
||||
const albums = await ytmusic.search(query, "ALBUM")
|
||||
const albums = await ytmusic.searchAlbum(query)
|
||||
expect(albums, LIST(ALBUM_DETAILED))
|
||||
})
|
||||
|
||||
it("Search Playlists", async () => {
|
||||
const playlists = await ytmusic.search(query, "PLAYLIST")
|
||||
const playlists = await ytmusic.searchPlaylist(query)
|
||||
expect(playlists, LIST(PLAYLIST_FULL))
|
||||
})
|
||||
|
||||
|
|
@ -70,49 +70,49 @@ queries.forEach(query => {
|
|||
})
|
||||
|
||||
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)
|
||||
expect(song, SONG_FULL)
|
||||
})
|
||||
|
||||
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)
|
||||
expect(video, VIDEO_FULL)
|
||||
})
|
||||
|
||||
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)
|
||||
expect(artist, ARTIST_FULL)
|
||||
})
|
||||
|
||||
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)
|
||||
expect(songs, LIST(SONG_DETAILED))
|
||||
})
|
||||
|
||||
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)
|
||||
expect(albums, LIST(ALBUM_DETAILED))
|
||||
})
|
||||
|
||||
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)
|
||||
expect(album, ALBUM_FULL)
|
||||
})
|
||||
|
||||
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)
|
||||
expect(playlist, PLAYLIST_FULL)
|
||||
})
|
||||
|
||||
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)
|
||||
expect(videos, LIST(PLAYLIST_VIDEO))
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue