bandcamp-fetch
Version:
Scrape Bandcamp content
97 lines • 4.85 kB
JavaScript
;
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;
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, opts) {
const $ = (0, cheerio_1.load)(html);
const blob = (0, html_entities_1.decode)($('#pagedata[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);
}
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 Parse_js_1.ParseError('Failed to parse show: missing bcw_data.', html);
}
}
_a = ShowParser, _ShowParser_getShowIdFromUrl = function _ShowParser_getShowIdFromUrl(showUrl) {
const params = (0, Parse_js_1.splitUrl)(showUrl).searchParams;
return params.get('show');
}, _ShowParser_getShowUrl = function _ShowParser_getShowUrl(showId) {
return `${Constants_js_1.URLS.SITE_URL}/?show=${showId}`;
};
exports.default = ShowParser;
//# sourceMappingURL=ShowParser.js.map