UNPKG

bandcamp-fetch

Version:
64 lines 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cheerio_1 = require("cheerio"); const Parse_js_1 = require("../utils/Parse.js"); class DiscoverOptionsParser { static parseOptions(html) { const $ = (0, cheerio_1.load)(html); const blob = $('#DiscoverApp[data-blob]').attr('data-blob'); if (!blob) { throw new Parse_js_1.ParseError('Failed to parse discover options: blob not found in data.', html); } let parsed; try { parsed = JSON.parse(blob); } catch (error) { throw new Parse_js_1.ParseError('Failed to parse discover options: JSON error.', blob, error); } const options = parsed?.appData?.initialState; if (options && typeof options === 'object') { const result = { categories: [], genres: [], subgenres: {}, customTags: [], sortBys: [], locations: [], times: [] }; if (Array.isArray(options.categories)) { result.categories = options.categories.map((cat) => ({ name: cat.label, value: cat.id, slug: cat.slug })); } if (Array.isArray(options.genres)) { result.genres = options.genres.map((genre) => ({ name: genre.label, value: genre.slug, id: genre.id })); } if (Array.isArray(options.categories)) { result.categories = options.categories.map((cat) => ({ name: cat.label, value: cat.id, slug: cat.slug })); } if (Array.isArray(options.slices)) { result.sortBys = options.slices.map((s) => ({ name: s.label, value: s.slug })); } if (Array.isArray(options.locations)) { result.locations = options.locations.map((loc) => ({ name: loc.label, value: loc.id })); } if (Array.isArray(options.times)) { result.times = options.times.map((t) => ({ name: t.label, value: t.id, slug: t.slug })); } if (Array.isArray(options.subgenres)) { for (const { id, label, slug, parentSlug } of options.subgenres) { if (id !== undefined && parentSlug && label && slug) { if (!result.subgenres[parentSlug]) { result.subgenres[parentSlug] = []; } result.subgenres[parentSlug].push({ name: label, value: slug, id }); } } } return result; } throw new Parse_js_1.ParseError('Failed to parse discover options: blob is missing or has invalid \'appData.initialState\' field.', parsed); } } exports.default = DiscoverOptionsParser; //# sourceMappingURL=DiscoverOptionsParser.js.map