UNPKG

pipebomb.js

Version:

Library for interacting with Pipe Bomb servers

133 lines 4.88 kB
import Suggestions from "../collection/TrackList.js"; export default class Track { constructor(context, trackID, metadata) { this.lyrics = null; this.context = context; this.rawTrackID = trackID; this.trackID = context.prefixAddress(trackID); this.metadata = metadata || null; } isUnknown() { return this.metadata == null; } async loadMetadata() { if (!this.metadata) { const info = await this.context.makeRequest("get", `v1/tracks/${this.rawTrackID}`); if (info.statusCode != 200) return null; const tempTrack = Track.convertJsonToTrack(this.context, info.response); if (!tempTrack) return null; this.metadata = tempTrack.metadata; } return this.metadata; } getMetadata() { return this.metadata || null; } async getSuggestedTracks() { const collectionCache = this.context.instance.collectionCache; const trackCache = this.context.instance.trackCache; const existing = collectionCache.getCollection(`suggestions/${this.rawTrackID}`); if (existing && existing instanceof Suggestions) return existing; const info = await this.context.makeRequest("get", `v1/tracks/${this.rawTrackID}/suggested`); if (info.statusCode != 200 || !Array.isArray(info.response)) return null; const tracks = []; for (let json of info.response) { const track = Track.convertJsonToTrack(this.context, json); if (track) { trackCache.updateTrack(track); tracks.push(track); } } const suggestions = new Suggestions(collectionCache, "suggestions/" + this.rawTrackID, "Suggestions", tracks); return suggestions; } getAudioUrl() { return `${this.context.serverURL}/v1/tracks/${this.rawTrackID}/audio`; } getThumbnailUrl() { return `${this.context.serverURL}/v1/tracks/${this.rawTrackID}/thumbnail`; } async getLyrics() { if (this.lyrics) { if (!this.lyrics.lyrics.length) return null; return this.lyrics; } try { const response = await this.context.makeRequest("get", `v1/tracks/${this.rawTrackID}/lyrics`); if (response.statusCode !== 200) throw "bad status code"; if (typeof response.response?.synced != "boolean") throw "not boolean"; if (!Array.isArray(response.response?.lyrics)) throw "not array"; const lyrics = { synced: response.response.synced, provider: response.response.provider, lyrics: [] }; if (response.response.synced) { for (let line of response.response.lyrics) { if (!line) continue; if (typeof line.time == "number" && typeof line.words == "string") { lyrics.lyrics.push({ time: line.time, words: line.words }); } } } else { for (let line of response.response.lyrics) { if (!line) continue; if (typeof line.words == "string") { lyrics.lyrics.push({ words: line.words }); } } } this.lyrics = lyrics; return lyrics; } catch { this.lyrics = { synced: false, provider: "none", lyrics: [] }; return null; } } static convertJsonToTrack(context, json) { const criteria = [ typeof json.trackID == "string", json.metadata == null || (typeof json?.metadata?.title == "string" && Array.isArray(json?.metadata?.artists) && (() => { for (let artist of json.metadata.artists) { if (typeof artist != "string") return false; } return true; })()) ]; for (let option of criteria) { if (!option) return null; } const track = new Track(context, json.trackID, json.metadata == null ? null : { title: json.metadata.title, artists: json.metadata.artists, image: json.metadata.image || null }); return track; } } //# sourceMappingURL=Track.js.map