bandcamp-fetch
Version:
Scrape Bandcamp content
72 lines • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cheerio_1 = require("cheerio");
const html_entities_1 = require("html-entities");
class FanItemsBaseParser {
static parsePageItems(html, params) {
const _getSequenceOrPending = (o) => {
return (Array.isArray(o.sequence) && o.sequence.length > 0 ? o.sequence
: Array.isArray(o.pending_sequence) && o.pending_sequence.length > 0 ?
o.pending_sequence
: []);
};
const $ = (0, cheerio_1.load)(html);
const blob = (0, html_entities_1.decode)($('#pagedata[data-blob]').attr('data-blob'));
const parsed = JSON.parse(blob);
const result = {
items: [],
total: 0,
continuation: null
};
const itemListData = parsed[`${params.dataKey}_data`];
const itemCache = parsed.item_cache?.[params.dataKey];
if (itemListData && itemCache) {
const tracklists = parsed.tracklists?.[params.dataKey];
const sequence = _getSequenceOrPending(itemListData);
const parseFn = params.parseItemFn;
sequence.forEach((itemKey) => {
const parsedItem = parseFn(itemCache[itemKey], params, tracklists);
if (parsedItem) {
result.items.push(parsedItem);
}
});
result.total = itemListData.item_count;
const fanId = parsed.fan_data && parsed.fan_data.fan_id ?
parsed.fan_data.fan_id
: null;
if (itemListData.item_count > sequence.length &&
itemListData.last_token &&
fanId) {
result.continuation = {
fanId,
token: itemListData.last_token
};
}
}
return result;
}
static parseContinuationItems(json, continuation, params) {
const items = json[params.dataKey] || [];
const tracklists = json.tracklists || null;
const parseFn = params.parseItemFn;
const result = {
items: [],
continuation: null
};
items.forEach((data) => {
const parsedItem = parseFn(data, params, tracklists);
if (parsedItem) {
result.items.push(parsedItem);
}
});
if (json.more_available && json.last_token) {
result.continuation = {
fanId: continuation.fanId,
token: json.last_token
};
}
return result;
}
}
exports.default = FanItemsBaseParser;
//# sourceMappingURL=FanItemsBaseParser.js.map