Last active
August 18, 2023 17:27
-
-
Save BillMoriarty/7c6f4b1c74b5ea25f0c16a958f3ba08a to your computer and use it in GitHub Desktop.
This script generates markdown files for Bandcamp albums. It takes an array of Bandcamp URLs and an output folder as input. For each URL, the script extracts the album name, artist name, and album ID from the Bandcamp page. It then generates a markdown file with an embedded Bandcamp player for the album. The markdown file is named after the arti…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const https = require('https'); | |
const path = require('path'); | |
// replace below folder you want to save this to | |
const outputFolder = '/path/to/local/folder/'; | |
// add Bandcamp URLs here - here is an example url that will take you to a great album, by the way | |
// note - Bill did not work on Fourtet - he just likes this music | |
const bandcampUrls = [ | |
'https://fourtet.bandcamp.com/album/pink', | |
]; | |
/** | |
* Generates markdown files for Bandcamp albums. | |
* @param {string[]} bandcampUrls - An array of Bandcamp URLs. | |
* @param {string} outputFolder - The path to the local folder where the markdown files will be saved. | |
* | |
* For each URL, the function extracts the album name, artist name, and album ID from the Bandcamp page. | |
* It then generates a markdown file with an embedded Bandcamp player for the album. | |
* The markdown file is named after the artist and album name and saved to the specified output folder. | |
* If a markdown file with the same name already exists in the output folder, the function will not overwrite it. | |
* | |
* The function replaces HTML entities for apostrophes in the album and artist names with actual apostrophes. | |
* It also replaces any forward slashes in the album name with hyphens when generating the markdown file name. | |
*/ | |
async function generateMarkdown(bandcampUrls, outputFolder) { | |
for (const url of bandcampUrls) { | |
// get album name, artist name, album ID from the Bandcamp page | |
const page = await get(url); | |
const title = page.match(/<title>([^<]*)<\/title>/)[1]; | |
let [albumName, artistName] = title.split(' | '); | |
// replace HTML entities with their corresponding characters | |
albumName = albumName.replace(/'/g, "'").replace(/&/g, '&'); | |
artistName = artistName.replace(/'/g, "'").replace(/&/g, '&'); | |
const albumId = page.match(/<meta property="twitter:player" content="[^"]*album=(\d+)[^"]*">/)[1]; | |
// generate markdown file name and embed | |
const fileName = `${artistName} - ${albumName.replace(/\//g, '-')}.md`; | |
const filePath = path.join(outputFolder, fileName); | |
const fileContent = `<iframe style="border: 0; width: 350px; height: 786px;" src="https://bandcamp.com/EmbeddedPlayer/album=${albumId}/size=large/bgcol=ffffff/linkcol=0687f5/transparent=true/" seamless><a href="${url}">${albumName} by ${artistName}</a></iframe>`; | |
// check if the markdown file already exists | |
if (!fs.existsSync(filePath)) { | |
// write the markdown file | |
fs.writeFileSync(filePath, fileContent); | |
console.log(`Generated markdown file: ${fileName}`); | |
} else { | |
console.log(`Markdown file already exists: ${fileName}`); | |
} | |
} | |
} | |
function get(url) { | |
return new Promise((resolve, reject) => { | |
https.get(url, res => { | |
let data = ''; | |
res.on('data', chunk => data += chunk); | |
res.on('end', () => resolve(data)); | |
}).on('error', reject); | |
}); | |
} | |
generateMarkdown(bandcampUrls, outputFolder); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment