Add getTagInfo()

This commit is contained in:
patrickkfkan 2021-02-14 19:34:30 +08:00
parent 221f5bda80
commit 231ec8ab04
5 changed files with 135 additions and 0 deletions

View File

@ -211,6 +211,12 @@ Fetches the contents of the Bandcamp Daily article at `articleUrl`.
- artistImageFormat - artistImageFormat
- includeRawData - includeRawData
### `getTagInfo(tagUrl)`
[**Example**](examples/getTagInfo.js) ([output](examples/getTagInfo_output.txt))
Fetches information about the tag referred to by `tagUrl`.
### `getReleasesByTag(tagUrl, [params], [options])` ### `getReleasesByTag(tagUrl, [params], [options])`
[**Example**](examples/getReleasesByTag.js) ([output](examples/getReleasesByTag_output.txt)) [**Example**](examples/getReleasesByTag.js) ([output](examples/getReleasesByTag_output.txt))

10
examples/getTagInfo.js Normal file
View File

@ -0,0 +1,10 @@
const bcfetch = require('../');
const util = require('util');
const tagUrl = 'https://bandcamp.com/tag/dark-ambient';
bcfetch.getTagInfo(tagUrl).then( results => {
console.log(util.inspect(results, false, null, false));
});

View File

@ -0,0 +1,80 @@
{ type: 'tag',
name: 'dark ambient',
url: 'https://bandcamp.com/tag/dark-ambient',
value: 'dark-ambient',
relatedTags:
[ { type: 'tag',
name: 'dungeon synth',
url: 'https://bandcamp.com/tag/dungeon-synth',
value: 'dungeon-synth',
isLocation: false },
{ type: 'tag',
name: 'drone ambient',
url: 'https://bandcamp.com/tag/drone-ambient',
value: 'drone-ambient',
isLocation: false },
{ type: 'tag',
name: 'drone',
url: 'https://bandcamp.com/tag/drone',
value: 'drone',
isLocation: false },
{ type: 'tag',
name: 'field recordings',
url: 'https://bandcamp.com/tag/field-recordings',
value: 'field-recordings',
isLocation: false },
{ type: 'tag',
name: 'atmospheric',
url: 'https://bandcamp.com/tag/atmospheric',
value: 'atmospheric',
isLocation: false },
{ type: 'tag',
name: 'soundscape',
url: 'https://bandcamp.com/tag/soundscape',
value: 'soundscape',
isLocation: false },
{ type: 'tag',
name: 'ambient',
url: 'https://bandcamp.com/tag/ambient',
value: 'ambient',
isLocation: false },
{ type: 'tag',
name: 'industrial',
url: 'https://bandcamp.com/tag/industrial',
value: 'industrial',
isLocation: false },
{ type: 'tag',
name: 'harsh noise',
url: 'https://bandcamp.com/tag/harsh-noise',
value: 'harsh-noise',
isLocation: false },
{ type: 'tag',
name: 'noise',
url: 'https://bandcamp.com/tag/noise',
value: 'noise',
isLocation: false },
{ type: 'tag',
name: 'ambient electronic',
url: 'https://bandcamp.com/tag/ambient-electronic',
value: 'ambient-electronic',
isLocation: false },
{ type: 'tag',
name: 'experimental electronic',
url: 'https://bandcamp.com/tag/experimental-electronic',
value: 'experimental-electronic',
isLocation: false },
{ type: 'tag',
name: 'Russia',
url: 'https://bandcamp.com/tag/russia',
value: 'russia',
isLocation: true },
{ type: 'tag',
name: 'soundtrack',
url: 'https://bandcamp.com/tag/soundtrack',
value: 'soundtrack',
isLocation: false },
{ type: 'tag',
name: 'experimental',
url: 'https://bandcamp.com/tag/experimental',
value: 'experimental',
isLocation: false } ] }

View File

@ -287,6 +287,11 @@ async function getArticle(articleUrl, options = {}) {
.then( html => parser.parseArticle(html, opts) ); .then( html => parser.parseArticle(html, opts) );
} }
async function getTagInfo(tagUrl) {
return _fetchPage(tagUrl)
.then( html => parser.parseTagInfo(html, {tagUrl}) );
}
async function getReleasesByTagFilterOptions(tagUrl) { async function getReleasesByTagFilterOptions(tagUrl) {
return getReleasesByTagFilterValueNames(tagUrl) return getReleasesByTagFilterValueNames(tagUrl)
.then( filterValueNames => { .then( filterValueNames => {
@ -410,6 +415,7 @@ module.exports = {
getArticleCategories, getArticleCategories,
getArticleList, getArticleList,
getArticle, getArticle,
getTagInfo,
getReleasesByTagFilterOptions, getReleasesByTagFilterOptions,
getReleasesByTag, getReleasesByTag,
searchTag, searchTag,

View File

@ -999,6 +999,38 @@ function parseArticle(html, opts) {
return article; return article;
} }
function parseTagInfo(html, opts) {
const $ = cheerio.load(html);
const blob = decode($('#pagedata[data-blob]').attr('data-blob'));
const parsed = JSON.parse(blob);
if (typeof parsed === 'object' && parsed.hub) {
const tag = {
type: 'tag',
name: parsed.hub.name,
url: opts.tagUrl,
value: parsed.hub.norm_name,
relatedTags: []
};
if (Array.isArray(parsed.hub.related_tags)) {
parsed.hub.related_tags.forEach( related => {
const relatedTag = {
type: 'tag',
name: related.name,
url: utils.getUrl(related.url),
value: related.norm_name,
isLocation: related.isloc
};
tag.relatedTags.push(relatedTag);
});
}
return tag;
}
else {
console.log('Failed to parse tag info');
return null;
}
}
function parseHubJSPath(html) { function parseHubJSPath(html) {
const jsMatch = /src="((?:.+?)hub-(?:.+?).js)"/g.exec(html); const jsMatch = /src="((?:.+?)hub-(?:.+?).js)"/g.exec(html);
return jsMatch[1] || null; return jsMatch[1] || null;
@ -1212,6 +1244,7 @@ module.exports = {
parseArticleCategories, parseArticleCategories,
parseArticleList, parseArticleList,
parseArticle, parseArticle,
parseTagInfo,
parseHubJSPath, parseHubJSPath,
parseHubJSFilterValueNames, parseHubJSFilterValueNames,
parseReleasesByTagFilterOptions, parseReleasesByTagFilterOptions,