bandcamp-fetch
Version:
Scrape Bandcamp content
57 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cheerio_1 = require("cheerio");
const Parse_js_1 = require("../utils/Parse.js");
class ImageParser {
static parseImageConstants(html) {
const $ = (0, cheerio_1.load)(html);
const scripts = $('script')
.map((_, el) => $(el).html())
.toArray();
let template = null;
for (const script of scripts) {
const templateRegex = /var TemplGlobals = (.+);/gm;
const templateMatch = templateRegex.exec(script);
if (templateMatch && templateMatch[1]) {
try {
template = JSON.parse(templateMatch[1]);
}
catch (error) {
template = null;
}
if (template && typeof template === 'object') {
break;
}
}
}
try {
if (!template) {
throw Error('TemplGlobals object not found or has invalid type');
}
const formats = template.image_formats;
if (!formats || !Array.isArray(formats)) {
throw Error('TemplGlobals.image_formats not found or has invalid type');
}
const siteRoot = template.image_siteroot_https;
if (!siteRoot || typeof siteRoot !== 'string') {
throw Error('TemplGlobals.image_siteroot_https not found or has invalid type');
}
return {
baseUrl: siteRoot,
formats: formats.map((format) => ({
id: format.id,
name: format.name,
width: format.width,
height: format.height,
fileFormat: format.file_format
}))
};
}
catch (error) {
const msg = error instanceof Error ? error.message : String(error);
throw new Parse_js_1.ParseError(`Failed to parse image constants: ${msg}`, template);
}
}
}
exports.default = ImageParser;
//# sourceMappingURL=ImageParser.js.map