UNPKG

musescore-metadata

Version:
103 lines (102 loc) 4.93 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.museSearch = exports.museSet = exports.muse = void 0; const cheerio = require("cheerio"); const node_fetch_1 = require("node-fetch"); function findValueByPrefix(object, prefix) { for (const property in object) if (object[property] && property.toString().startsWith(prefix)) return object[property]; return undefined; } function parseBody(body) { const $ = cheerio.load(body); const meta = $('meta[property="og:image"]')[0]; const image = meta.attribs.content; const firstPage = image.split("@")[0]; const stores = Array.from($('div[class^="js-"]')); const found = stores.find(x => x.attribs && x.attribs.class && x.attribs.class.match(/^js-\w+$/) && findValueByPrefix(x.attribs, "data-")); const store = findValueByPrefix(found.attribs, "data-"); if (!store) throw new Error("Cannot find store."); return Object.assign(JSON.parse(store).store.page.data.score, { firstPage }); } function parseSet(body) { const $ = cheerio.load(body); const stores = Array.from($('div[class^="js-"]')); const found = stores.find(x => x.attribs && x.attribs.class && x.attribs.class.match(/^js-\w+$/) && findValueByPrefix(x.attribs, "data-")); const store = findValueByPrefix(found.attribs, "data-"); if (!store) throw new Error("Cannot find store."); const data = JSON.parse(store).store.page.data; return Object.assign(data.set, { user: data.user, scores: data.scores, pagination: data.pagination }); } function parseSearch(body) { const $ = cheerio.load(body); const stores = Array.from($('div[class^="js-"]')); const found = stores.find(x => x.attribs && x.attribs.class && x.attribs.class.match(/^js-\w+$/) && findValueByPrefix(x.attribs, "data-")); const store = findValueByPrefix(found.attribs, "data-"); if (!store) throw new Error("Cannot find store."); const parsed = JSON.parse(store).store; delete parsed.user; delete parsed.notifications; delete parsed.page.experiments; delete parsed.page.data.filters; return parsed; } function muse(url) { return __awaiter(this, void 0, void 0, function* () { const response = yield (0, node_fetch_1.default)(url); if (!response.ok) throw new Error(`Received HTTP status code ${response.status} when fetching data.`); return parseBody(yield response.text()); }); } exports.muse = muse; function museSet(url, options = {}) { return __awaiter(this, void 0, void 0, function* () { if (options.all) url = url.split("?")[0]; else if (options.page) url = url.split("?")[0] + "?page=" + options.page; const response = yield (0, node_fetch_1.default)(url); if (!response.ok) throw new Error(`Received HTTP status code ${response.status} when fetching data.`); const parsed = parseSet(yield response.text()); if (!options.all || parsed.pagination.totalCount < parsed.pagination.defaultPageSize) return parsed; for (let ii = 2; ii <= Math.ceil(parsed.pagination.totalCount / parsed.pagination.defaultPageSize); ii++) { const response = yield (0, node_fetch_1.default)(url + "?page=" + ii); if (!response.ok) throw new Error(`Received HTTP status code ${response.status} when fetching data.`); const more = parseSet(yield response.text()); parsed.scores = parsed.scores.concat(more.scores); } return parsed; }); } exports.museSet = museSet; function museSearch(query, options = {}) { return __awaiter(this, void 0, void 0, function* () { let url = `https://musescore.com/sheetmusic?text=${encodeURIComponent(query)}`; if (options.page) url += `page=${options.page}`; if (options.sort) url += `sort=${options.sort}`; const response = yield (0, node_fetch_1.default)(url); if (!response.ok) throw new Error(`Received HTTP status code ${response.status} when fetching data.`); return parseSearch(yield response.text()); }); } exports.museSearch = museSearch;