spotify-ts-wrapper
Version:
Spotify TypeScript wrapper.
247 lines (246 loc) • 13.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchParser = searchParser;
const getSpotifyId_1 = require("../Utils/getSpotifyId");
function searchParser(body) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
const { data } = body;
const searchV2 = data === null || data === void 0 ? void 0 : data.searchV2;
const preRelease = [];
const albums = [];
(_b = (_a = searchV2 === null || searchV2 === void 0 ? void 0 : searchV2.albumsV2) === null || _a === void 0 ? void 0 : _a.items) === null || _b === void 0 ? void 0 : _b.map((albumItem) => {
const album = albumItem === null || albumItem === void 0 ? void 0 : albumItem.data;
const type = albumItem === null || albumItem === void 0 ? void 0 : albumItem.__typename;
if (type === "PreReleaseResponseWrapper") {
const pre = fetchPreReleaseAlbum(album);
preRelease.push(pre);
}
else {
const al = fetchAlbum(album);
albums.push(al);
}
});
const artist = (_d = (_c = searchV2 === null || searchV2 === void 0 ? void 0 : searchV2.artists) === null || _c === void 0 ? void 0 : _c.items) === null || _d === void 0 ? void 0 : _d.map((item) => {
return fetchArtist(item === null || item === void 0 ? void 0 : item.data);
});
const playlists = (_f = (_e = searchV2 === null || searchV2 === void 0 ? void 0 : searchV2.playlists) === null || _e === void 0 ? void 0 : _e.items) === null || _f === void 0 ? void 0 : _f.map((item) => {
const list = item === null || item === void 0 ? void 0 : item.data;
return fetchPlaylist(list);
});
const tracks = (_h = (_g = searchV2 === null || searchV2 === void 0 ? void 0 : searchV2.tracksV2) === null || _g === void 0 ? void 0 : _g.items) === null || _h === void 0 ? void 0 : _h.map((item) => {
var _a;
const song = (_a = item === null || item === void 0 ? void 0 : item.item) === null || _a === void 0 ? void 0 : _a.data;
return fetchTrack(song);
});
const genres = (_k = (_j = searchV2 === null || searchV2 === void 0 ? void 0 : searchV2.genres) === null || _j === void 0 ? void 0 : _j.items) === null || _k === void 0 ? void 0 : _k.map((item) => {
const genre = item === null || item === void 0 ? void 0 : item.data;
return fetchGenre(genre);
});
const user = (_m = (_l = searchV2 === null || searchV2 === void 0 ? void 0 : searchV2.users) === null || _l === void 0 ? void 0 : _l.items) === null || _m === void 0 ? void 0 : _m.map((item) => {
return fetchUser(item);
});
const podcasts = (_p = (_o = searchV2 === null || searchV2 === void 0 ? void 0 : searchV2.podcasts) === null || _o === void 0 ? void 0 : _o.items) === null || _p === void 0 ? void 0 : _p.map((item) => {
return fetchPodcast(item === null || item === void 0 ? void 0 : item.data);
});
const topResultsV2 = (_r = (_q = searchV2 === null || searchV2 === void 0 ? void 0 : searchV2.topResultsV2) === null || _q === void 0 ? void 0 : _q.itemsV2) === null || _r === void 0 ? void 0 : _r.map((i) => {
var _a;
return fetchTopResult((_a = i === null || i === void 0 ? void 0 : i.item) === null || _a === void 0 ? void 0 : _a.data);
});
return {
tracks,
artist,
genres,
user,
podcasts,
topResults: topResultsV2,
playlists,
albums: {
preRelease,
items: albums,
},
};
}
function fetchTopResult(content) {
const type = content === null || content === void 0 ? void 0 : content.__typename;
const artist = [];
const track = [];
const list = [];
if (type === "Artist") {
const a = fetchArtist(content);
artist.push(a);
}
else if (type === "Track") {
const t = fetchTrack(content);
track.push(t);
}
else if (type === "Playlist") {
const p = fetchPlaylist(content);
list.push(p);
}
return {
artist,
track,
list,
};
}
function fetchPodcast(content) {
var _a, _b, _c, _d;
const title = content === null || content === void 0 ? void 0 : content.name;
const type = content === null || content === void 0 ? void 0 : content.__typename;
if (type === "NotFound")
return;
const mediaType = content === null || content === void 0 ? void 0 : content.mediaType;
const id = (0, getSpotifyId_1.extractSpotifyID)(content === null || content === void 0 ? void 0 : content.uri);
const image = (_c = (_b = (_a = content === null || content === void 0 ? void 0 : content.coverArt) === null || _a === void 0 ? void 0 : _a.sources) === null || _b === void 0 ? void 0 : _b.at(-1)) === null || _c === void 0 ? void 0 : _c.url;
const publisher = (_d = content === null || content === void 0 ? void 0 : content.publisher) === null || _d === void 0 ? void 0 : _d.name;
return {
title,
id,
type,
image,
publisher,
mediaType,
};
}
function fetchUser(content) {
var _a, _b, _c;
const user = content === null || content === void 0 ? void 0 : content.data;
const name = user === null || user === void 0 ? void 0 : user.displayName;
const username = user === null || user === void 0 ? void 0 : user.username;
const type = user === null || user === void 0 ? void 0 : user.__typename;
if (type === "NotFound")
return;
const id = (0, getSpotifyId_1.extractSpotifyID)(user === null || user === void 0 ? void 0 : user.uri);
const image = (_c = (_b = (_a = user === null || user === void 0 ? void 0 : user.avatar) === null || _a === void 0 ? void 0 : _a.sources) === null || _b === void 0 ? void 0 : _b.at(-1)) === null || _c === void 0 ? void 0 : _c.url;
return {
id,
name,
username,
type,
image,
};
}
function fetchGenre(content) {
var _a, _b, _c;
const type = content === null || content === void 0 ? void 0 : content.__typename;
if (type === "NotFound")
return;
const image = (_c = (_b = (_a = content === null || content === void 0 ? void 0 : content.image) === null || _a === void 0 ? void 0 : _a.sources) === null || _b === void 0 ? void 0 : _b.at(-1)) === null || _c === void 0 ? void 0 : _c.url;
const name = content === null || content === void 0 ? void 0 : content.name;
const id = (0, getSpotifyId_1.extractSpotifyID)(content === null || content === void 0 ? void 0 : content.uri);
return {
name,
id,
image,
type,
};
}
function fetchPlaylist(content) {
var _a, _b, _c, _d, _e, _f, _g;
const type = content === null || content === void 0 ? void 0 : content.__typename;
if (type === "NotFound")
return;
const title = content === null || content === void 0 ? void 0 : content.name;
const id = (0, getSpotifyId_1.extractSpotifyID)(content === null || content === void 0 ? void 0 : content.uri);
const format = content === null || content === void 0 ? void 0 : content.format;
const description = content === null || content === void 0 ? void 0 : content.description;
const image = (_d = (_c = (_b = (_a = content === null || content === void 0 ? void 0 : content.images) === null || _a === void 0 ? void 0 : _a.items[0]) === null || _b === void 0 ? void 0 : _b.sources) === null || _c === void 0 ? void 0 : _c.at(0)) === null || _d === void 0 ? void 0 : _d.url;
const _owner = (_e = content === null || content === void 0 ? void 0 : content.ownerV2) === null || _e === void 0 ? void 0 : _e.data;
const owner = {
name: _owner.name,
id: (0, getSpotifyId_1.extractSpotifyID)(_owner === null || _owner === void 0 ? void 0 : _owner.uri),
image: (_g = (_f = _owner === null || _owner === void 0 ? void 0 : _owner.avatar) === null || _f === void 0 ? void 0 : _f.sources) === null || _g === void 0 ? void 0 : _g.at(-1).url,
};
return {
owner,
title,
id,
format,
description,
image,
type,
};
}
function fetchPreReleaseAlbum(content) {
var _a, _b, _c, _d;
const album = content === null || content === void 0 ? void 0 : content.preReleaseContent;
const title = album === null || album === void 0 ? void 0 : album.name;
const type = content === null || content === void 0 ? void 0 : content.__typename;
if (type === "NotFound")
return;
const artwork = (_c = (_b = (_a = album === null || album === void 0 ? void 0 : album.coverArt) === null || _a === void 0 ? void 0 : _a.sources) === null || _b === void 0 ? void 0 : _b.at(-1)) === null || _c === void 0 ? void 0 : _c.url;
const id = (0, getSpotifyId_1.extractSpotifyID)(album === null || album === void 0 ? void 0 : album.uri);
const timezone = content === null || content === void 0 ? void 0 : content.timezone;
const date = (_d = content === null || content === void 0 ? void 0 : content.releaseDate) === null || _d === void 0 ? void 0 : _d.isoString;
return {
title,
type,
artwork,
id,
date,
timezone,
};
}
function fetchTrack(track) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const title = track === null || track === void 0 ? void 0 : track.name;
const album = {
id: (0, getSpotifyId_1.extractSpotifyID)((_a = track === null || track === void 0 ? void 0 : track.albumOfTrack) === null || _a === void 0 ? void 0 : _a.uri),
artwork: (_e = (_d = (_c = (_b = track === null || track === void 0 ? void 0 : track.albumOfTrack) === null || _b === void 0 ? void 0 : _b.coverArt) === null || _c === void 0 ? void 0 : _c.sources) === null || _d === void 0 ? void 0 : _d.at(-1)) === null || _e === void 0 ? void 0 : _e.url,
title: (_f = track === null || track === void 0 ? void 0 : track.albumOfTrack) === null || _f === void 0 ? void 0 : _f.name,
};
const artists = (_h = (_g = track === null || track === void 0 ? void 0 : track.artists) === null || _g === void 0 ? void 0 : _g.items) === null || _h === void 0 ? void 0 : _h.map((a) => {
var _a;
return ({
name: (_a = a.profile) === null || _a === void 0 ? void 0 : _a.name,
id: (0, getSpotifyId_1.extractSpotifyID)(a === null || a === void 0 ? void 0 : a.uri),
});
});
const duration = (_j = track === null || track === void 0 ? void 0 : track.duration) === null || _j === void 0 ? void 0 : _j.totalMilliseconds;
const id = track === null || track === void 0 ? void 0 : track.id;
const contentRating = (_k = track === null || track === void 0 ? void 0 : track.contentRating) === null || _k === void 0 ? void 0 : _k.label;
return {
title,
album,
artists,
duration,
id,
contentRating,
};
}
function fetchAlbum(_latest) {
var _a, _b, _c, _d, _e, _f;
if ((_latest === null || _latest === void 0 ? void 0 : _latest.__typename) === "NotFound")
return;
return {
title: _latest === null || _latest === void 0 ? void 0 : _latest.name,
id: (0, getSpotifyId_1.extractSpotifyID)(_latest === null || _latest === void 0 ? void 0 : _latest.uri),
artwork: (_c = (_b = (_a = _latest === null || _latest === void 0 ? void 0 : _latest.coverArt) === null || _a === void 0 ? void 0 : _a.sources) === null || _b === void 0 ? void 0 : _b.at(-1)) === null || _c === void 0 ? void 0 : _c.url,
date: (_d = _latest === null || _latest === void 0 ? void 0 : _latest.date) === null || _d === void 0 ? void 0 : _d.year,
artists: (_f = (_e = _latest === null || _latest === void 0 ? void 0 : _latest.artists) === null || _e === void 0 ? void 0 : _e.items) === null || _f === void 0 ? void 0 : _f.map((artist) => {
var _a;
return ({
name: (_a = artist === null || artist === void 0 ? void 0 : artist.profile) === null || _a === void 0 ? void 0 : _a.name,
id: (0, getSpotifyId_1.extractSpotifyID)(artist === null || artist === void 0 ? void 0 : artist.uri),
});
}),
type: _latest === null || _latest === void 0 ? void 0 : _latest.__typename,
};
}
function fetchArtist(content) {
var _a, _b, _c, _d, _e, _f;
const type = content === null || content === void 0 ? void 0 : content.__typename;
if (type === "NotFound")
return;
const name = (_a = content === null || content === void 0 ? void 0 : content.profile) === null || _a === void 0 ? void 0 : _a.name;
const id = (0, getSpotifyId_1.extractSpotifyID)(content === null || content === void 0 ? void 0 : content.uri);
const verified = (_b = content === null || content === void 0 ? void 0 : content.profile) === null || _b === void 0 ? void 0 : _b.verified;
const image = (_f = (_e = (_d = (_c = content === null || content === void 0 ? void 0 : content.visuals) === null || _c === void 0 ? void 0 : _c.avatarImage) === null || _d === void 0 ? void 0 : _d.sources) === null || _e === void 0 ? void 0 : _e.at(0)) === null || _f === void 0 ? void 0 : _f.url;
return {
name,
id,
verified,
type,
image,
};
}