Created
December 15, 2023 20:35
-
-
Save 0x7An/24fb33b17ac4eaade9fb6180cd88c909 to your computer and use it in GitHub Desktop.
This script processes a directory of NFT metadata JSON files, generating reports based on attributes and unique values
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
/** | |
* NFT Metadata Processing Script | |
* | |
* This script processes a directory of NFT metadata JSON files, generating reports based on attributes and unique values. | |
* | |
* Usage: | |
* node scriptName.js [options] | |
* | |
* Options: | |
* frequencies Display the frequency count of each attribute across all NFTs. | |
* rarities Display the rarity score of each NFT based on its attributes. | |
* unique Display any unique non-attribute values found in the NFT metadata. | |
* | |
* Example: | |
* node metadata-checker.js f r | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
const directoryPath = path.join(__dirname, '../legendary-metadata'); | |
// Function to process JSON files and extract data | |
const processNFTs = (dir) => { | |
const attributeFrequencies = {}; | |
const nfts = []; | |
const uniqueFlags = {}; | |
// Reading and processing each file | |
fs.readdirSync(dir).forEach(file => { | |
const fullPath = path.join(dir, file); | |
const fileData = fs.readFileSync(fullPath); | |
const json = JSON.parse(fileData); | |
nfts.push(json); | |
// Counting attribute frequencies | |
if (json.attributes && Array.isArray(json.attributes)) { | |
json.attributes.forEach(attr => { | |
const key = `${attr.trait_type}:${attr.value}`; | |
attributeFrequencies[key] = (attributeFrequencies[key] || 0) + 1; | |
}); | |
} | |
// Flagging unique non-attribute values | |
Object.keys(json).forEach(key => { | |
if (key !== 'attributes' && key !== 'name') { | |
uniqueFlags[key] = uniqueFlags[key] || new Set(); | |
uniqueFlags[key].add(json[key]); | |
} | |
}); | |
}); | |
// Calculating rarity for each NFT | |
const nftRarities = nfts.map(nft => { | |
let rarityScore = 0; | |
if (nft.attributes) { | |
nft.attributes.forEach(attr => { | |
const key = `${attr.trait_type}:${attr.value}`; | |
rarityScore += 1 / attributeFrequencies[key]; | |
}); | |
} | |
return { name: nft.name, rarityScore }; | |
}); | |
// Sort NFTs by rarity | |
nftRarities.sort((a, b) => a.rarityScore - b.rarityScore); | |
// Identify unique non-attribute values | |
const uniqueValues = {}; | |
Object.keys(uniqueFlags).forEach(key => { | |
if (uniqueFlags[key].size === 1) { | |
uniqueValues[key] = Array.from(uniqueFlags[key]); | |
} | |
}); | |
return { attributeFrequencies, nftRarities, uniqueValues }; | |
}; | |
const parseArguments = () => { | |
const args = process.argv.slice(2); | |
const options = { | |
showFrequencies: args.includes('f'), | |
showRarities: args.includes('r'), | |
showUniqueValues: args.includes('u') | |
}; | |
return options; | |
}; | |
const main = () => { | |
const options = parseArguments(); | |
const { attributeFrequencies, nftRarities, uniqueValues } = processNFTs(directoryPath); | |
if (options.showFrequencies) { | |
console.log("Attribute Frequencies:", attributeFrequencies); | |
} | |
if (options.showRarities) { | |
console.log("NFT Rarities:", nftRarities); | |
} | |
if (options.showUniqueValues) { | |
console.log("Unique Non-Attribute Values:", uniqueValues); | |
} | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment