UNPKG

pipebomb.js

Version:

Library for interacting with Pipe Bomb servers

253 lines 10.1 kB
import Playlist from "../collection/Playlist.js"; import Track from "../music/Track.js"; import APIVersion from "./APIVersion.js"; import TrackList from "../collection/TrackList.js"; import ExternalCollection from "../collection/ExternalCollection.js"; import Axios from "axios"; import ServerInfo from "../ServerInfo.js"; export default class V1 extends APIVersion { constructor(context, trackCache, collectionCache) { super("v1", context, trackCache, collectionCache); } async search(service, query) { const response = await this.makeRequest("post", "search", { service, query }); if (response.statusCode != 200) throw response; const responseType = response.response.type; if (responseType == "object found") { const foundObject = { responseType: "found object", objectType: response.response.object.type, id: response.response.object.id }; return foundObject; } if (responseType == "search results") { const tracks = []; for (let trackInfo of response.response.items) { if (trackInfo.type == "track") { const track = Track.convertJsonToTrack(this.context, trackInfo); if (track) { this.trackCache.updateTrack(track); tracks.push(track); } } else if (trackInfo.type == "playlist" || trackInfo.type == "album") { const collection = ExternalCollection.convertJsonToExternalCollection(this.context, this.trackCache, this.collectionCache, trackInfo); tracks.push(collection); } } const results = { responseType: "search results", results: tracks }; return results; } throw response; } async getPlaylists() { const response = await this.makeRequest("get", "playlists"); if (response.statusCode != 200) throw response; if (!Array.isArray(response.response)) return []; const collections = []; for (let collectionJson of response.response) { try { const collection = Playlist.convertJsonToPlaylist(this.context, collectionJson); collections.push(collection); } catch (e) { } } return collections; } async getPlaylist(collectionID, outOfDateThreshold) { const instance = await this.context.getInstanceForURI(collectionID); collectionID = instance.id; if (!instance.ownInstance) { if (!instance.instance) throw `Server is not online`; return await instance.instance.v1.getPlaylist(collectionID, outOfDateThreshold); } const cachedPlaylist = this.collectionCache.getCollection(collectionID); if (cachedPlaylist instanceof Playlist) { try { await cachedPlaylist.checkForUpdates(outOfDateThreshold ?? this.context.playlistUpdateFrequency); } catch { } return cachedPlaylist; } const response = await this.makeRequest("get", `playlists/${collectionID}`); if (response.statusCode != 200) throw response; const collection = Playlist.convertJsonToPlaylist(this.context, response.response); return collection; } async createPlaylist(name, trackList) { if (!trackList) trackList = []; const response = await this.makeRequest("post", "playlists", { playlist_title: name, tracks: trackList.map(track => track.trackID) }); if (response.statusCode != 201) throw response; const collection = Playlist.convertJsonToPlaylist(this.context, response.response); return collection; } async getExternalPlaylist(playlistID) { const instance = await this.context.getInstanceForURI(playlistID); playlistID = instance.id; if (!instance.ownInstance) { if (!instance.instance) throw `Server is not online`; return await instance.instance.v1.getExternalPlaylist(playlistID); } const data = await this.makeRequest("get", `externalplaylists/${playlistID}`); if (data.statusCode != 200) throw data; const collection = ExternalCollection.convertJsonToExternalCollection(this.context, this.trackCache, this.collectionCache, data.response); return collection; } async getServices() { const response = await this.makeRequest("get", "services"); if (response.statusCode != 200) throw response; const out = []; if (Array.isArray(response.response)) { for (let data of response.response) { if (typeof data.name == "string" && typeof data.prefix == "string") { out.push({ name: data.name, prefix: data.prefix }); } } } return out; } async getCharts() { const response = await this.makeRequest("get", "charts"); if (response.statusCode != 200) throw response; const out = []; if (Array.isArray(response.response)) { for (let data of response.response) { if (typeof data.slug == "string" && typeof data.name == "string") { out.push(new TrackList(this.collectionCache, `charts/${data.slug}`, data.name, null, data.service, `${this.context.getHost()}/v1/charts/${data.slug}/thumbnail`)); } } } return out; } async getChart(chartSlug) { const instance = await this.context.getInstanceForURI(chartSlug); chartSlug = instance.id; if (!instance.ownInstance) { if (!instance.instance) throw `Server is not online`; return await instance.instance.v1.getChart(chartSlug); } const response = await this.makeRequest("get", `charts/${chartSlug}`); if (response.statusCode != 200) throw response; try { const json = response.response; const criteria = [ typeof json?.slug == "string", typeof json?.name == "string", typeof json?.service == "string", !(json?.trackList) || (() => { if (!Array.isArray(json.trackList)) return false; for (let track of json.trackList) { if (typeof track?.trackID != "string") return false; } return true; })() ]; for (let option of criteria) { if (!option) return null; } let trackList = null; if (json?.trackList) { trackList = []; for (let track of json.trackList) { let trackObject = Track.convertJsonToTrack(this.context, track); if (!trackObject) continue; trackList.push(trackObject); this.trackCache.updateTrack(trackObject); } } const chart = new TrackList(this.collectionCache, "charts/" + json.slug, json.name, trackList, json.service); const output = this.collectionCache.setCollection(chart); if (output instanceof TrackList) return output; return chart; } catch (e) { console.error(e); } return null; } async getUser(userID) { const instance = await this.context.getInstanceForURI(userID); userID = instance.id; if (!instance.ownInstance) { if (!instance.instance) throw `Server is not online`; return await instance.instance.v1.getUser(userID); } const data = await this.makeRequest("get", `user/${userID}`); if (data.statusCode != 200) throw data; const userData = data.response.user; const user = { userID: this.context.prefixAddress(userData.userID), username: userData.username, rawID: userData.userID }; const playlists = []; if (Array.isArray(data.response.playlists)) { for (let rawPlaylist of data.response.playlists) { const playlist = Playlist.convertJsonToPlaylist(this.context, rawPlaylist); playlists.push(playlist); } } return { user, playlists }; } async getRegistryServers(url) { try { const { data } = await Axios.get(url + "/servers/index"); if (data?.statusCode != 200 || !Array.isArray(data?.data)) throw "invalid response"; const servers = []; for (let itemData of data.data) { if (typeof itemData?.address != "string") continue; if (typeof itemData?.name != "string") continue; if (typeof itemData?.https != "boolean") continue; if (typeof itemData?.uptime != "number") continue; const server = new ServerInfo(itemData.address, itemData.name, itemData.https, itemData.uptime); servers.push(server); } return servers; } catch { throw `Failed to retrieve servers from registry '${url}'`; } } } //# sourceMappingURL=V1.js.map