UNPKG

bandcamp-fetch

Version:
190 lines 8.06 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _a, _SearchResultsParser_matchSearchItemTypeWithResult; Object.defineProperty(exports, "__esModule", { value: true }); const cheerio_1 = require("cheerio"); const Parse_js_1 = require("../utils/Parse.js"); const SearchAPI_js_1 = require("./SearchAPI.js"); const VALID_RESULT_TYPES = ['artist', 'label', 'album', 'track', 'fan']; class SearchResultsParser { static parseResults(html, opts) { const $ = (0, cheerio_1.load)(html); const resultsList = $('li.searchresult'); const items = []; resultsList.each((i, resultListItem) => { resultListItem = $(resultListItem); const resultInfo = resultListItem.find('.result-info'); // Common info const resultType = resultInfo .children('.itemtype') .text() .trim() .toLowerCase(); const imgSrc = $('.art img', resultListItem).attr('src'); const heading = $('.heading a', resultInfo); const name = heading.text().trim(); const url = resultInfo.find('.itemurl').text().trim(); const imageUrl = (0, Parse_js_1.reformatImageUrl)(imgSrc, resultType === 'album' || resultType === 'track' ? opts.albumImageFormat : opts.artistImageFormat); if (!__classPrivateFieldGet(this, _a, "m", _SearchResultsParser_matchSearchItemTypeWithResult).call(this, opts.itemType, resultType) || !name || !url) { return true; } // Other info let location, genre, tags, artist, numTracks, duration, releaseDate, album; resultInfo .find('.subhead, .genre, .tags, .released, .length') .each((i, info) => { info = $(info); if (info.hasClass('subhead')) { if (resultType === 'artist' || resultType === 'label') { location = info.text().trim(); } else if (resultType === 'album' || resultType === 'track') { const infoText = info.text(); artist = (0, Parse_js_1.substrAfter)(infoText, 'by ')?.trim(); if (resultType === 'track') { album = (0, Parse_js_1.substrBefore)(infoText, ' by'); if (album) { album = (0, Parse_js_1.substrAfter)(album, 'from ')?.trim(); } } } return true; } if (info.hasClass('genre')) { genre = (0, Parse_js_1.substrAfter)(info.text(), 'genre: ')?.trim(); return true; } if (info.hasClass('tags')) { tags = (0, Parse_js_1.substrAfter)(info.text(), 'tags:'); if (tags) { tags = (0, Parse_js_1.stripLineBreaks)((0, Parse_js_1.stripMultipleWhitespaces)(tags)).trim(); } return true; } if (info.hasClass('released')) { releaseDate = (0, Parse_js_1.substrAfter)(info.text(), 'released ')?.trim(); return true; } if (info.hasClass('length')) { const lengthParts = info.text().split(','); const tracksText = lengthParts[0]; const minutesText = lengthParts[1]; const numTracksStr = tracksText ? (0, Parse_js_1.substrBefore)(tracksText, 'tracks') : null; if (numTracksStr) { numTracks = parseInt(numTracksStr, 10); } const durationStr = minutesText ? (0, Parse_js_1.substrBefore)(minutesText, 'minutes') : null; if (durationStr) { duration = parseInt(durationStr, 10) * 60; } } }); const base = { name: heading.text().trim(), url: resultInfo.find('.itemurl').text().trim() || '' }; if (imageUrl) { base.imageUrl = imageUrl; } if (resultType === 'artist') { const artist = { type: 'artist', ...base }; if (location) artist.location = location; if (genre) artist.genre = genre; if (tags) artist.tags = tags; items.push(artist); } else if (resultType === 'label') { const label = { type: 'label', ...base }; if (location) label.location = location; items.push(label); } else if (resultType === 'album') { const album = { type: 'album', ...base }; if (artist) album.artist = artist; if (numTracks) album.numTracks = numTracks; if (duration) album.duration = duration; if (releaseDate) album.releaseDate = releaseDate; if (tags) album.tags = tags; items.push(album); } else if (resultType === 'track') { const track = { type: 'track', ...base }; if (artist) track.artist = artist; if (album) track.album = album; if (releaseDate) track.releaseDate = releaseDate; if (tags) track.tags = tags; items.push(track); } else if (resultType === 'fan') { const fan = { type: 'fan', ...base }; if (genre) fan.genre = genre; items.push(fan); } }); let totalPages = parseInt($('.pagelist').find('.pagenum').last().text(), 10); if (isNaN(totalPages)) { totalPages = 1; } return { items, totalPages }; } } _a = SearchResultsParser, _SearchResultsParser_matchSearchItemTypeWithResult = function _SearchResultsParser_matchSearchItemTypeWithResult(searchType, resultType) { if (searchType === SearchAPI_js_1.SearchItemType.All && VALID_RESULT_TYPES.includes(resultType)) { return true; } switch (searchType) { case SearchAPI_js_1.SearchItemType.ArtistsAndLabels: return resultType === 'artist' || resultType === 'label'; case SearchAPI_js_1.SearchItemType.Albums: return resultType === 'album'; case SearchAPI_js_1.SearchItemType.Tracks: return resultType === 'track'; case SearchAPI_js_1.SearchItemType.Fans: return resultType === 'fan'; default: return false; } }; exports.default = SearchResultsParser; //# sourceMappingURL=SearchResultsParser.js.map