124 lines
2.8 KiB
JavaScript
124 lines
2.8 KiB
JavaScript
const querystring = require('querystring');
|
|
const {URL} = require('url');
|
|
|
|
function getUrl(href, baseUrl) {
|
|
if (baseUrl === undefined) {
|
|
baseUrl = getSiteUrl();
|
|
}
|
|
return new URL(href, baseUrl).toString();
|
|
}
|
|
|
|
function getSiteUrl() {
|
|
return 'https://bandcamp.com';
|
|
}
|
|
|
|
function getDiscoverUrl(params = {}) {
|
|
const qs = {
|
|
s: params.sortBy || 'top',
|
|
p: params.page || 0,
|
|
};
|
|
if (params.genre) {
|
|
qs.g = params.genre;
|
|
|
|
if (params.subgenre) {
|
|
qs.t = params.subgenre;
|
|
}
|
|
}
|
|
if (params.location !== undefined) {
|
|
qs.gn = params.location;
|
|
}
|
|
if (params.format) {
|
|
qs.f = params.format;
|
|
}
|
|
if (qs.s === 'rec' && params.artistRecommendationType) {
|
|
qs.r = params.artistRecommendationType;
|
|
}
|
|
if (params.time !== undefined) {
|
|
qs.w = params.time;
|
|
}
|
|
return getSiteUrl() + '/api/discover/3/get_web?' + querystring.encode(qs);
|
|
}
|
|
|
|
function reformatImageUrl(imageUrl, imageFormat) {
|
|
if (typeof imageUrl === 'string' && imageFormat !== null) {
|
|
// regex taken from:
|
|
// https://github.com/masterT/bandcamp-scraper/blob/master/lib/htmlParser.js
|
|
return imageUrl.replace(/_\d{1,3}\./, `_${imageFormat.id}.`);
|
|
}
|
|
else if (typeof imageUrl === 'string') {
|
|
return imageUrl;
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function stripTags(str) {
|
|
// https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
|
|
return str.replace(/(<([^>]+)>)/gi, '');
|
|
}
|
|
|
|
function stripLineBreaks(str) {
|
|
return str.replace(/(\r\n|\n|\r)/gm, ' ');
|
|
}
|
|
|
|
function brToNewLine(str) {
|
|
// https://stackoverflow.com/questions/5959415/jquery-javascript-regex-replace-br-with-n
|
|
return str.replace(/<br\s*[\/]?>/gi, '\n');
|
|
}
|
|
|
|
function substrAfter(str, after) {
|
|
let afterIndex = str.indexOf(after);
|
|
if (afterIndex >= 0) {
|
|
return str.substr(afterIndex + after.length);
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function substrBefore(str, before) {
|
|
let beforeIndex = str.indexOf(before);
|
|
if (beforeIndex >= 0) {
|
|
return str.substr(0, beforeIndex);
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function splitUrl(url) {
|
|
const _url = new URL(url);
|
|
return {
|
|
base: _url.protocol + '//' + _url.host,
|
|
path: _url.pathname,
|
|
query: _url.search
|
|
};
|
|
}
|
|
|
|
function getSearchUrl(params = {}) {
|
|
const qs = {
|
|
q: params.query,
|
|
page: params.page || 1,
|
|
};
|
|
return getSiteUrl() + '/search?' + querystring.encode(qs);
|
|
}
|
|
|
|
function stripMultipleWhitespaces(str) {
|
|
return str.replace(/\s+/g, ' ');
|
|
}
|
|
|
|
module.exports = {
|
|
getUrl,
|
|
getSiteUrl,
|
|
getDiscoverUrl,
|
|
reformatImageUrl,
|
|
stripTags,
|
|
stripLineBreaks,
|
|
brToNewLine,
|
|
substrAfter,
|
|
substrBefore,
|
|
splitUrl,
|
|
getSearchUrl,
|
|
stripMultipleWhitespaces
|
|
}; |