bandcamp-fetch
Version:
Scrape Bandcamp content
34 lines • 1.37 kB
JavaScript
;
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");
class ImageParser {
static parseImageConstants(html) {
const $ = (0, cheerio_1.load)(html);
const vars = (0, html_entities_1.decode)($('script[data-vars]').attr('data-vars'));
let parsed;
try {
parsed = JSON.parse(vars);
}
catch (error) {
throw new Parse_js_1.ParseError('Failed to parse image constants: JSON error.', vars, error);
}
if (parsed?.client_template_globals) {
const formats = parsed.client_template_globals.image_formats;
return {
baseUrl: parsed.client_template_globals.image_siteroot_https,
formats: formats?.map((format) => ({
id: format.id,
name: format.name,
width: format.width,
height: format.height,
fileFormat: format.file_format
}))
};
}
throw new Parse_js_1.ParseError('Failed to parse image constants: data is missing \'client_template_globals\' field.', parsed);
}
}
exports.default = ImageParser;
//# sourceMappingURL=ImageParser.js.map