bandcamp-fetch
Version:
Scrape Bandcamp content
31 lines • 1.23 kB
JavaScript
import { load as cheerioLoad } from 'cheerio';
import { decode } from 'html-entities';
import { ParseError } from '../utils/Parse.js';
export default class ImageParser {
static parseImageConstants(html) {
const $ = cheerioLoad(html);
const vars = decode($('script[data-vars]').attr('data-vars'));
let parsed;
try {
parsed = JSON.parse(vars);
}
catch (error) {
throw new 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 ParseError('Failed to parse image constants: data is missing \'client_template_globals\' field.', parsed);
}
}
//# sourceMappingURL=ImageParser.js.map