Get and filter isaac items

mail@pastecode.io avatar
unknown
javascript
a year ago
2.2 kB
0
Indexable
Never
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');

function removeAndReassignKeys(originalDictionary) {
    const updatedDictionary = {};
    const keysToRemove = [43, 59, 61, 235, 263, 587, 613, 620, 630, 648, 656, 662, 666, 718];
    keysToRemove.sort((a, b) => a - b);
  
    let currentKey = 1;
  
    for (let key in originalDictionary) {
      while (keysToRemove.includes(currentKey)) {
        currentKey++;
      }
  
      updatedDictionary[currentKey] = originalDictionary[key];
      currentKey++;
    }
  
    return updatedDictionary;
  }

async function scrapeWebsite() {
    const url = 'https://bindingofisaacrebirth.fandom.com/wiki/Collection_Page_(Repentance)';
    try {
        const response = await axios.get(url);
        const $ = cheerio.load(response.data);

        const itemMapping = {};

        // Select the <td> elements that contain the items
        $('table.collection-page.wikitable tbody tr td').each((index, element) => {
            const span = $(element).find('span.tooltip');
            const img = $(element).find('img[alt*="Collectible"]');

            if (span.length > 0) {
                const entityID = index + 1; // Entity IDs start from 1
                let itemName = span.attr('data-tooltip').split('|')[1];

                if (img.length > 0) {
                    const altText = img.attr('alt');
                    const matches = altText.match(/Collectible\s(.+?)\sicon/);
                    if (matches) {
                        itemName = matches[1];
                    }
                }

                itemMapping[entityID] = itemName;
            }
        });

        const updatedMapping = removeAndReassignKeys(itemMapping);
        // Log the item mapping
        console.log(updatedMapping);

        // Export itemMapping to a JavaScript file
        const jsCode = `const itemMapping = ${JSON.stringify(updatedMapping, null, 2)};\n\nmodule.exports = updatedMapping;`;

        fs.writeFileSync('updatedMapping.js', jsCode);

        console.log('Item mapping exported to updatedMapping.js');
    } catch (error) {
        console.error('Error:', error);
    }
}

scrapeWebsite();