UNPKG

bandcamp-fetch

Version:
83 lines 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cheerio_1 = require("cheerio"); const html_entities_1 = require("html-entities"); const Parse_js_1 = require("../utils/Parse.js"); const Constants_js_1 = require("../utils/Constants.js"); class ShowParser { static parseShow(html, json, opts) { const $ = (0, cheerio_1.load)(html); const blob = (0, html_entities_1.decode)($('#ArchiveApp[data-blob]').attr('data-blob')); let parsed; try { parsed = JSON.parse(blob); } catch (error) { throw new Parse_js_1.ParseError('Failed to parse show: JSON error in data-blob.', html, error); } const appData = typeof parsed === 'object' ? parsed.appData : null; const showId = appData?.showId; const shows = appData?.shows; const showInfo = showId && Array.isArray(shows) ? shows.find((value) => value.itemId === showId) : null; const tracksInfo = typeof json === 'object' && Array.isArray(json.tracks) ? json.tracks : null; const audioInfo = typeof json === 'object' && typeof json.compiledTrack === 'object' ? json.compiledTrack : null; if (showInfo) { const show = { type: 'show', name: showInfo.title, url: new URL(showInfo.url, Constants_js_1.URLS.SITE_URL).toString(), publishedDate: showInfo.date, description: showInfo.description, shortDescription: showInfo.shortDesc, imageCaption: showInfo.metadata, screenImageUrl: `${opts.imageBaseUrl}/img/${showInfo.imageId}_0` }; if (audioInfo) { const { streamUrl, duration } = audioInfo; if (streamUrl) { show.streamUrl = streamUrl; } if (duration) { show.duration = duration; } } if (opts.showImageFormat?.id) { show.imageUrl = `${opts.imageBaseUrl}/img/${showInfo.imageId}_${opts.showImageFormat.id}.jpg`; } if (tracksInfo) { show.tracks = tracksInfo.map((track) => { const trackItem = { id: track.id, name: track.title, url: track.url, seekPosition: track.timecode, artist: { name: track.artistName, url: track.bandUrl } }; if (opts.albumImageFormat?.id && track.artId) { trackItem.imageUrl = `${opts.imageBaseUrl}/img/a${track.artId}_${opts.albumImageFormat.id}.jpg`; } if (track.album && typeof track.album === 'object') { trackItem.album = { name: track.album.title, url: track.album.url }; } return trackItem; }); } return show; } throw new Parse_js_1.ParseError('Failed to parse show: "appData" not found in data-blob or contains invalid data.', blob); } } exports.default = ShowParser; //# sourceMappingURL=ShowParser.js.map