UNPKG

bandcamp-fetch

Version:
95 lines 4.68 kB
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, _ShowParser_getShowIdFromUrl, _ShowParser_getShowUrl; import { load as cheerioLoad } from 'cheerio'; import { decode } from 'html-entities'; import { ParseError, splitUrl } from '../utils/Parse.js'; import { URLS } from '../utils/Constants.js'; class ShowParser { static parseShow(html, opts) { const $ = cheerioLoad(html); const blob = decode($('#pagedata[data-blob]').attr('data-blob')); let parsed; try { parsed = JSON.parse(blob); } catch (error) { throw new ParseError('Failed to parse show: JSON error in data-blob.', html, error); } if (typeof parsed === 'object' && parsed.bcw_data) { const showId = __classPrivateFieldGet(this, _a, "m", _ShowParser_getShowIdFromUrl).call(this, opts.showUrl); let showInfo; if (showId !== null) { showInfo = parsed.bcw_data?.[showId]; } if (showInfo) { const show = { type: 'show', name: showInfo.title, url: __classPrivateFieldGet(this, _a, "m", _ShowParser_getShowUrl).call(this, showInfo.show_id), publishedDate: showInfo.published_date, description: showInfo.desc, shortDescription: showInfo.short_desc, imageCaption: showInfo.image_caption, subtitle: showInfo.subtitle, duration: showInfo.audio_duration, screenImageUrl: `${opts.imageBaseUrl}/img/${showInfo.show_v2_image_id}_0` }; const mp3StreamUrl = showInfo.audio_stream?.['mp3-128']; const opusStreamUrl = showInfo.audio_stream?.['opus-lo']; if (mp3StreamUrl || opusStreamUrl) { show.streamUrl = {}; if (mp3StreamUrl) { show.streamUrl['mp3-128'] = mp3StreamUrl; } if (opusStreamUrl) { show.streamUrl['opus-lo'] = opusStreamUrl; } } if (opts.showImageFormat?.id) { show.imageUrl = `${opts.imageBaseUrl}/img/${showInfo.show_v2_image_id}_${opts.showImageFormat.id}.jpg`; } show.tracks = showInfo.tracks.map((track) => { const trackItem = { id: track.track_id, name: track.title, url: track.track_url, seekPosition: track.timecode, artist: { name: track.artist, url: `https://${track.url_hints.subdomain}.bandcamp.com`, location: track.location_text } }; if (opts.albumImageFormat?.id) { trackItem.imageUrl = `${opts.imageBaseUrl}/img/a${track.track_art_id}_${opts.albumImageFormat.id}.jpg`; } if (opts.artistImageFormat?.id && trackItem.artist) { trackItem.artist.imageUrl = `${opts.imageBaseUrl}/img/${track.bio_image_id}_${opts.artistImageFormat.id}.jpg`; } if (track.album_title) { trackItem.album = { id: track.album_id, name: track.album_title, url: track.album_url }; } return trackItem; }); return show; } } throw new ParseError('Failed to parse show: missing bcw_data.', html); } } _a = ShowParser, _ShowParser_getShowIdFromUrl = function _ShowParser_getShowIdFromUrl(showUrl) { const params = splitUrl(showUrl).searchParams; return params.get('show'); }, _ShowParser_getShowUrl = function _ShowParser_getShowUrl(showId) { return `${URLS.SITE_URL}/?show=${showId}`; }; export default ShowParser; //# sourceMappingURL=ShowParser.js.map