Latest
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254/* * Copyright 2022 XXIV * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */const BASE_URL = "https://theaudiodb.com/api/v1/json/2/";
export class AudioDBException extends Error { constructor(message: string) { super(message); }}
/** * * @param {string} s * @return Return Artist details from artist name * @throws AudioDBException on failure */export async function searchArtist(s: string): Promise<any> { try { let req = await fetch(`${BASE_URL}search.php?s=${encodeURI(s)}`); let response = await req.text(); if (response != null && typeof response !== "undefined" && response != "") { let data = JSON.parse(response); if ( data.artists != null && typeof data.artists !== "undefined" && data.artists != "" ) { return data.artists[0]; } else { throw "no results found"; } } else { throw "no results found"; } } catch(err) { throw new AudioDBException(err); }}
/** * * @param {string} s * @return Return Discography for an Artist with Album names and year only * @throws AudioDBException on failure */export async function discography(s: string): Promise<Array<any>> { try { let req = await fetch(`${BASE_URL}discography.php?s=${encodeURI(s)}`); let response = await req.text(); if (response != null && typeof response !== "undefined" && response != "") { let data = JSON.parse(response); if ( data.album != null && typeof data.album !== "undefined" && data.album != "" ) { return data.album; } else { throw "no results found"; } } else { throw "no results found"; } } catch(err) { throw new AudioDBException(err); }}
/** * * @param {number} i * @return Return individual Artist details using known Artist ID * @throws AudioDBException on failure */export async function searchArtistById(i: number): Promise<any> { try { let req = await fetch(`${BASE_URL}artist.php?i=${i}`); let response = await req.text(); if (response != null && typeof response !== "undefined" && response != "") { let data = JSON.parse(response); if ( data.artists != null && typeof data.artists !== "undefined" && data.artists != "" ) { return data.artists[0]; } else { throw "no results found"; } } else { throw "no results found"; } } catch(err) { throw new AudioDBException(err); }}
/** * * @param {number} i * @return Return individual Album info using known Album ID * @throws AudioDBException on failure */export async function searchAlbumById(i: number): Promise<any> { try { let req = await fetch(`${BASE_URL}album.php?m=${i}`); let response = await req.text(); if (response != null && typeof response !== "undefined" && response != "") { let data = JSON.parse(response); if ( data.album != null && typeof data.album !== "undefined" && data.album != "" ) { return data.album[0]; } else { throw "no results found"; } } else { throw "no results found"; } } catch(err) { throw new AudioDBException(err); }}
/** * * @param {number} i * @return Return All Albums for an Artist using known Artist ID * @throws AudioDBException on failure */export async function searchAlbumsByArtistId(i: number): Promise<Array<any>> { try { let req = await fetch(`${BASE_URL}album.php?i=${i}`); let response = await req.text(); if (response != null && typeof response !== "undefined" && response != "") { let data = JSON.parse(response); if ( data.album != null && typeof data.album !== "undefined" && data.album != "" ) { return data.album; } else { throw "no results found"; } } else { throw "no results found"; } } catch(err) { throw new AudioDBException(err); }}
/** * * @param {number} i * @return Return All Tracks for Album from known Album ID * @throws AudioDBException on failure */export async function searchTracksByAlbumId(i: number): Promise<Array<any>> { try { let req = await fetch(`${BASE_URL}track.php?m=${i}`); let response = await req.text(); if (response != null && typeof response !== "undefined" && response != "") { let data = JSON.parse(response); if ( data.track != null && typeof data.track !== "undefined" && data.track != "" ) { return data.track; } else { throw "no results found"; } } else { throw "no results found"; } } catch(err) { throw new AudioDBException(err); }}
/** * * @param {number} i * @return Return individual track info using a known Track ID * @throws AudioDBException on failure */export async function searchTrackById(i: number): Promise<any> { try { let req = await fetch(`${BASE_URL}track.php?h=${i}`); let response = await req.text(); if (response != null && typeof response !== "undefined" && response != "") { let data = JSON.parse(response); if ( data.track != null && typeof data.track !== "undefined" && data.track != "" ) { return data.track[0]; } else { throw "no results found"; } } else { throw "no results found"; } } catch(err) { throw new AudioDBException(err); }}
/** * * @param {number} i * @return Return all the Music videos for a known Artist ID * @throws AudioDBException on failure */export async function searchMusicVideosByArtistId(i: number): Promise<Array<any>> { try { let req = await fetch(`${BASE_URL}mvid.php?i=${i}`); let response = await req.text(); if (response != null && typeof response !== "undefined" && response != "") { let data = JSON.parse(response); if ( data.mvids != null && typeof data.mvids !== "undefined" && data.mvids != "" ) { return data.mvids; } else { throw "no results found"; } } else { throw "no results found"; } } catch(err) { throw new AudioDBException(err); }}