UNPKG

bandcamp-fetch

Version:
143 lines 6.7 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _a, _DiscoverResultParser_parseAlbum, _DiscoverResultParser_parseShirt, _DiscoverResultParser_getImageURL, _DiscoverResultParser_stripFromDiscoverPage; Object.defineProperty(exports, "__esModule", { value: true }); const url_1 = require("url"); const Parse_js_1 = require("../utils/Parse.js"); class DiscoverResultParser { static parseDiscoverResult(json, opts, params, availableOptions) { if (typeof json === 'object' && Array.isArray(json.results)) { const items = json.results.reduce((result, item) => { switch (item.result_type) { case 'a': { const album = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_parseAlbum).call(this, item, opts, availableOptions); result.push(album); break; } case 's': { const shirt = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_parseShirt).call(this, item, opts); result.push(shirt); break; } } return result; }, []); const parsed = { items, total: json.result_count, params }; if (json.cursor) { parsed.continuation = { ...params, cursor: json.cursor }; if (opts.albumImageFormat) { parsed.continuation.albumImageFormat = opts.albumImageFormat; } if (opts.artistImageFormat) { parsed.continuation.artistImageFormat = opts.artistImageFormat; } if (opts.merchImageFormat) { parsed.continuation.merchImageFormat = opts.merchImageFormat; } } return parsed; } throw new Parse_js_1.ParseError('Failed to parse discover results: data is missing or has invalid \'results\' field.', json); } } _a = DiscoverResultParser, _DiscoverResultParser_parseAlbum = function _DiscoverResultParser_parseAlbum(item, opts, availableOptions) { const artist = { type: 'artist', name: item.album_artist || item.band_name }; const album = { type: 'album', id: item.id, name: item.title, artist, location: item.band_location }; const genre = item.band_genre_id !== undefined ? availableOptions.genres.find((genre) => item.band_genre_id === genre.id) : null; if (genre?.name) { album.genre = genre.name; } if (item.band_url) { artist.url = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_stripFromDiscoverPage).call(this, item.band_url); } if (item.item_url) { album.url = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_stripFromDiscoverPage).call(this, item.item_url); } if (item.item_image_id && opts.albumImageFormat) { album.imageUrl = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_getImageURL).call(this, item.item_image_id, opts.imageBaseUrl, opts.albumImageFormat, 'a'); } if (item.featured_track) { album.featuredTrack = { id: item.featured_track.id, name: item.featured_track.title, streamUrl: item.featured_track.stream_url }; } if (item.band_bio_image_id && opts.artistImageFormat) { artist.imageUrl = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_getImageURL).call(this, item.band_bio_image_id, opts.imageBaseUrl, opts.artistImageFormat); } if (item.label_name) { album.label = { name: item.label_name }; if (item.label_url) { album.label.url = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_stripFromDiscoverPage).call(this, item.label_url); } } return album; }, _DiscoverResultParser_parseShirt = function _DiscoverResultParser_parseShirt(item, opts) { const shirt = { type: 'shirt', name: item.title, }; if (item.item_url) { shirt.url = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_stripFromDiscoverPage).call(this, item.item_url); } if (item.item_image_id && opts.merchImageFormat) { shirt.imageUrl = { primary: __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_getImageURL).call(this, item.item_image_id, opts.imageBaseUrl, opts.merchImageFormat) }; if (item.tshirt_secondary_image_id) { shirt.imageUrl.secondary = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_getImageURL).call(this, item.tshirt_secondary_image_id, opts.imageBaseUrl, opts.merchImageFormat); } } if (item.release_date) { shirt.releaseDate = item.release_date; } if (item.band_name) { const artist = { type: 'artist', name: item.band_name }; if (item.band_url) { artist.url = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_stripFromDiscoverPage).call(this, item.band_url); } if (item.band_bio_image_id && opts.artistImageFormat) { artist.imageUrl = __classPrivateFieldGet(this, _a, "m", _DiscoverResultParser_getImageURL).call(this, item.band_bio_image_id, opts.imageBaseUrl, opts.artistImageFormat); } shirt.artist = artist; } return shirt; }, _DiscoverResultParser_getImageURL = function _DiscoverResultParser_getImageURL(imageId, imageBaseUrl, format, prefix = '') { return `${imageBaseUrl}/img/${prefix}${imageId}_${format.id}.jpg`; }, _DiscoverResultParser_stripFromDiscoverPage = function _DiscoverResultParser_stripFromDiscoverPage(url) { const urlObj = new url_1.URL(url); const fromValue = urlObj.searchParams.get('from')?.trim(); if (fromValue === 'discover_page') { urlObj.searchParams.delete('from'); } return urlObj.toString(); }; exports.default = DiscoverResultParser; //# sourceMappingURL=DiscoverResultParser.js.map