UNPKG

spotify-ts-wrapper

Version:

Spotify TypeScript wrapper.

113 lines (112 loc) 5.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.homeFeedParser = homeFeedParser; const getSpotifyId_1 = require("../Utils/getSpotifyId"); function homeFeedParser(body) { var _a, _b, _c, _d; const { data } = body; const home = data === null || data === void 0 ? void 0 : data.home; const greetingText = (_a = home === null || home === void 0 ? void 0 : home.greeting) === null || _a === void 0 ? void 0 : _a.text; const sections = (_d = (_c = (_b = home === null || home === void 0 ? void 0 : home.sectionContainer) === null || _b === void 0 ? void 0 : _b.sections) === null || _c === void 0 ? void 0 : _c.items) === null || _d === void 0 ? void 0 : _d.map((item) => { var _a, _b, _c; const data = item === null || item === void 0 ? void 0 : item.data; const title = (_a = data === null || data === void 0 ? void 0 : data.title) === null || _a === void 0 ? void 0 : _a.text; const artist = []; const album = []; const playlist = []; const items = (_c = (_b = item === null || item === void 0 ? void 0 : item.sectionItems) === null || _b === void 0 ? void 0 : _b.items) === null || _c === void 0 ? void 0 : _c.map((section) => { var _a; const content = section === null || section === void 0 ? void 0 : section.content; const type = (_a = content === null || content === void 0 ? void 0 : content.data) === null || _a === void 0 ? void 0 : _a.__typename; if (type === "Episode" || type === "RestrictedContent" || type === "NotFound") return undefined; if (type === "Artist") { const a = fetchArtist(content === null || content === void 0 ? void 0 : content.data); artist.push(a); } else if (type === "Album") { const a = fetchAlbum(content === null || content === void 0 ? void 0 : content.data); album === null || album === void 0 ? void 0 : album.push(a); } else if (type === "Playlist") { const p = fetchPlaylist(content === null || content === void 0 ? void 0 : content.data); playlist.push(p); } else return null; return { playlists: playlist, albums: album, artists: artist, }; }); return { title, items, }; }); return { greetingText, items: sections, }; } function fetchArtist(content) { var _a, _b, _c, _d, _e; 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 image = (_e = (_d = (_c = (_b = content === null || content === void 0 ? void 0 : content.visuals) === null || _b === void 0 ? void 0 : _b.avatarImage) === null || _c === void 0 ? void 0 : _c.sources) === null || _d === void 0 ? void 0 : _d.at(0)) === null || _e === void 0 ? void 0 : _e.url; return { name, id, type, image, }; } function fetchPlaylist(content) { var _a, _b, _c, _d, _e; 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), }; return { owner, title, id, format, description, image, type, }; } function fetchAlbum(_latest) { var _a, _b, _c, _d, _e; 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, artists: (_e = (_d = _latest === null || _latest === void 0 ? void 0 : _latest.artists) === null || _d === void 0 ? void 0 : _d.items) === null || _e === void 0 ? void 0 : _e.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.albumType, }; }