Files
bc-wishlist-scraper/getWishlist.mjs
T

92 lines
3.0 KiB
JavaScript

import bcfetch from 'bandcamp-fetch';
import fs from 'fs/promises'; // Using the promise-based version of fs for clean async/await
const username = 'noschxl';
async function fetchEntireWishlist() {
let previousWishlist = [];
// 1. Try to load the previous wishlist to compare against
try {
const fileData = await fs.readFile('wishlist.json', 'utf8');
previousWishlist = JSON.parse(fileData);
console.log(`Loaded ${previousWishlist.length} previous items from wishlist.json for comparison.`);
} catch (error) {
if (error.code === 'ENOENT') {
console.log('No existing wishlist.json found. This must be the first run!');
} else {
console.error('Error reading wishlist.json:', error);
}
}
let allWishlistItems = [];
let currentParams = {
target: username,
imageFormat: 'art_app_large'
};
let hasMorePages = true;
let pageCount = 1;
console.log(`\nStarting wishlist scrape for user: ${username}...`);
// 2. Scrape the current wishlist
while (hasMorePages) {
console.log(`Fetching page ${pageCount}...`);
try {
const result = await bcfetch.fan.getWishlist(currentParams);
if (result && result.items) {
allWishlistItems.push(...result.items);
}
if (result.continuation) {
currentParams.target = result.continuation;
pageCount++;
} else {
hasMorePages = false;
}
} catch (error) {
console.error(`Error fetching page ${pageCount}:`, error);
break;
}
}
console.log(`\nDone! Successfully scraped ${allWishlistItems.length} items from your wishlist.`);
// 3. Compare the old list against the new list
if (previousWishlist.length > 0) {
// Create a Set of all the current IDs for fast, easy lookup
const currentIds = new Set(allWishlistItems.map(item => item.id));
// Filter the previous list to find items whose IDs are missing from the current scrape
const missingItems = previousWishlist.filter(item => !currentIds.has(item.id));
if (missingItems.length > 0) {
console.log(`\nWARNING: ${missingItems.length} item(s) have disappeared since the last run!`);
missingItems.forEach(item => {
console.log(`- ${item.artist.name} : ${item.name}`);
console.log(` Old URL: ${item.url}\n`);
});
// Optional: Save the missing items to a separate file so you don't lose their metadata
await fs.writeFile('missing_items.json', JSON.stringify(missingItems, null, 2));
console.log('Saved missing items to missing_items.json');
} else {
console.log('\nAll good! No items have disappeared since your last run.');
}
}
// 4. Overwrite wishlist.json with the new, up-to-date data for the next run
try {
await fs.writeFile('wishlist.json', JSON.stringify(allWishlistItems, null, 2));
console.log('Successfully updated wishlist.json with current data.');
} catch (error) {
console.error('Error writing to wishlist.json:', error);
}
}
fetchEntireWishlist();